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.
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.