Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 994
Mahdi-Jafaree Avatar asked Sep 13 '25 07:09

Mahdi-Jafaree


1 Answers

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;
    },
  },
like image 68
Saheb Mohammadi Avatar answered Sep 16 '25 02:09

Saheb Mohammadi