Missing cookies in request from server
Hey I am trying to make ofetch call from the middleware to preload data into the store. Problem is that the call should include also cookies with auth information but as i am trying to console.log headers of this request made on server, there are no headers at all
19 Replies
When I was trying to do same on the client it was working but I want to utilize SSR
Show some codes
Branislav Hozza
StackBlitz
Nuxt - cookies issue - StackBlitz
Create a new Nuxt project, module, layer or start from a theme with our collection of starters.
did you use useRequestHeaders ?
on ssr context and server middleware you could write header back to a request(event) using setHeader
import { setHeader } from "h3";
setHeader(useRequestEvent()!, "set-cookie", cookies);
Hm interesting I will try and let you know 🙂
use useRequestHeaders to get access to request cookies
there is also setResponseHeaders
I am getting this error:
I am unable to use this function in server context
can i see the code
by server context u mean the server middleware ?
try this appraoch inside event handlers
In
server/middlewares/auth.ts
i added this snippet:
oh ye these composables wont work inside the server handlers
to get cookies using h3
there is another way to actually write cookies back
something like
your request is in event object you should be able to see it if you log
Look so this is the issue i am trying to explain, when I am sending initial request to page cookies exist (you can see the first one), but then server tries to fetch data from other API and cookies are missing
Data fetching runs in middleware
as far as i know subsequent requests are isolated you cannot alter with their headers i guess there was a issue for this
for example
second call will run with the old cookies
https://github.com/nuxt/nuxt/issues/13096#issuecomment-1397311005
I tried reading cookie header in middleware and its there so probably i just somehow need to proxy these cookies into requests that i am sending later
you are trying to refresh an access token right ?
I fixed the issue by using middleware combined with a timestamp to track the JWT token's expiry date. In my case, the token was set to expire after 20 minutes. In the middleware, I check if the token has expired, and if so, I handle the refresh, pass the updated cookies, and continue with the request. also have a custom useAuth composable with a useState inside to keep track of cookie expiry on the client side. Additionally, I use a Redis instance on the backend to cache both the JWT and the refresh token for one minute. This ensures that if any subsequent requests fail to get the cookie from the frontend, I can return it directly from the cache. instead of generating new one ( in my case i revoke previous refresh tokens )
simplfied version
No I am trying to get data into pinia store in ssr context. But i am missing auth token in cookies that are sent from nuxt server to my external server with data
But as I am trying to explain, my cookies are lost in communication between nuxt server and external server.
but this might work let me check
GitHub
pass on cookies from server-side API calls on SSR response · Issue ...
Environment Operating System: Linux Node Version: v16.13.1 Nuxt Version: 3.0.0-27333874.279bfdc Package Manager: [email protected] Bundler: Vite User Config: - Runtime Modules: - Build Modules: - Reprod...
Okay, I finally have tokens on server but now I am not getting redirected to authorization page... its just ignoring 302 replies