Replies: 3 comments
-
|
would be nice if someone gives an insight about this |
Beta Was this translation helpful? Give feedback.
-
|
The restriction exists because SvelteKit's The solution: Move token refresh to
|
Beta Was this translation helpful? Give feedback.
-
|
si el problema es que no puedes setear cookies en query functions, la solucion es mover esa logica a un hook. en hooks.server.ts puedes interceptar la request, verificar el token corto, si expiro usar el largo para renovarlo y setear la cookie ahi antes de que llegue a la query. // hooks.server.ts
export const handle = async ({ event, resolve }) => {
const shortToken = event.cookies.get("short_token")
if (isExpired(shortToken)) {
const longToken = event.cookies.get("long_token")
if (isValid(longToken)) {
const newShort = generateShortToken(longToken)
event.cookies.set("short_token", newShort, { path: "/" })
}
}
return resolve(event)
}la restriccion existe porque las query functions pueden ejecutarse en contextos donde no hay una response HTTP activa (prerender, etc). los hooks si tienen acceso completo a la response. es un poco lio al principio pero tiene su logica |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a JWT session system using long-lived and short-lived tokens. I’m making a request to a query function, but the short-lived token has expired. My logic is to use the long-lived token to verify the user in the DB, return the requested data, and ideally send back a new short-lived token as a cookie to save a DB trip on the next few requests.
However, I’m getting an error: Error: Cannot set cookies in query or prerender functions. Since this is my first time implementing JWT sessions from scratch, am I doing something wrong, or is there a workaround? Also, why does this limitation exist? It feels arbitrary, but I assume it’s for cacheability, though I wouldn't want these requests cached anyway, as they’d return stale data.
Beta Was this translation helpful? Give feedback.
All reactions