N
Nuxt7mo ago
Nik ⚡

Getting access to cookies in middleware

Hi am building a very simple auth in Nuxt3. I am setting session cookies with jwt tokens in my login.post.ts endpoint. For the protected pages I am using a middleware, but I am struggling to get access to the cookies in the middleware. In the login endpoint I am setting the cookie with the h3 setCookie function. Can you please help and let me know how I access the cookie in the middleware now so I can validate the JWT? Thanks.
30 Replies
Likonima
Likonima7mo ago
have you tried useCookie ?
Nik ⚡
Nik ⚡OP7mo ago
useCookie to access the cookie only, or both to set and read?
Likonima
Likonima7mo ago
you asked for reading
Nik ⚡
Nik ⚡OP7mo ago
I have tried useCookie, but it is not working, even thought the cookie is set, see screenshot.
No description
Likonima
Likonima7mo ago
it's a ref. needs .value
Nik ⚡
Nik ⚡OP7mo ago
Well well, thanks very much. Totally missed that. It's getting the value now.
No description
Nik ⚡
Nik ⚡OP7mo ago
Thank you very much @Likonima
Nik ⚡
Nik ⚡OP7mo ago
@Likonima I have one more question please. I am still very new to Nuxt and now that I am using useCookie with the ref, it's giving me a bahaviour that I don't understand. In this prototype I super simply just want to check if the session exists and if the JWT in the session is correct. However the fact that it's a ref is making this a bit more difficult I feel and I am struggling to get this to work properly, could you please assist? When no cookie is set, it is correctly redirecting back to / However when a cookie is set it is showing the protected page that is using this middleware for a split second, before then redirecting to / also. Why?
No description
Likonima
Likonima7mo ago
sounds weird. at least for the code that can be improved by using if(!session), if(!payload) and then the optional chaining for session?.value could be omitted not sure with the theme but did you do compare = null (which would assign a value) instead of === null ? regardless of that the code improvement should be done using logical not ! yeah that seems to be it and will likely fix your issue
Nik ⚡
Nik ⚡OP7mo ago
I initially had just the logical not (!) but was running into strange behaviour with it, which is why I changed it, but let me try again. Yeah same strange behaviour after applying those logical not changes
Likonima
Likonima7mo ago
but it's right to use that. any errors?
Nik ⚡
Nik ⚡OP7mo ago
No errors, just the strange redirect.
Likonima
Likonima7mo ago
unpleasant can you copy paste the modified code?
Nik ⚡
Nik ⚡OP7mo ago
Here an even more simple example of the strange behaviour (which may be the root case). Just having this code (nothing else) in the middleware
Nik ⚡
Nik ⚡OP7mo ago
No description
Nik ⚡
Nik ⚡OP7mo ago
It doesn't redirect (navigateTo) at all, whether a cookie with name session is set or not. I think it's just me missing something important regarding the fact that it's a ref?
Likonima
Likonima7mo ago
well again .value missing but it should redirect in this case since the variable is a ref containing either value of string, null or undefined
Nik ⚡
Nik ⚡OP7mo ago
This now same behaviour, as in it shows the protected page for a split second, then redirects to /
No description
Nik ⚡
Nik ⚡OP7mo ago
No description
Likonima
Likonima7mo ago
looks good to me. must be sth else
Nik ⚡
Nik ⚡OP7mo ago
Nik ⚡
Nik ⚡OP7mo ago
Yeah there is not really anything to the app.. It's strange. Also tried in a different browser, same issue.
Likonima
Likonima7mo ago
that needs some debugging. maybe some other condition messing with this logic. without seeing the project I can't tell from here
Nik ⚡
Nik ⚡OP7mo ago
Even though this is such a small project, creating a band new one now with just the protected page and the middleware, let's see.
Nik ⚡
Nik ⚡OP7mo ago
Ok found the problem @Likonima , when setting the cookie if httpOnly is set to true, it's doing the strange split second render and then redirect, if I don't set that property (or set to false) it works as it should. Would you know why that is?
No description
Nik ⚡
Nik ⚡OP7mo ago
Lol, I am giving up.. So it works for the simple check to see if the session exists. If I also do the additional check for the JWT I am again getting this strange split second redirect, so strange!!
Likonima
Likonima7mo ago
I have no clue but I've had several issues when trying to use nuxt as a backend and just don't use it.
Nik ⚡
Nik ⚡OP7mo ago
I would really like to use it, but it's just not making it easy for me to like it... :*( Thanks for your help anyways!
Likonima
Likonima7mo ago
it surely does not
Nik ⚡
Nik ⚡OP7mo ago
After some more trying around, looks like it has to do with doing an async call in the middleware, but I thought middleware supports async? The veryifyJWT is being awaited, once I remove that from the code it works.. Why!?!?
No description
Want results from more Discord servers?
Add your server