Handling Cookies in API Routes
I have global middleware:
And those two routes in
server/api/getCookie.ts
file:
and server/api/setCookie.ts
:
I was refering to this section in docs https://nuxt.com/docs/api/composables/use-cookie#handling-cookies-in-api-routes
When I run this app I expect to create cookie and console.log
it but it doesn't save cookie even on multiple reloads of the app (screenshot). Why?43 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
What do you mean with parseCookies?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
wow this works
i'll play with it
thanks
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
No sorry actually it doesn't, I changed getCookies.ts to
and now it's:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it's still same:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
setCookie.ts is:
the reproduction is here https://github.com/d0peCode/nuxt3-pinia-middleware-issue on
api-route-handling-cookies
branch
https://github.com/d0peCode/nuxt3-pinia-middleware-issue/tree/api-route-handling-cookiesUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
sure thank you for helping me!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it does create the cookie on logout button click due to redirect
but it doesnt when you just load the app
without this button
disregard this button
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
no no
in middleware i execute this routes
it should set the cookie immiedietly when you run app in browser
you know what i mean?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
ignore this button i want to set cookie from api route which is executed from middleware
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it's not used at all
pull now, i clean up this reproduction project
it's only about this 3 files
in auth.global.ts i execute this:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
i want to be able to set cookie from middleware by the route api so that i can create httpOnly cookie and remove it on server side
that's why i'm using api routes for creating cookies
and it's in docs so i thought it's gonna work
you can not remove httpOnly cookie from client side javascript, it has to be removed from the server where it was created. I wanted to create few API routes with nuxt and handle my httpOnly cookies in this way
i was suspecting that the issue is that i'm setting and getting the cookie in same ssr cycle but then after page reload the cookie is still not created
any hope?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
so you propose plugin instead of global middleware
lemme check 1sec
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
does it really work for you?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
i pushed changes
for me it doesnt create the cookie
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
remove it and reload
does it create?
everytime?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
do
git pull
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
you dont need to do it in nuxt3
Nuxt automatically reads the files in your plugins directory and loads them at the creation of the Vue application.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
and mode client
I need to set those cookies in server side
that's the thing, I want to set cookies from server side either middleware or plugin but from server
can you use the ~/server/middleware dir for that?
yes that works!
thank you!