Getting updated user data from NextJS App Router Middleware.
How can i get updated user data from middleware. i tried refreshing the token before getting user data with . My issue is that i tried deleting the user from the kinde ui and refreshed my nextjs app to see if i would get redirected to "/" because there is no user, but the user was still autheticated.
2 Replies
Forget it i moved to lucia auth
Sorry to hear that you wanted to move quickly.
For anyone else coming across this issue, here some tips to help.
To get updated user data from middleware in your Next.js app, you need to ensure that the tokens are refreshed and the session is updated correctly. Here is a step-by-step guide to achieve this:
Refresh Tokens: Use the refreshTokens helper function to refresh the tokens in your session.
Get Updated User Data: After refreshing the tokens, use the getUser function to get the updated user data.
Here is an example of how you can implement this in your middleware:
In this example:
The refreshTokens function is called to refresh the tokens.
The getUser function is then called to get the updated user data.
If the user data is not available or the user is not authenticated, a 401 Unauthorized response is returned, which can be used to redirect the user to the home page or login page.
Make sure you have enabled the application's access to the Kinde Management API in your Kinde settings.
For more details, you can refer to the NextJS App Router SDK documentation.