App Dir: Refresh Token Strategy #53704
Replies: 2 comments
-
|
This needs serious attention. In my case, I am developing my storefront using Next.js app router and using Wordpress/Woocommerce via GraphQL in the backend. For authentication, the WPGraphQL follows the short-lived token strategy and refreshes them using refresh-tokens and also maintains session using a different token (responsible for synchronizing cart with server-client). Really didn't think that this will be a challenge as other frameworks manage token rotation seamlessly. Here is how I have implemented it obviously not-so-good solution: Used Apollo client: Requires two instances i.e. one for client and other one for server: src\lib\apollo.client.ts:src\lib\apollo.server.ts:And here is the route handler for actually refreshing the token with the help of an external redis service: This works but with lot of random issues. PS: It does require a refactor |
Beta Was this translation helpful? Give feedback.
-
|
Here is the better and simple solution to implement this : https://medium.com/@sundargautam2022/implementing-refresh-token-with-nextjs-15-using-app-router-with-cross-api-different-api-5682f83f9802 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What is the correct way in Next 13 to handle token rotations? There is absolutely nothing about utilizing external API's for auth which seems like a relatively popular thing to do.
Token refreshing should be easy. I want to simply automatically rotate a short lived access-token via burning a refresh-token. I've done this in many web frameworks including Next 12 but I've never had this large of an issue on such a rudimentary issue.
99% of our mutations and queries are done server side.
We have the following code to
1) Make a request
2) Add auth header if access token exists
3) Check for a 401 and refresh tokens if needed
4) Remake request(not present in this snippit)
Problem being the /api/refresh route is not setting the cookie I presume because it is made server side instead of client. But since the request is run server side; we're a bit confused on the best practice to enforce token rotations in NextJS 13.
Beta Was this translation helpful? Give feedback.
All reactions