admin 管理员组

文章数量: 1086019

callbacks: {
  async jwt({ token, user }) {
    if (user) {
      token.user = jwtDecode<UserToken>(user.accessToken);
      token.user.accessToken = user.accessToken;

      if (token.user.exp) {
        token.exp = token.user.exp;
      }
    }
    console.log('JWT Callback - Token Exp:', token.exp); // Debugging
    return token;
  },
  async session({ session, token }) {
    Object.assign(session.user, token.user ?? {});

    if (token.exp) {
      session.expires = new Date(token.exp * 1000).toISOString();
    }

    console.log('Session Callback - Session Expires:', session.expires); // Debugging
    return session;
  },
}

What I’ve tried: Logging token.exp in both jwt and session callbacks (it seems correctly assigned).

Ensuring token.user.exp exists before assigning it to token.exp.

Clearing cookies and trying a fresh login.

Expected Behavior: session.expires should be updated correctly using the exp value from the token.

Actual Behavior: session.expires does not reflect the expected expiration time, and remains unchanged or incorrect.

Manually setting maxAge in session: { maxAge: 60 * 60 }, which works, but I want it to be dynamic based on the token’s exp.

Expected Behavior: session.expires should be updated correctly using the exp value from the token.

Actual Behavior: session.expires does not reflect the expected expiration time, and remains unchanged or incorrect.

How can I ensure that session.expires correctly updates from token.exp dynamically?

本文标签: authenticationNextAuthjs sessionexpires not updating correctly from JWT expStack Overflow