N
Nuxt8mo ago
Eric

SSR and cookies

How do cookies work with SSR ?
1 - user tyes website.com in the browser 2 - the browser sends a cookie along 3 - the nuxt app receives the request and retreives the access_token from the cookie 4 - te nuxt app makes a request to the directus server 5 - the directus server sends back a response containing data and maybe a set-cookie rule 6 - the nuxt app renders the page and sends it to the browser with the set-cookie rule 7 - the browser shows the page and creates a new cookie does that make sence ? I'm trying to figure out how to authenticate to directus in a nuxt app.
5 Replies
ninjaparade
ninjaparade8mo ago
I am trying to work through this myself as well, currently I am using the parseCookies composable on a global server middleware and setting that cookie value that I need for the auth (api token) to the event.context.${yourObject} as shown here https://nuxt.com/docs/guide/directory-structure/server#server-middleware
Nuxt
server/ · Nuxt Directory Structure
The server/ directory is used to register API and server handlers to your application.
ninjaparade
ninjaparade8mo ago
my current experience is that it seems that locally with HMR running, the first load the SSR happens more than 1 times, the first few return as though there is no cookie or value, but then the last one does. are you experiencing something similar?
Eric
EricOP8mo ago
I'm still trying to figure out how to read cookies on the server.
parseCookies ? is that a nuxt composable ? the docs say that useCookie is an "an SSR-friendly composable to read and write cookies."To me, that sounds like I can use it server side. But I also feel like server side, we need to read the request hearders to find the cookies... Could we use useRequesHeaders to get the Cookie headers from the request ?
ninjaparade
ninjaparade8mo ago
I’m a bit confused I linked you how to do this above. There is also nuxt auth utils what issue are you trying to solve that isn’t covered in the above link?
Kyllian
Kyllian8mo ago
i just use const cookie = getCookie(event, 'Token'); for middleware cookies, should work server-sided too
Want results from more Discord servers?
Add your server