How to generate a new token on every site revisit?
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
Hi there. Does anyone know how to generate a new JWT on every site revisit? If the browser is refreshed or if it is closed and opened to revisit my site, I wanted a new JWT. My idea is to use my root layout (
At the moment, that's not the case. I'm not trying to create a login/logout authentication system. I simply want a new JWT every time someone revisits my site.
I'd be modifying the JWT server side and with middleware checks as the JWT would be used in my API route.
Code is attached.
layout.js) to accomplish this because it would be remounted on browser refresh or site revisit. At the moment, that's not the case. I'm not trying to create a login/logout authentication system. I simply want a new JWT every time someone revisits my site.
I'd be modifying the JWT server side and with middleware checks as the JWT would be used in my API route.
Code is attached.
7 Replies
@Asiatic Lion Hi there. **Does anyone know how to generate a new JWT on every site revisit?** If the browser is refreshed or if it is closed and opened to revisit my site, I wanted a new JWT. My idea is to use my root layout (`layout.js`) to accomplish this because it would be remounted on browser refresh or site revisit.
At the moment, that's not the case. I'm not trying to create a login/logout authentication system. **I simply want a new JWT every time someone revisits my site. **
I'd be modifying the JWT server side and with middleware checks as the JWT would be used in my API route.
Code is attached.
idt you can set cookies in server components, but you can set cookies on page load with middleware
so try middleware
@joulev idt you can set cookies in server components, but you can set cookies on page load with middleware
Asiatic LionOP
Thanks @joulev for following up. How can I set the cookies on page load? The trouble I had was that when navigating to a new page using my navbar (uses <Link>Contact Page</Link>), the middleware would capture that request and generate a new JWT.
I resorted to using Next-Auth but am struggling to have it set a new JWT on a new page load (via full browser refresh or site revisit).
I resorted to using Next-Auth but am struggling to have it set a new JWT on a new page load (via full browser refresh or site revisit).
@Asiatic Lion Thanks <@484037068239142956> for following up. How can I set the cookies on page load? The trouble I had was that when navigating to a new page using my navbar (uses <Link>Contact Page</Link>), the middleware would capture that request and generate a new JWT.
I resorted to using Next-Auth but am struggling to have it set a new JWT on a new page load (via full browser refresh or site revisit).
iirc middleware doesnt capture client side navigation, have you tried?
i had a lot of issues with this middleware "feature" but it seems your use case is the reason why they do it that way
European sprat
once the pages are loaded, they are subject to the (unchangeable at present) 30 second Router Cache
something you could do is have a route handler endpoint that is automatically checked on each page load which then also regenerates a jwt. all route handler requests go through middleware
something you could do is have a route handler endpoint that is automatically checked on each page load which then also regenerates a jwt. all route handler requests go through middleware
5 min router cache on static pages