SupabaseS
Supabase3y ago
Igor

Issuing a JWT via SvelteKit API route throws `Cannot use `cookies.set(...)`...`

Cannot use cookies.set(...) after the response has been generated

In my SvelteKit (1.29.1.) I have an API endpoint that's used by the companion Chrome extension.

Extension grabs the cookie and sends it as a bearer in a fetch chain:
  • first to /api/refresh that's checking whether the token is still valid, resigns it if it's outdated, and returns it;
  • then /api/upload using the returned token attaches it again to the header, along with the body.
However SvelteKit crashes right after the endpoint returns the new signed JWT. While the token is still valid (I set expiration to 60 while reproducing the bug) (see the screenshot).

My hooks.server.ts implementation is based on the tutorials, minus the api route interception to catch the bearer.

I'm clueless. Any advice please? :)
CleanShot_2023-12-18_at_13.23.25.png
CleanShot_2023-12-18_at_13.25.14.png
CleanShot_2023-12-18_at_13.25.08.png
CleanShot_2023-12-18_at_13.25.04.png
Was this page helpful?