-
Just curious if there were any plans to implement an official JWT token refresh strategy. I performed this right before we pushed an alpha but essentially I reduced token expiry from 4w to 1h, added the refresh token, and the logic in the Apollo client to refresh the token if a request was made w/ it expired & session is still valid. Summarizing and missing some steps here but you get the idea. It seems this is about the only thing missing from taking this boilerplate from an MVP accelerator to an almost production ready masterpiece. On a side note, migrating from lerna to Turborepo has been a lifesaver. Nothing super important, just spurring discussion |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Would be super interested to see how you implemented the refresh token as it's something we've been looking into recently. The most important aspect is that the refresh token needs to be stored in an httpOnly cookie so that it's not accessible via JS. The point of the refresh token (in our view) is so that if an attacker gets hold of the access token, it's only valid for a short period. We've seen a few scenarios where the refresh token is stored alongside the access token and this just makes no sense. In the meantime, I've updated the boilerplate to store the current access token in an httpOnly cookie (protected against XSS) and we are using next's api routes to proxy the backend server. This allows us to set an httpOnly cookie on login/register and forward all Apollo requests through the next.js api route. In there we access the token and pass it in the headers to the backend server. This also allows the react native app to continue using regular jwt auth. |
Beta Was this translation helpful? Give feedback.
-
Okay, I just realized I would need to learn a bit about react native to actually make a PR to implement this in the I skimmed the code in So I'll just post how I went about it here to start and get feedback. Note: I skipped some error handling/best practices for brevity sake Generate the Refresh/Access token on the API.The commit currently in dev has the access token being generated; so I'll just note this since sending a refresh token with a longer expiry time is trivial from there. Just send the signed refresh token whenever the access token is sent. Create
|
Beta Was this translation helpful? Give feedback.
-
@Rykuno I've pushed a commit to develop for refresh tokens, have a look and let me know what you think! At the moment I've set the app token to expire in 1 minute and the refresh for 4 weeks. However, something I realised when building it: the whole refresh token flow will only run when a mutation or a query throws an Unauthenticated error. So there's potentially a weird behaviour that occurs if a user logs in and then just sits on the homepage and refreshes the page, the app token would expire after 1 minute and the user will be logged out, because it never threw a Unauthenticated error, because they never hit "authenticated" queries/mutations. This is obviously a bit of a weird edge case as you would usually have authenticated routes that would be hit by the user as they use the site, but it could happen. I'm trying to think of some solutions at the moment, one being just to render a component at the root that sets an interval to refresh the token. This is currently commented out in the _app.tsx file if you wana have a play. The other one would be to return app & refresh tokens also from the Me query, but this doesn't account for all the other potentially unauthenticated queries/mutations. What do you think? |
Beta Was this translation helpful? Give feedback.
@Rykuno I've pushed a commit to develop for refresh tokens, have a look and let me know what you think!
At the moment I've set the app token to expire in 1 minute and the refresh for 4 weeks.
However, something I realised when building it: the whole refresh token flow will only run when a mutation or a query throws an Unauthenticated error. So there's potentially a weird behaviour that occurs if a user logs in and then just sits on the homepage and refreshes the page, the app token would expire after 1 minute and the user will be logged out, because it never threw a Unauthenticated error, because they never hit "authenticated" queries/mutations.
This is obviously a bit of a weird edge cas…