What is a correct way to refresh access tokens #4592
Replies: 2 comments 31 replies
-
Here's some pseudocode that should help. The "trick" is to verify the token, and if it's expired, get a new token and redirect back to the same page. You should now have the correct token. export const loader = async ({request}) => {
const token = await verifyToken(request)
// the token will be valid here because it either hasn't expired
// or we refreshed the token and threw a redirect with new token
//... do what you need to do with the token
}
async function verifyToken(request: Request) {
let token = await getToken(request)
if (isExpired(token)) {
token = await refreshToken(request)
// token is now valid, so redirect back to same page
// and Remix will resend the request with the new token
// we throw here so we don't have to check return in loader
throw redirect(request.url, { headers: { 'set-coookie': tokenCookie }})
}
// token should be valid here, so return it
return token
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help, @kiliman! Assuming I have nested routes and in the loaders of each route I need to make an authenticated request to my API (and therefore a valid token), should I call the verifyToken() function in each of them or only in the root? UPDATE: If I place this only in root's loader than the verifyToken gets called only on initial page load, but not when navigating from page to page. If I place it in loaders of other routes then this function gets called multiple times on initial route which eventually leads race condition and multiple token refreshes. That's exactly what my issue is. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am new to remix, and so far I love this framework.
However I've been struggling with one issue for a few days and I am now stuck. I have Directus as my headless CMS and authentication service for my users. The way auth works is you get an access token and a refresh token. The refresh token is used to get a new access token when the old one expires. The problem is that I don't know where to put my access token refresh code.
If I put it in the loader of
root.tsx
it works fine for the initial load, but then if I start navigating from one route to another and a token refresh is needed at some point, the code inroot.tsx
doesn't fire. So I end up with failed requests in my loaders. If I place that code in every route that requires authentication, then I have code racing on initial load when there are nested routes that have the token refresh code in their loaders. I cannot have the token refresh only in the leaf routes, as I have parent routes that also need the user object, for example to render a user menu in the page navigation.I should say that I user Remix Server and would prefer not switching to Express unless I absolutely have to.
I don't know how to proceed and this issue is just driving me crazy. Have anyone else had to deal with this problem? I would appreciate any advice.
Beta Was this translation helpful? Give feedback.
All reactions