session.data.expires undefined first time using useSession in v4 #12197
-
I am using jwt strategic with the following config (NextAuthOptions):
when I use "const session = useSession()" y return"status =authenticated" but "data.expires" is undefined
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey, in NextAuth.js v4, the expires field is only set if it is explicitly specified when the JWT is created. If you use a JWT strategy and do not implement custom logic that adds the expires field, it remains undefined by default. SolutionYou can ensure that the expires field is set correctly by explicitly adding it in the JWT callback. In the NextAuthOptions configuration, you can use the JWT callback to add the expires property: callbacks: {
async jwt({ token, account, user }) {
if (account && user) {
token.expires = Date.now() + 1 * 60 * 60 * 1000;
}
return token;
},
async session({ session, token }) {
// Add expires to the session object
if (token.expires) {
session.expires = token.expires;
}
return session;
},
}, |
Beta Was this translation helpful? Give feedback.
@jmpena2011
Okay, if this is the case, you can may use the
session.update()
method. This solution is not as clean as the jwt one, but in this case it's your best possible solution i guess.