Search code examples
reactjsnext.jsgoogle-signinnext-auth

how to get the provider access token in next-auth


Hi I am new to nextjs and for authentication I am using next-auth. The users can log in using Google and when they are logged in and authenticated I want to get the access_token given by provider in this case Google. Although I can get that inside signIn callback function but is there a way to globally access that?


Solution

  • you can create the callbacks key in the nextAuth config as below to access your userId and access_token, as well.

    callbacks: {
        async session({ session, token, user }) {
          session.user.id = token.id;
          session.accessToken = token.accessToken;
          return session;
        },
        async jwt({ token, user, account, profile, isNewUser }) {
          if (user) {
            token.id = user.id;
          }
          if (account) {
            token.accessToken = account.access_token;
          }
          return token;
        },
      },