Hooks response not working unless hard refresh #7055
Replies: 3 comments 23 replies
-
|
Which browser are you using? Sometimes it's a visual bug, and switching to a different tab in the web inspector will show the updated list of cookies. Also, you can now use the SvelteKit Cookies API, which uses the - import * as cookie from 'cookie';
/** @type {import('@sveltejs/kit').Handle} */
export const handle = async ({ event, resolve }) => {
- const cookies = cookie.parse(event.request.headers.get('cookie') || '');
+ const csrf_bongo = event.cookies.get('csrf_bongo');
const response = await resolve(event);
- if(!('csrf_bongo' in cookies)){
+ if(!csrf_bongo){
const data = await fetch('http://127.0.0.1:8000/api/auth/user/csrf')
response.headers.set('set-cookie', data.headers.get("set-cookie"))
}
return response;
}; |
Beta Was this translation helpful? Give feedback.
-
|
I would say the problem is that you are using hooks.server.js which only it's executed on server calls? such as a hard refresh or calling from the client with a fetch(...) But I'm a noobie so take what I said with a grain of salt |
Beta Was this translation helpful? Give feedback.
-
|
You will need client side logic to invalidate a session when a cookie expires/is invalidated. You could have an endpoint that you hit which returns the user or a 401 response or something of the sort, then respond accordingly. A few ways you can accomplish this:
Also, have a look at https://github.com/pilcrowOnPaper/lucia-sveltekit which implements client side invalidation. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is my response.header:
for the first time when code run cookies set to browser. Later when i delete that cookie and refresh the browser cookie is not set to browser. If i hard reload the page, the cookie is set to the browser.
So i log the response.headers when the cookie is not set to the browser. here is the result
But set-cookie is not set to the browser. If i hard reload the page, the cookie is set to the browser.
Beta Was this translation helpful? Give feedback.
All reactions