Next.js Discord

Discord Forum

How can I redirect when JWT refresh fails

Unanswered
Alligator mississippiensis posted this in #help-forum
Open in Discord
Alligator mississippiensisOP
I have a successful workflow with NextJS (14.0.3) and Next-Auth (4.24.5) with a custom backend which provides an access_token (short lived) and a refresh_token long lived.

The refreshing of the access_token works fine, but what should I do if the refresh_token has expired or is invalid. How can I redirect to the login to enforce the user to re authenticate?

Thanks for help.

14 Replies

you can use the middleware, to check the jwt and redirect if it fails ^^ @Alligator mississippiensis
@B33fb0n3 you can use the middleware, to check the jwt and redirect if it fails ^^ <@285200847263236097>
Alligator mississippiensisOP
Do you have a good example? I'm using also the next-intl Middleware, so I need both working.
Alligator mississippiensisOP
Ok, the middlewares are running, but how can I check in the if something was wrong during refresh? The refresh happens in NextAuthOptions jwt callback.
@Alligator mississippiensis Ok, the middlewares are running, but how can I check in the if something was wrong during refresh? The refresh happens in `NextAuthOptions` jwt callback.
the middleware will be called every route change. So you can check the auth every route change inside your middleware
@B33fb0n3 the middleware will be called every route change. So you can check the auth every route change inside your middleware
Alligator mississippiensisOP
Yes, but how can I check? Do I have to do another refresh request to my backend, in the middleware to react on refresh failure?
@Alligator mississippiensis Yes, but how can I check? Do I have to do another `refresh` request to my backend, in the middleware to react on refresh failure?
you don't need to. The user request the new page (will be redirected or whatever), then the middleware will be executed (jwt check: valid or invalid) and then the page.js will be called. Inside the page.js you don't need to check the jwt again
Alligator mississippiensisOP
I will continue tomorrow, but still have no idea how 😄
@Alligator mississippiensis I will continue tomorrow, but still have no idea how 😄
take a look at this. Your middleware will be executed everytime a route change happends. And everytime BEFORE the page.js call. So the user isn't able to see the page.js when you protect it by the middleware. If the middleware say "no you can go there, because you are not authenticated, I will take care of that and redirect you to the login page." And the route is protected 👍
Alligator mississippiensisOP
My Routes are already protected, this works fine. The only Problem is, whenever I try to refresh my JWT access_token with my custom backend (not NextJS) and this call fails I want to redirect to login.
@B33fb0n3 oh ok, then I misunderstood it. How and where do you call this? > custom backend (not NextJS) and this call fails
Alligator mississippiensisOP
I have a rout handler which uses NextAuth with an NextAuthOptions object and there is a callback jwt here I check if the access_token is expired. And if so, then I do a refresh request to my custom backend, but if this request fails (for any reason, e.g. if the refresh_token is also expired) then I want to handle this situation and redirect to the login, so the user can re-authenticate itself and get a new access_token and a new refresh_token.
then I can't help you that much, sorry
@B33fb0n3 then I can't help you that much, sorry
Alligator mississippiensisOP
Ok, thanks anyway.