You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importNextAuth,{typeUser}from"next-auth"importGooglefrom"next-auth/providers/google"exportconst{ handlers, auth }=NextAuth({providers: [Google({// Google requires "offline" access_type to provide a refresh_tokenauthorization: {params: {access_type: "offline",prompt: "consent"}},}),],callbacks: {asyncjwt({ token, account }){if(account){// First-time login, save the access_token, its expiry and the refresh_tokenreturn{
...token,access_token: account.access_token,expires_at: account.expires_at,refresh_token: account.refresh_token,}}elseif(Date.now()<token.expires_at*1000){// Subsequent logins, but the access_token is still validreturntoken}else{// Subsequent logins, but the access_token has expired, try to refresh itif(!token.refresh_token)thrownewTypeError("Missing refresh_token")try{constresponse=awaitfetch("https://oauth2.googleapis.com/token",{method: "POST",body: newURLSearchParams({client_id: process.env.AUTH_GOOGLE_ID!,client_secret: process.env.AUTH_GOOGLE_SECRET!,grant_type: "refresh_token",refresh_token: token.refresh_token!,}),})consttokensOrError=awaitresponse.json()if(!response.ok)throwtokensOrErrorconstnewTokens=tokensOrErroras{access_token: stringexpires_in: numberrefresh_token?: string}token.access_token=newTokens.access_tokentoken.expires_at=Math.floor(Date.now()/1000+newTokens.expires_in)if(newTokens.refresh_token)token.refresh_token=newTokens.refresh_tokenreturntoken}catch(error){console.error("Error refreshing access_token",error)token.error="RefreshTokenError"returntoken}}},},session: ({ session, token })=>{session.error=token.errorreturnsession},})declare module "next-auth"{interfaceSession{error?: "RefreshTokenError"}}declare module "next-auth/jwt"{interfaceJWT{access_token: stringexpires_at: numberrefresh_token?: stringerror?: "RefreshTokenError"}}
Maybe session should be in callbacks ?
session: ({ session, token }) => {
session.error = token.error
return session
},
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Maybe session should be in callbacks ?
session: ({ session, token }) => {
session.error = token.error
return session
},
Beta Was this translation helpful? Give feedback.
All reactions