next auth. how to get google id_token

user510841

In short: How to get google id token from nextauth?

Details: I do authorization through Google and after that I have to verify the response from Google on the server, after which the server will return its access token to me. A friend who builds backend tells me that he needs an id_token and he will check it with google-auth. No matter how I try to set up the callback, I can't get the id_token. I only get the session token in cookie and nothing else.

If it is possible to do this without id_token, please write that as well.

ninsau

Here's a snippet of how to achieve that. You have to fetch the account details returned from google using the callback:

 callbacks: {
    async jwt({ token, user, account }) {
      // Persist the OAuth access_token to the token right after signin
    
      if (account) {
        token.id_token = account.id_token;
      }
      return token;
    },
    async session({ session, token }) {
      // Send properties to the client, like an access_token from a provider.
      session.id_token = token.id_token;
      return session;
    },
  },

So now, you can retrieve the token by doing something like:

  const { data: session } = useSession();
  console.log(session.id_token)

So basically, what we just did was access the id_token provided by google. We accessed it from account and now we are passing to the session object so we can use in the client side of our application.

PS> Check your terminal during authentication with google. You'll see a log of the profile object and account object inside the terminal.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Examples of how to get ID_token from Google OpenID Connect

Auth0 Android - How to renew id_token?

Is it possible to get profile information in an id_token from Google?

Google OpenID Connect: How to verify id_token?

How to get public certificate for id_token in openam?

How to get the next page token from google cloud logging API

How to "sign in with Google" (id_token) *and* receive a code/access_token for specific scope (for the backend)

How get in Azure access_token from Azure Function app with id_token?

Can I get a consistent 'iss' value for a Google OpenIDConnect id_token?

Without using auth middleware, how to get user id by token?

Gmail API / Google Auth - How to get verified?

How can I get id_token in identity server, before redirecting to client?

How do I get the id_token in Blazor WebAssembly after authenticating with OpenId (Cognito)?

How to get session data in next-auth after loading is done

How to get next id for img

Owin auth - how to get IP address of client requesting the auth token

Golang and gcloud API: how to get an auth token

How to get the token from firebase_auth

How to get auth token programatically in GCP

Getting an id_token using Auth0 in Expo app

Finding correct Firebase Auth id_token on $onAuthStateChanged

How to get authorized email Id after getting authorized from google auth2 new version

Get Google Refresh token from existing user auth code

Google auth2 .getCurrentUser() get the user token?

Connecting to Google APIs using next-auth

How to get google user profile on auth state changed

get auth/argument-error with next-firebase-auth

How to get the IID_TOKEN for Google Instance ID service?

How do I get Google ID Token on postman?