-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
accessing useCookie
on server immediately after setting it doesn't return cookie
#22631
Comments
The issue here is that when setting a cookie it is set in the response headers. But when reading a cookie it is looked for in the request headers. When you redirect to If your intent is to check for the cookie immediately after it is initially set (that is, in the same request) then you probably also need to set it in the if (process.server) {
// you can access arbitrary values on the event, like this
useRequestEvent().context.token
} There is a PR to do this automatically: #21940. |
useCookie
on server immediately after setting it doesn't return cookie
did you find a workaround? im having a similar issue where changing |
here is my case and how i fixed it.. i have 2 plugins: The first plugin: startup.ts something like this:
the second plugin: api.ts something like this:
the the weirdest thing, is the `fetchUser() is wrapped with if statement to check if we have a token cookie, that's why it got called, but why that cookie was not there in the second plugin? My fix, I followed @danielroe method above, by saving the token in
and then read it as well in the second plugin
it works, but im not really confident, and hopefully the root cause gets fixed |
@danielroe Hey, sorry for pinging you but any chances that this issue will be resolved soon? Seems like a critical one |
Probably it's a little bit late but here is workaround:
|
Hi, is there any news on this issue ? It seems that some methods has been implemented on |
@danielroe does this mean we want |
Yes, it would be good to do that within Nuxt. We would need to have the initial value of One tricky thing is that this very much depends on cookie options - these effectively mean we can have different cookies which need to be tracked separately but have the same 'name'. |
On the server side, could we handle shared cookie response data using the following steps:
This way, we can share data between different I'm not sure if this would effectively resolve the issue. If I've overlooked anything, please let me know. Thank you! cc: @danielroe |
@Mini-ghost That absolutely makes sense to me. But the issue - as you point out - is the possibility of varying I wonder if there is a way to address this? |
Environment
Nuxi 3.6.5
Reproduction
https://github.com/sync42johnny/nuxt-app
Describe the bug
Description:
An issue has been identified with the authentication logic in the middleware of a Nuxt application. The application attempts to authenticate a user by checking for an
authToken
cookie. However, due to theauthToken
cookie not being set on the server-side before navigateTo, the middleware encounters an infinite redirection loop.Details:
Affected Files:
index.vue
main.vue
app.global.ts
auth.ts
confirm.ts
Workflow:
index.vue
), theconfirm
middleware is invoked. This middleware tries to set theauthToken
cookie on the server-side./main
./main
page is protected by theauth
middleware, which checks for the presence of theauthToken
cookie.auth
middleware redirects the user back to/
, leading to a continuous loop of redirections.Additional context
The issue might be related to the incorrect functioning of the
useCookie
function when used within middleware before navigateTo.Logs
The text was updated successfully, but these errors were encountered: