-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Route protection not working in SvelteKit v2 #9454
Comments
I recently met the same situation but usually it's no harm and the route will be automatically protected when you do server side data fetching by adding +page.server.ts |
Thanks for the quick tip! Makes sense to me. Closing 🙏 |
It is a temporary fix though, adding a +page.server.ts to each and every route just for re-running the hooks doesn't seem like the permanent right thing to do. And not doing that leads to a confusing user experience where they see an error code 500 without any explanation, which is to simply login, which should be done automatically by redirecting from the hooks |
@WhyAsh5114 I'm not very familiar with Svelte/SvelteKit, could you elaborate more on your recommended solution? |
@ndom91 okay, so the TLDR is, if a route (page) needs some data, say from an external API, you'd use either a Difference is that So the proposed solution is adding a I know the hooks wouldn't run unless a server-side event occurs, so I doubt there's anything we can do from the project developer's point of view to fix it. |
@WhyAsh5114 yeah so I get the redundancy of adding an unnecessary |
I'll try to think up something, I was getting decent results by putting the auth logic in +layout.server.ts, but I need to test it a bit since it's explicitly not recommended in the docs. I'll get back to you after I get a decent solution. |
Ah gotcha, I thought yuo had something in mind already. Well thanks and keep me posted, I'm up to help ship something for this 👍 |
I'm not 100% sure but I believe you can set ssr to true, csr to false, in a specific layout, to make child routes server side only. |
if we use a layout load function like so:
I do get all the desired results, since we are using The reason this was not recommended in the first place by AuthJS docs was because:
That issue is still open and being discussed, I guess there's not much to do from your side, other than maybe updating the documentation, because IMO, the Ofc this isn't perfect either, there's other problems with the layout approach mentioned in sveltejs/kit#6315, so the decision of whether or not to update the docs is subjective. I don't think there's much to do from your side since this issue is still open on SvelteKit's repo. |
Thanks for the deep dive on this and links! I think including the path param in the load function, to get it to run on each navigation, at least makes sense for now 👍 |
Environment
(also tried in Windows 11 with similar config)
System:
OS: Linux 6.2 Zorin OS 17 17
CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
Memory: 10.69 GB / 15.52 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
npm: 10.2.5 - ~/.nvm/versions/node/v20.10.0/bin/npm
npmPackages:
@auth/core: ^0.19.0 => 0.19.0
@auth/sveltekit: ^0.5.0 => 0.5.0
Reproduction URL
https://github.com/WhyAsh5114/authjs-sveltekitv2
Describe the issue
Using a sequence hook with SvelteKitAuth and an authorization function is not working as intended.
Protected route can still be accessed with no session, need to reload to redirect as expected.
How to reproduce
Expected behavior
Instead of being able to see the protected route, should redirect as defined in the authorization middleware function
The text was updated successfully, but these errors were encountered: