Skip to content

Commit

Permalink
Forward JWT cookie as header (#917)
Browse files Browse the repository at this point in the history
Fixes 401s when SSRing the profile page after the request is rewritten
  • Loading branch information
SapiensAnatis authored Jun 30, 2024
1 parent 56d2927 commit f35a994
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Website/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ export const handleFetch: HandleFetch = ({ request, fetch, event }) => {
// Rewrite URL to internal
const newUrl = request.url.replace(requestUrl.origin, internalApiUrl.origin);
console.log(`Rewriting request: from ${requestUrl.href} to ${newUrl}`);
console.log({ cookies: event.cookies.getAll() });

// We need to explicitly add the JWT back in, because SvelteKit seems to refuse to forward cookies here; it's
// possible it views the request as changing origins and no longer internal.
const idToken = event.cookies.get(Cookies.IdToken);
if (idToken) {
request.headers.append('Authorization', `Bearer ${idToken}`);
}

return fetch(new Request(newUrl, request));
}

Expand Down

0 comments on commit f35a994

Please sign in to comment.