'Next Auth.js - I can't get token with getToken({req})

I can't get the token with getToken:

enter image description here

This variables are ok: NEXTAUTH_SECRET=secret NEXTAUTH_URL=http://localhost:3000

Here is my [...nextauth].js - I can do console.log(token) and it works well

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";

...
  jwt: {
    secret: process.env.JWT_SECRET,
    encryption: true,
  },
  secret: process.env.NEXTAUTH_SECRET,
  callbacks: {
    async redirect({ url, baseUrl }) {
      return Promise.resolve(url);
    },
    async jwt({ token, user, account, profile, isNewUser }) {
      return token;
    },
    async session({ session, user, token }) {
      return session;
    },
  },
});

API section (I think getToken doesnt work well):

import { getToken } from "next-auth/jwt";

const secret = process.env.NEXTAUTH_SECRET;

export default async (req, res) => {
  const token = await getToken({ req, secret, encryption: true });
  console.log(token);
  if (token) {
    // Signed in
    console.log("JSON Web Token", JSON.stringify(token, null, 2));
  } else {
    // Not Signed in
    res.status(401);
  }
  res.end();
};


Solution 1:[1]

Try to pass req without destructuring it.

e.g.

const token = await getToken(req, secret, encryption: true);

instead of .. await getToken({req, secret, encryption: true})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David