Next Auth: how to redirect a user to "/" if the token refresh is not successful?
Answered
Ocicat posted this in #help-forum
OcicatOP
In the jwt callback, if there's an error during the refresh token process, I'm returning the token with the error property "RefreshAccessTokenError". The error looks like this:
How can I redirect the user to the login page which is "/" if I'm getting that error? So the user can log in again and get a new access token.
Error refreshing access token { error: 'invalid_grant', error_description: 'Refresh token revoked' }How can I redirect the user to the login page which is "/" if I'm getting that error? So the user can log in again and get a new access token.
async jwt({ token, account, user }) {
if (account) {
console.log("New authentication, creating new token");
return {
...token,
access_token: account.access_token,
expires_at: account.expires_at,
refresh_token: account.refresh_token,
user,
};
}
if (token.expires_at && Date.now() < token.expires_at) {
console.log("Token still valid, returning existing token");
return token;
}
console.log("Token expired, trying to refresh");
if (!token.refresh_token) {
throw new Error("Missing refresh token");
}
try {
const refreshedToken = await refreshToken(token);
if (!refreshedToken) {
throw new Error("Failed to refresh token");
}
console.log("Token refreshed successfully, returning updated token");
return refreshedToken;
} catch (error) {
console.error("Error refreshing access token", error);
return { ...token, error: "RefreshAccessTokenError" as const };
}
},
session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
session.access_token = token.access_token as string;
}
return session;
},
},
});20 Replies
umm, check if error == invalid_grant:
redirect("/")
redirect("/")
Answer
import { redirect } from "next/headers"
@Anay-208 umm, check if error == invalid_grant:
redirect("/")
OcicatOP
simply as that? Can I do that directly in the jwt callback?
@Ocicat simply as that? Can I do that directly in the jwt callback?
you can return the output to the route handler
you can try it out in jwt
If it doesn't you can pass the error to the route handler which can redirect
@Anay-208 you can try it out in jwt
I believe it should work
@Anay-208 I believe it should work
OcicatOP
so basically like this?
try {
const refreshedToken = await refreshToken(token);
if (!refreshedToken) {
throw new Error("Failed to refresh token");
}
console.log("Token refreshed successfully, returning updated token");
return refreshedToken;
} catch (error) {
console.error("Error refreshing access token", error);
if (error.error === 'invalid_grant' && error.error_description === 'Refresh token revoked') {
redirect('/');
}
throw error;
}
},Yes, try this
OcicatOP
okok, I'll try it!
@Anay-208 import { redirect } from "next/headers"
OcicatOP
are you sure redirect is from "next/headers"? Can't find it...
I’m not sure, could be from next/navigation
Google it
@Ocicat resolved?
@Anay-208 <@327741242505166848> resolved?
OcicatOP
yep, sorry I forgot to mark the solution. It should work now
thank you!
@Anay-208 umm, check if error == invalid_grant:
redirect("/")
Mark this message as solution