I am having an issue with my auth flow, and hoping someone with keen eye might help me resolve the issue. The error I am getting is [next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error Cannot read properties of undefined (reading 'jwt')
, and have since reviewed the code multiple times to see what might have gone wrong. Here are the steps I took, and how you might be able to reproduce the error:
- Set up and configured
[...nextauth].js file
as shown below - Updated Providers with credentials needed.
Upon trying to login to test, I got the error messages as shown below.
frontend/pages/api/auth/[...nextauth].js
import NextAuth from "next-auth";
import GoogleProvider from 'next-auth/providers/google';
import FacebookProvider from 'next-auth/providers/facebook';
import EmailProvider from 'next-auth/providers/email';
const options = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
}),
],
pages: {
signIn: '/signin',
signOut: '/',
error: '/auth/error', // Error code passed in query string as ?error=
verifyRequest: '/auth/verify-request', // (used for check email message)
newUser: '/firstpage', // New users will be directed here on first sign in (leave the property out if not of interest)
},
secret: process.env.NEXTAUTH_SECRET,
database: process.env.NEXT_PUBLIC_DATABASE_URL,
session: {
jwt: true,
},
callbacks: {
session: async (session, user) => {
session.jwt = user.jwt;
session.id = user.id;
return Promise.resolve(session);
},
jwt: async (token, user, account) => {
const isSignIn = user ? true : false;
if (isSignIn) {
const response = await fetch(
`${process.env.NEXT_PUBLIC_STRAPI_API_URL}/auth/${account.provider}/callback?access_token=${account?.accessToken}`
);
const data = await response.json();
console.log("DATTA",data);
token.jwt = data.jwt;
token.id = data.user.id;
}
return Promise.resolve(token);
},
},
};
const Auth = (req, res) =>
NextAuth(req, res, options);
export default Auth;
Error below
[next-auth][error][JWT_SESSION_ERROR]
https://next-auth.js.org/errors#jwt_session_error Cannot read properties of undefined (reading 'jwt') {
message: "Cannot read properties of undefined (reading 'jwt')",
stack: "TypeError: Cannot read properties of undefined (reading 'jwt')n" +
' at Object.session (webpack-internal:///(api)/./pages/api/auth/[...nextauth].js:45:32)n' +
' at Object.session (/Users/PATH-TO-FILES...)n'
name: 'TypeError'
}
2
Answers
Try to replace URI in jwt callback to:
const response = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/api/auth/${account.provider}/callback?access_token=${account?.access_token}`);
Here is my configuration for
[...nextauth].js
. Note: I’m usingauthOptions
insteadoptions
to be able to get a session on a server ingetServerSideProps
.There are a number of reasons this can happen but in your case, remove this line.
That should fix it. Logging the response
const data = await response.json();
inside here causes the issue. I’m not sure exactly why that’s the case, but that should fix the error.For those of you who can’t fix with this solution, read this thread and use the saved answer.