You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am building a SvelteKit SPA app that is using Authorization Code Flow with PKCE with Keycloak.
I want to handle the auth redirect in the load() function of the root +layout.js file. During auth handling, Keycloak JS SDK removes the hash from the url. The problem is that SvelteKit restores the hash after load() function and the rendered url has the auth redirect hash visible.
See the repro below.
Reproduction
+layout.js
exportconstssr=false;/** @type {import('./$types').LayoutLoad} */exportasyncfunctionload(){console.log('[+layout.js] before auth:',window.location.hash);awaithandleAuth();console.log('[+layout.js] after auth:',window.location.hash);}
I'm not sure what's the best way forward here - or rather, if this is "works as designed" or if we should take into account this edge case.
The reason why this happens is that the URL is used to find out the correct route to go to, and that URL is later added unchanged to the function which updates the state, which then also resets the URL hash in your case. One could make the case that at least the hash should be checked for changes.
The workaround in the meantime would be to remove the hash inside the Svelte file or after a setTimeout in the load function, which resolves after it has returned.
I wouldn't call this an edge case. Authorization Code Flow with PKCE is a widely used auth flow in SPA applications. The current SvelteKit implementation is likely to cause a lot of confusion in these auth scenarios.
load functions shouldn't really contain side effects (such as writing to window.location.hash) — they can be called when you're not actually navigating (e.g. you mouseover a <a data-sveltekit-prefetch> link), so the timing is all wrong. I don't really understand what the handleAuth() function is doing in your example, but is it something that could happen in an afterNavigate callback?
Describe the bug
I am building a SvelteKit SPA app that is using Authorization Code Flow with PKCE with Keycloak.
I want to handle the auth redirect in the
load()
function of the root+layout.js
file. During auth handling, Keycloak JS SDK removes the hash from the url. The problem is that SvelteKit restores the hash afterload()
function and the rendered url has the auth redirect hash visible.See the repro below.
Reproduction
+layout.js
+page.svelte
Screenshot (see the address bar and console logs):
Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: