Dawid
Issue with JWT auth in Nuxt 3 SSR
Hello everyone!
I'm experiencing an issue with JWT authentication in my Nuxt 3 SSR application. The problem occurs when trying to access protected routes directly (by entering the URL) rather than navigating through the app.
Current setup:
- Using middleware to check if a user is logged in and redirect to login if necessary
- Access token is stored in an auth store (Pinia)
- Refresh token is stored in an HttpOnly cookie
- Auth middleware checks token validity and refreshes if needed (sends a request to refresh token endpoint with refresh token cookie)
The issue:
When navigating within the app using Nuxt Link, everything works as expected. If the access token is invalid, a request is sent to refresh it, and I can access protected routes normally.
However, when I try to access a protected route directly by entering its URL, the middleware seems to run on the server-side without access to the HttpOnly cookie containing the refresh token. As a result, the refresh request fails, and I'm unable to access the protected route.
Could you please help me what's the best way to fix it?
middleware/auth.ts:
STORE/auth.ts
5 replies