最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

authentication - NextAuth.js session.expires not updating correctly from JWT exp - Stack Overflow

programmeradmin1浏览0评论
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?

发布评论

评论列表(0)

  1. 暂无评论