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
This is fine as a base, but in practice you really need more to be able to use this. For instance, logging out does not end your session from your auth server and you need to update it to something more like this:
exportconstauthOptions: NextAuthOptions={// https://next-auth.js.org/configuration/providers/oauthproviders: [{id: "myKey",name: "Paul's Key",type: "oauth",wellKnown: "http://localhost:3255/auth/realms/DevRealm/.well-known/openid-configuration",authorization: {params: {scope: "openid email profile recipe_management"}},idToken: true,checks: ["pkce","state"],clientId: "recipe_management.next",clientSecret: "974d6f71-d41b-4601-9a7a-a33081f82188",profile(profile){return{id: profile.sub,name: profile.name,email: profile.email,image: profile.picture,}},}],events: {asyncsignOut({token}){varrefreshToken=token.refreshTokenletheaders={"Content-Type": "application/x-www-form-urlencoded"};try{awaitaxios.post("http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/logout",querystring.stringify({refresh_token: refreshToken,client_secret: 'myguid',client_id: 'recipe_management.next',}),{ headers },);}catch(e){}},},theme: {colorScheme: "light",},callbacks: {asyncjwt({ token, user, account }){// Initial sign inif(account&&user){return{accessToken: account.access_token,accessTokenExpires: Date.now()+account.expires_at*1000,refreshToken: account.refresh_token,
user,}}// Return previous token if the access token has not expired yetif(Date.now()<token.accessTokenExpires){returntoken}// Access token has expired, try to update itreturnrefreshAccessToken(token)},asyncsession({ session, token }){session.user=token.usersession.accessToken=token.accessTokensession.error=token.errorreturnsession},},}asyncfunctionrefreshAccessToken(token){try{consturl="http://localhost:3255/auth/realms/DevRealm/protocol/openid-connect/token?"+newURLSearchParams({client_secret: 'myguid',client_id: 'recipe_management.next',grant_type: "refresh_token",refresh_token: token.refreshToken,})constresponse=awaitfetch(url,{headers: {"Content-Type": "application/x-www-form-urlencoded",},method: "POST",})constrefreshedTokens=awaitresponse.json()if(!response.ok){throwrefreshedTokens}return{
...token,accessToken: refreshedTokens.access_token,accessTokenExpires: Date.now()+refreshedTokens.expires_at*1000,refreshToken: refreshedTokens.refresh_token??token.refreshToken,// Fall back to old refresh token}}catch(error){console.log(error)return{
...token,error: "RefreshAccessTokenError",}}}
I would think when using a well known provider, you could just use it to get the end_session_endpoint and call that automatically at signout without all this boilerplate.
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.
-
Currently, when setting up next-auth with OIDC with a code flow using PKCE, I can to do something like this:
This is fine as a base, but in practice you really need more to be able to use this. For instance, logging out does not end your session from your auth server and you need to update it to something more like this:
I would think when using a well known provider, you could just use it to get the
end_session_endpoint
and call that automatically at signout without all this boilerplate.Ex well known:
Beta Was this translation helpful? Give feedback.
All reactions