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

javascript - NEXT Auth v5 Server side session update - Stack Overflow

programmeradmin0浏览0评论

I'm trying to update a user session via NextAuth.js v5 using JWT strategy.

My end goal is switch to another active company in server side. As I use their unstable_update function and it actually triggers my JWT callback.

    async jwt({ token, user, trigger, session }) {
      if (user) {
        const userId = token.sub as string;
        const user = await prisma.user.findUnique({
          where: { id: +userId },
        });
        token.role = user?.role as Role;
        token.username = user?.username as string;
        token.firstName = user?.firstName as string;
        token.lastName = user?.lastName as string;
        token.activeCompanyId = user?.activeCompanyId;
      }
      if (trigger === 'update') {
        console.log('Update triggered, session:');
        console.log(session);
        token.activeCompanyId = session.user.activeCompanyId;
      }
      return token;
    },
    async session({ token, session }: { token: JWT; session: Session }) {
      console.log('Token in session callback');
      console.log(token);
      const userId = token.sub as string;
      session.user.userId = +userId;
      session.user.role = token.role as Role;
      session.user.firstName = token.firstName;
      session.user.lastName = token.lastName;
      session.user.username = token.username;
      session.user.activeCompanyId = token.activeCompanyId;
      return session;
    },
  }

After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId is null. Am I doing it correctly?

I'm trying to update a user session via NextAuth.js v5 using JWT strategy.

My end goal is switch to another active company in server side. As I use their unstable_update function and it actually triggers my JWT callback.

    async jwt({ token, user, trigger, session }) {
      if (user) {
        const userId = token.sub as string;
        const user = await prisma.user.findUnique({
          where: { id: +userId },
        });
        token.role = user?.role as Role;
        token.username = user?.username as string;
        token.firstName = user?.firstName as string;
        token.lastName = user?.lastName as string;
        token.activeCompanyId = user?.activeCompanyId;
      }
      if (trigger === 'update') {
        console.log('Update triggered, session:');
        console.log(session);
        token.activeCompanyId = session.user.activeCompanyId;
      }
      return token;
    },
    async session({ token, session }: { token: JWT; session: Session }) {
      console.log('Token in session callback');
      console.log(token);
      const userId = token.sub as string;
      session.user.userId = +userId;
      session.user.role = token.role as Role;
      session.user.firstName = token.firstName;
      session.user.lastName = token.lastName;
      session.user.username = token.username;
      session.user.activeCompanyId = token.activeCompanyId;
      return session;
    },
  }

After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId is null. Am I doing it correctly?

Share Improve this question asked Nov 19, 2024 at 14:19 justelio19justelio19 1231 gold badge1 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As the documentation says: The arguments user, account, profile and isNewUser are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only token will be available.

So, you should only assign token.activeCompanyId if session != null.

I'ld expect that the session argument received in the session callback still has the user.activeCompanyId value, so I don't think you need to parse it from token at all.

发布评论

评论列表(0)

  1. 暂无评论