Does NextAuth refresh the access token automatically?
Unanswered
Aland posted this in #help-forum
AlandOP
As the title says I was wondering about refreshing the access token, Does NextAuth does that automatically?
Or as the docs say, you have to implement it manually?
Or as the docs say, you have to implement it manually?
// https://accounts.google.com/.well-known/openid-configuration
// We need the `token_endpoint`.
const response = await fetch("https://oauth2.googleapis.com/token", {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: process.env.GOOGLE_ID,
client_secret: process.env.GOOGLE_SECRET,
grant_type: "refresh_token",
refresh_token: token.refresh_token,
}),
method: "POST",
})
const tokens: TokenSet = await response.json()
if (!response.ok) throw tokens
return {
...token, // Keep the previous token properties
access_token: tokens.access_token,
expires_at: Math.floor(Date.now() / 1000 + tokens.expires_in),
// Fall back to old refresh token, but note that
// many providers may only allow using a refresh token once.
refresh_token: tokens.refresh_token ?? token.refresh_token,
}2 Replies
Rainbow trout
You have to do this manually
Carolina Dog
I tried this https://authjs.dev/guides/basics/refresh-token-rotation
But somehow it doesn't work in production (Works on local run and build). It says the error is invalid grant type.
But somehow it doesn't work in production (Works on local run and build). It says the error is invalid grant type.