Тёмный

Protect SvelteKit Routes with Hooks 

Huntabyte
Подписаться 25 тыс.
Просмотров 58 тыс.
50% 1

Опубликовано:

 

20 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 184   
@YuGoCheff
@YuGoCheff Год назад
- great visualisation - mentioning issues many devs faced (e.g. confusion about 'locals -> local storage') - information apply for Typescript AND Javascript devs - great detail Thank you so much! You are helping sveltekit users so much! Please keep going :) Potential future topics: - error handling - integrating payment solutions (stripe, PayPal)
@nyashachiroro2531
@nyashachiroro2531 Год назад
You and Joy Of Code are doing amazing things in the Svelte community. Keep it up. I have been watching y'all a lot.
@Huntabyte
@Huntabyte Год назад
Thank you!
@WaelMx
@WaelMx 4 месяца назад
Hi. Any other channels/places would you recommend to learn Svelte? I'm new to SvelteKit and Supabase
@nyashachiroro2531
@nyashachiroro2531 4 месяца назад
@@WaelMx these 2 plus the docs will be enough. Trust me. I myself learned from the docs alone.
@WaelMx
@WaelMx 4 месяца назад
@nyashachiroro2531 Thanks a lot for your response!
@padeen2683
@padeen2683 Год назад
Just like with form actions, you are breaking down another advanced topic within Sveltekit. This video + using it in my own project = level up.
@Huntabyte
@Huntabyte Год назад
That's great to hear!
@padeen2683
@padeen2683 Год назад
@@Huntabyte I'm using this now with firebase passwordless login with email link authentication for a client project. This is the best "no fluff" channel for intermediate sveltekit.
@Mexad0n
@Mexad0n Год назад
Very informative and in plain English. Thank you
@Huntabyte
@Huntabyte Год назад
Thanks, Stan!
@maskman4821
@maskman4821 Год назад
Thank you Sir so much for explaining hooks, locals, protected route so clear, I finally know how to handle authentication with Sveltekit, you are live saver 🙏😭
@Huntabyte
@Huntabyte Год назад
You’re very welcome, Mask Man!
@jean_luc_retard
@jean_luc_retard Год назад
@@Huntabyte what if the auth token expires? For example, if I make a call to my API (in my case strapi) and it returns token expired error. Do I return that error from every page.server.js file? Or is it better to check token expiration time beforehand in handle method? What is the proper way of handling that?
@grahamhoran2038
@grahamhoran2038 Год назад
Another quality tutorial to compliment your growing SvelteKit playlist. As a beginner, I find that the diagrams make it much easier to understand the process and the dialog/interaction between the various steps in the system as a whole. It is very daunting to open up the SvelteKit documentation and know where to start, having just a basic understanding of web development. Having someone brighter and more technically gifted to lead the way is a real privilege. I really appreciate you giving your time, effort and energy. I hope to see you around far into the future and wish you every success with your channel.
@Huntabyte
@Huntabyte Год назад
Thank you so much for the kind words, I greatly appreciate it and am very happy that you find the content useful!
@Xalkoob-SD
@Xalkoob-SD Год назад
Wow, Thank you, sir. That is a straightforward and to-the-point description of Handle hook's usage.
@Huntabyte
@Huntabyte Год назад
You're very welcome!
@javierruelas1996
@javierruelas1996 Год назад
I’m working on authentication with SvelteKit right now and this is hugely helpful! Thanks so much
@Huntabyte
@Huntabyte Год назад
Great to hear, you're very welcome!
@frankerzed973
@frankerzed973 Месяц назад
You're such a blessing to this world
@asatorftw
@asatorftw Год назад
Thanks to you learning SvelteKit has been nothing but pleasure. Absolute life saver, thank you so much.
@Huntabyte
@Huntabyte Год назад
You’re very welcome!
@dellaian
@dellaian Год назад
This was the missing video I really needed, thank you very much for all your insightful videos on the subject of Svelte(Kit), but especially this one
@em11l
@em11l 10 месяцев назад
I didn't understand why someone would do this, but then It literally came to me in my sleep why this is a good solution. I also read up about this on git and sooo I tried to implement it. Soo much easier, cleaner and safer. Thanks for the explanaiton.
@unboxkarunadu372
@unboxkarunadu372 Год назад
SVELTEKIT 1.O OFFICIALLY RELEASED. 🤩
@Huntabyte
@Huntabyte Год назад
I was using it in this video ✅
@hongquannguyen3243
@hongquannguyen3243 7 месяцев назад
Thank you for sharing. However, when I use group layout(), I encounter an issue where the URL is not received within the hook, and if I place it outside, the layout is not separated between routes.
@alexponoran7146
@alexponoran7146 Год назад
Thanks so much, I'm finally beginning to use SvelteKit at its full potential!
@Huntabyte
@Huntabyte Год назад
That's great to here! Thanks for becoming a channel member, it means a lot!
@artneo7
@artneo7 Год назад
Great video, really helped me understand the locals concept! Thanks a lot! 😄
@Huntabyte
@Huntabyte Год назад
Glad to hear that! You're very welcome and thanks for taking the time to comment!
@ABsazerNer
@ABsazerNer Год назад
OMG finally I get what locals means, thank you so much 👍
@dubspwns
@dubspwns Год назад
Fantastic video. Very clear and concise. Much appreciated. Thank you.
@Huntabyte
@Huntabyte Год назад
I'm glad to hear that, you're very welcome!
@StarlightInsights_com
@StarlightInsights_com Год назад
Your videos are so great! Thanks for posting them 🙏👌
@Huntabyte
@Huntabyte Год назад
You’re very welcome, and thank you for the kind words!
@danielkrastev6786
@danielkrastev6786 Год назад
It is important to note that this wouldn't work for client side navigation, as the Handle hook would only run when requests are made to the server. If svelte assumes it wouldn't need to call the server, when you go to `/protected` you would still be able to access it even when logged out. I think for you it calls the server, because you have a `protected/+page.server.js` that does nothing. If you remove that you would probably be in trouble.
@guhkunpatata3150
@guhkunpatata3150 Год назад
ah yes this is true, this is need to mentioned!!!
@guhkunpatata3150
@guhkunpatata3150 Год назад
any idea how to handle this ?
@xffeeo
@xffeeo Год назад
prolly disable csr@@guhkunpatata3150
@voiciray
@voiciray 5 месяцев назад
@@guhkunpatata3150you've probably figured this out already but for anyone else, you could use getSession in a hooks.client.js file, read your cookie for eg an access token and redirect if the user was logged out.
@mursie100
@mursie100 12 дней назад
Ok so we add "+page.server.ts" to every route, that fixes the issue right?
@mikle1404
@mikle1404 Год назад
This is EXACTLY what I was looking for, great example and even better explanation. Thanks a lot!
@gedaredorke5941
@gedaredorke5941 Год назад
found your channel and really loving it. I have been tasked with build a simple application for onboarding customers. I am pretty much a backend dev but sveltekit has become my goto framework for when I need it. Your content is really amazing, I have learnt so much
@Huntabyte
@Huntabyte Год назад
That's awesome to hear, thanks for taking the time to comment!
@rogerpence
@rogerpence Год назад
What a great Svelte/SK resource! Thank you
@8DMafia
@8DMafia Год назад
Best channel for svelte
@Huntabyte
@Huntabyte Год назад
Thank you!
@luminox1
@luminox1 Год назад
++ on the route protection trick with the handle hooks!
@Huntabyte
@Huntabyte Год назад
I’m a for hook route protections!
@Huntabyte
@Huntabyte Год назад
ATTN: I’ve heard this comment multiple times so I need to address it to stop the spread: Do not solely use +layout.server to handle auth for a bunch of routes, unless you are awaiting parent on every subsequent +page.server. Watch this video for more info: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-UbhhJWV3bmI.html
@greendsnow
@greendsnow Год назад
Hey Hunter! If you can make your VS code font as big as your h1 it'll be so much easier to watch your videos on the phone
@ScriptRaccoon
@ScriptRaccoon Год назад
Very useful and very well explained, thank you!
@Huntabyte
@Huntabyte Год назад
You’re welcome!
@ScriptRaccoon
@ScriptRaccoon Год назад
@@Huntabyte I was wondering if you could publish your (awesome!) hooks graphic and add it to the video description? This is very good teaching material.
@tobias3464
@tobias3464 Год назад
Thank you very much! Clear explanation! It helps to make progress!
@FlygOnLiTe
@FlygOnLiTe Год назад
Another banger!! 🔥🔥🎉
@Huntabyte
@Huntabyte Год назад
Thank you!
@bunny_the_lifeguard9789
@bunny_the_lifeguard9789 Год назад
17:38 if you want to move it down without the mouse you can simply hold alt and then use the arrow keys to move it up or down. Also if you hold shift + alt and then use the arrow keys you can duplicate the selected line(s) :)
@Huntabyte
@Huntabyte Год назад
I do actually use those shortcuts quite a bit (if you watch my streams/other videos), for some reason I just choose not to sometimes 😂
@2600D4u
@2600D4u Год назад
Damn, I think this is the exact thing I am looking for. Very good video, ty!
@ghoshsanjoy
@ghoshsanjoy Год назад
What an awesome video - explained everything so well
@JOCS20
@JOCS20 Год назад
Awesome content as usual. Thanks for making this!
@Huntabyte
@Huntabyte Год назад
You’re welcome!
@Nataaq
@Nataaq Год назад
Very useful info using hooks, can't wait for your other lessons in the sveltekit series ❤❤
@Huntabyte
@Huntabyte Год назад
I'm glad you found it useful!
@kylerjohnson988
@kylerjohnson988 Год назад
Phenomenal video. Thanks!
@Huntabyte
@Huntabyte Год назад
You’re welcome!
@hunterwoodall8222
@hunterwoodall8222 Год назад
Great video! I do have a quick question though. In the Admin protected route section, will this work if you were to provide a link to this section so that the application routes client side (instead of directly typing the route in the address bar)? I think if the admin route doesn’t have a +page.server.js file, then the hook will never run meaning if the user was logged out while on the page with the link, they could still navigate to the admin route. Any thoughts here would be appreciated. It’s something I am struggling to figure out a way around at the moment.
@vitorguidorizzzi7538
@vitorguidorizzzi7538 Год назад
Having the same issue, hard to think the creator of this video did not notice this.
@joshua.hintze
@joshua.hintze 9 месяцев назад
I was thinking the same thing. Although the video was good author doesn’t discuss how to stop client side navigation.
@j3z741
@j3z741 Год назад
Thanks! Great video!
@Huntabyte
@Huntabyte Год назад
You're very welcome!
@Techthy12
@Techthy12 Год назад
Idk if it's me but the handle hook only get called once when the page is fetched. If the user then navigates you need client side code that prevents the user from beeing ablte to access your content. That's why I now have both within my code. Maybe someone know whats wrong?
@divino6502
@divino6502 4 месяца назад
Great video, appreciate the straightforward approach! One question: how would you approach consuming the session/user data in the protected routes themselves. Type-wise, the session and user objects can both be undefined, and +page.server is not "aware" that the auth check has been done in the hooks already. So the options are to either do a non-null assertion, or do the session null check + redirect in +page.server again, even though it has already been done in the hook. Any other option which would be cleaner?
@devinosborne3396
@devinosborne3396 Год назад
easiest subscribe in a while. here for it.
@ykhi
@ykhi Год назад
Great tutorial!
@Huntabyte
@Huntabyte Год назад
Thank you!
@philidornews4055
@philidornews4055 3 месяца назад
Awesome, this was so helpful! Thanks! Do you have a strategy for components too? For instance if you wanted to render markup conditionally in the main nav (like showing a login button if the user isn't logged in yet). How do you keep it reactive?
@Hugos68
@Hugos68 Год назад
Banger video, you can also do (procected) directory to still use layouts without creating a subdirectory for the routes
@Huntabyte
@Huntabyte Год назад
Handling Auth in Layouts without properly calling `await parent()` on every subsequent +page.server is not secure: github.com/sveltejs/kit/issues/6315
@Hugos68
@Hugos68 Год назад
@@Huntabyte Oh how strange, let me fix that real quick before deploying haha, you're a lifesaver, will they address this or will this be how it is?
@Huntabyte
@Huntabyte Год назад
@@Hugos68 I'm posting a video on this in a few minutes :) which will include a workaround if you do prefer to use layouts.
@Tenif123
@Tenif123 Год назад
Awesome content!
@Huntabyte
@Huntabyte Год назад
Thank you!
@guhkunpatata3150
@guhkunpatata3150 Год назад
thanks for the great explanation!
@guhkunpatata3150
@guhkunpatata3150 Год назад
seems like file hook.server.ts only triggered when making request to server side. when browsing in client side, its not working. please help any suggestion ? thanks before
@imadev4861
@imadev4861 Год назад
Keep it up bro. I support you.
@littlefreak3000
@littlefreak3000 2 месяца назад
Interesting to see how Svelte does middleware. Its a shame they don't just call it that. I feel a lot of us with experience know what middleware is. These new frameworks just love the word "hook" lol. Thanks though this video really helped, be wrap my head on how svelte does this.
@__da1m__
@__da1m__ 9 месяцев назад
thanks, for this! 2 quick questions - given we already have the hook.server.ts which handles the redirect do we need the load function or even the +page.server.ts file (setting aside that you need it for simulating a login in this case) in the base of the routes folder? - how do you decide if the hook file should be a server.ts or or just a ts file? thanks again! love the content :)
@martinsandor707
@martinsandor707 6 месяцев назад
if I have a separate backend server running, and I want to make a fetch call which passes a cookie, what do I have to do for Svelte to let me pass the cookie? I've looked at the documentation, but handleFetch doesn't seem to work for me. The backend server is on the same machine, just on a different port.
@DisasterSPA
@DisasterSPA Год назад
Nice tutorial. I've got an off topic question though: Which vs code icons are those?
@Huntabyte
@Huntabyte Год назад
Material Icons, however, I have modified the directories underneath of routes to be green :)
@ShourovRoy-pk5zm
@ShourovRoy-pk5zm 4 месяца назад
Not working with links thats are made with a tag in build version prerender mood. But working perfectly in dev mode. Any solution?
@meaningmean
@meaningmean Год назад
Great explanation. Thanks 👍 you might want to try sveltekit auth which vercel just announced
@Huntabyte
@Huntabyte Год назад
I'll certainly look into it!
@StarkTech47
@StarkTech47 4 месяца назад
That is very awesome, good explanations and great topic, you made me understand the hooks. Does it work with another backend than sveltekit like node js? I have tried but without any success.
@MrKooops
@MrKooops Год назад
just send a few coffees your way, thank you!
@Huntabyte
@Huntabyte Год назад
Hey I appreciate that! Thank you!!
@MrKooops
@MrKooops Год назад
@@Huntabyte well deserved!
@sahilmulla1680
@sahilmulla1680 27 дней назад
If I go to the admin page using an anchor link it bypasses the hook, it only runs if there is a +page.server file inside the admin folder, however if done as a hard redirect it checks the role
@roberttmorgan
@roberttmorgan Год назад
Hunter, I could be mistaken but at about 16:17 in the video, when you're demonstrating that a user with a regularusertoken would be unable to access the '/protected/admin' route, it looks like you were able to access the route with the regularusertoken. Was there something misconfigured there - I haven't been able to grab the repo and go through it yet, thank you.
@Huntabyte
@Huntabyte Год назад
I couldn't, I was stupid and for some reason had the `h1` tag on the '/protected' page say "Admin". If you look again the /protected page has a title of `Admin` and '/protected/admin' has the title of Admin Page!
@roberttmorgan
@roberttmorgan Год назад
​@@Huntabyte ah, ok that makes sense. I appreciate the reply to clarify - thank you for the amazing content!
@Huntabyte
@Huntabyte Год назад
@@roberttmorgan You're very welcome!
@EvertvanBrussel
@EvertvanBrussel 9 месяцев назад
Hey, uhm, I don't know if I'm doing something wrong, because uh... it works for the most part, except that the client-side router may still execute the load function of a protected route when it's preloading that protected route as a response to the user hovering over a link that points towards a protected route. So I still get a console log in my browser that basically says "fetching sensitive data". It seems like in that scenario, the hook on the server is not being invoked. Do you have any tips for that? Or do I just have to disable preloading?
@pengain4
@pengain4 Год назад
I kinda wish having group-scoped hooks in the future 😊
@geonhoyun
@geonhoyun 7 месяцев назад
Great video!!
@chased_productions
@chased_productions Год назад
So is all this necessary if you're using firebase or something similar?
@fuskaren
@fuskaren Год назад
So how do we do if we want the handle to be run on client-side routing as well? I have some pages that are not super important to guard, but I still want to redirect if the user got logged out for some reason. Now they can still navigate in the app client side when the cookie is removed.
@lian1238
@lian1238 Год назад
I had problems with this technique in my own code: after logging out, i redirect users to the login page. But the user is able to go "back" to the protected page, and still use the nav menu to go to another protected link and it would STILL load that page (and not redirect to login page like it should.) The only simple solution I found is to disable csr. With this solution, the user could go back, but clicking a link to protected route would correctly redirect to login page.
@justintime9720
@justintime9720 3 месяца назад
might look good in theory, but if you have an API call inside +page.server.ts it then event.url.pathname will show the API url, not the current page?
@el.bromas
@el.bromas Год назад
Something than I found is every route than you want to protect you have to create +page.server.ts file, Are there something different to that? thank in advance
@zacontraption
@zacontraption Год назад
Great video
@Huntabyte
@Huntabyte Год назад
Thank you!
@jurgensubat2827
@jurgensubat2827 Год назад
Thanks for this great tutorial. After I wached that video I downloaded the coding from git because "LOGOUT" doesn't worked correctly in my coding. The cookie is not deleted and still I don't know why. After I downloaded your coding from git plus installation, the issue is stile there. Do you have any advise, why the cookie is not deleted?
@nickstaresinic9933
@nickstaresinic9933 Год назад
If I've understood the diagram at the start of the video correctly, can I think of the Handle hook as (similar to) middleware?
@Huntabyte
@Huntabyte Год назад
You are exactly right.
@raghavendra6978
@raghavendra6978 3 месяца назад
thank you!
@EssaAlshammri
@EssaAlshammri Год назад
Thanks a lot
@Huntabyte
@Huntabyte Год назад
You're very welcome!
@CodeNCut
@CodeNCut Год назад
Fantastic video! I first watched your "don't protected with layouts", and then watched this... You helped me understand a ton about the page building process, and where to intercept the authentication checks. I really appreciate it. One question, if you put all of your protected content under a "(app)" folder using parenthesis, how should we update the event.url.pathnamne.startsWith function? (around 13:55 in). How to apply the hook to every page within a group?
@bdueck1234
@bdueck1234 Год назад
Great video, thanks for putting it (and others) together. One thing I don't understand though, it seems that your protected route requires protected/+page.server.ts to exist (even if empty) in order for the handler hook to fire from a link. If you remove protected/+page.server.ts you will see that the hook is not run unless you manually navigate to it by typing in the URL (e.g. localhost:/protected). Is Sveltekit intended to work this way? Seems like a variation of the naive layout issue you referenced elsewhere in comments that could lead to dangerous consequences if you somehow forget to put an empty +page.server.ts file there.
@Huntabyte
@Huntabyte Год назад
I believe it is intended to work that way, as having a +page.server tells SvelteKit there’s something needed from the server to load that page (even if it’s empty). There are open discussions on GitHub about this and the idea of making protecting routes more obvious, something like a +auth.server. Hopefully the maintainers can make a decision and move quickly, as there are many outstanding ‘gotchas’.
@wolfeygamedev1688
@wolfeygamedev1688 Год назад
@@Huntabyte Not sure if there is something for this yet. but so far I really dont like doing protected routes in sveltekit, it feels very janky / an afterthought compared to more established frameworks. Having to remember to put an empty +page.server.js in order to protect each individual route kinda makes the hook feel almost as janky as the layout.server.js, additionally at least with the layout.server.js It is nested which routes are protected, while I have to specify each route for the hooks solution. Both are kinda just trash tbh.
@brettski-tc
@brettski-tc Год назад
@@Huntabyte could the same need for +page.server be done at the root of that path with a +layout.server.js to ensure that hooks.server is run? In some initial testing I put a a +layout.server at /my, and when navigating to /my/info/profiles or /my/info, hooks.server did fire. I am not sure if this is foolproof or not and am wondering what others think of the approach.
@OlavurNonE
@OlavurNonE 9 месяцев назад
@@brettski-tc After some testing, i also found this to work, but would be nice to have this a confirmed method from more rutined people
@Tomaduf
@Tomaduf Год назад
Great video ! But I have a problem, if I delete the cookie and navigate with the previous page button of my brownser, i can still reach protected route unitl I refresh the page manualy. This is the same problem that explain in the next video "Stop using +layout for auth", thanks
@0xedb
@0xedb Год назад
So handle is just a service worker for Svelte, right?
@hiranga
@hiranga Год назад
I get the above, but what happens if you have a link Href on the Layout or Nav bar, and a user click on it to go to a /protectedpage ? I'm finding the above doesn't prevent a user from doing that :S ? Am I missing something? Edit: every protected page must have a server side function, otherwise it will bypass the hooks and be rendered as a static site page! I used a console log during testing to trigger this 👌🏾 works!
@timurtimaev8808
@timurtimaev8808 Год назад
Thank you for a great video! Can you please tell me if event.locals object is different for every user? So i dont get leaks between different users' sessions? Sorry for a perhaps dumb question but i'm just starting... Thanks again!
@Huntabyte
@Huntabyte Год назад
It is not only unique to each user, but unique to each request even from the same user. Locals is created and only lives for the duration of each request!
@timurtimaev8808
@timurtimaev8808 Год назад
@@Huntabyte Thank you!
@deimne970
@deimne970 Год назад
I think SvelteKit should provide a better way to deal with this with something like Angular guards, for example.
@StarlightInsights_com
@StarlightInsights_com Год назад
Thanks!
@Huntabyte
@Huntabyte Год назад
You are welcome and thank you for the generous donation!
@one_juru
@one_juru Год назад
Hey, I'm just starting to learn Svelte, or really making webapps in general, and just want to thank you for your awesome content so far, that I was able to discover yesterday and today. I noticed you did some videos on Supabase and Pocketbase, and while I really like Supabase, their selfhosted version is not as feature rich and usable as their cloud version. An interesting alternative to those two is Appwrite, wich selfhosted version gives you the same experience as their hosted one. Would it be possible for you to make some basic tutorials about Svelte and Appwrite, like securely loggin in users, with it? I'm a bit unsure how to use handle routes together with appwrite for securing access. Thank you again for your awesome tutorials :)
@Huntabyte
@Huntabyte Год назад
Thanks for the comment! I have explored AppWrite in the past but they didn't have solid SSR support, things may have changed though, so I will look into revisiting!
@mt09rider72
@mt09rider72 Год назад
Love the content, do you have any plan of doing something similar with Firebase auth?
@Huntabyte
@Huntabyte Год назад
Possibly in the future!
@eduardolagoeiro1810
@eduardolagoeiro1810 Год назад
Nice video
@Huntabyte
@Huntabyte Год назад
Thank you!
@gabrieluhlir
@gabrieluhlir Год назад
Just implemented some authentication thanks to this video. But what would be the best practise to save cookies into local storage to access the user info globally? For example, I have a Navigation.svelte component in the root +layout.svelte (where i want to display the user email) but I don't want to have every page request on the server side. But since the auth.js, hooks.server.js, and registration are done on the serverside, where should I save the cookies to the local storage? Should I have some condition in the root layout.svelte component when the cookie is set? Or should I just access the cookie in the .svelte files directly? Thanks for any help :)
@Huntabyte
@Huntabyte Год назад
You could run a single load function on your root +layout.ts and return the users information, then within your components, use $page.data.user.email.
@khairulhaaziq2332
@khairulhaaziq2332 Год назад
hunter love your videos! Could you possibly make a video about middlewares? Im thinking of multitenant with subdomain for each tenants and I think this is doable via using a function in +layout.ts but not sure how, another option is using node/express adapter. Do you know how to do it, can you possibly cover it in a video? Thanks!
@Huntabyte
@Huntabyte Год назад
I've been looking into this a bit myself, once I get through a few others things I'm working on, I will spend a bit more time on it and probably create a video!
@nazuu
@nazuu Год назад
Liked and viewed . Fair trade =)
@Huntabyte
@Huntabyte Год назад
Thank you!
@RyszardDzwonkowski
@RyszardDzwonkowski Год назад
why do we need cookies then, if everything can be stored in locals?
@Huntabyte
@Huntabyte Год назад
Locals read from the cookies!
@sarma_sarma
@sarma_sarma Год назад
hook only run which route have +page.server.ts file if you navigate any route inside produced path without have +page.server.ts using button it will navigate because hooks.server.ts not run
@ykhi
@ykhi Год назад
I got an empty object when console logging... after 20min I found out it was because I named the hook file `hook.server.ts` and not `hooks.server.ts` ......................
@AdPast8080
@AdPast8080 Год назад
At 13:30, you demonstrate protected an entire path with hooks.server.ts. Then you say, "you're still going to want to ensure users aren't trying to create items for other users and things like that . . . ." Can you elaborate on what you're referring to? Are you referring to row-level security on the database side, or to some other checks? Also, I'd love a tutorial tying together SvelteKit protected routes and Supabase authentication.
@EECyrpys
@EECyrpys 8 месяцев назад
There seems to be a bypass to this method if you have a link from an unprotected route to a "protected" route
@EECyrpys
@EECyrpys 8 месяцев назад
Oh this went away when I added a +page.server.ts to the route, so it seems anything server side triggers the hooks, which is fine
@arslantariq89
@arslantariq89 Год назад
hi, great video and it is really helping me in my project. But a quick question, is it secure doing it this way? I mean we are using cookies to handle authentication and I just managed to land on the admin page by just changing the value of the cookie in my browser (with a cookie editor) from "regularusertoken" to "adminusertoken". Just like that I was able to land on to the admin page. I have heared about cookie steaaling and such but I wanted to ask if the case I am describing is the same as cookie stealing or is the issue with the way we are handling the request? Because in the current scenario any user can just log in to our site as admin without much trouble at all. Thanks
@arslantariq89
@arslantariq89 Год назад
Also can you please guide as to what can be done to prevent this? Thank you
@somyongkim8237
@somyongkim8237 Год назад
I think that you would use JWT tokens stored in httpOnly cookies to prevent this.
@Pscytlk
@Pscytlk Год назад
please make some clone tutorials using sveltekit. Thanks in advance.
@garlandcrow
@garlandcrow Год назад
I find this easier to do by just moving whatever routes in to a route group like `(unauthenticated)` and `(app)` then making a `+layout.server.ts` that does the check of user is logged in or user role etc and doing the redirect there. That way you can apply to all the routes w/out relying on hardcoded route strings and having to prepend a path like `/protected` to it.
@Huntabyte
@Huntabyte Год назад
Layout load only runs when the first route of that layout loads. So you aren’t actually checking auth status with every request to a protected resource, only the first protected resource. There’s actually an issue open right now on this exact issue: github.com/sveltejs/kit/issues/6315
@karolus__
@karolus__ Год назад
@@Huntabyte interesting… so when you enter /(protected)/something-else the layout in (protected) will not run?
@karolus__
@karolus__ Год назад
I wasn't able to achieve that to be honest, but maybe it's a good opportunity to make a video about it, as some people may rely on layout.server
@Huntabyte
@Huntabyte Год назад
Put a console log inside your layout load. Navigate to /protected then click a link in your app that takes you to /protected/item. If you directly navigate through the URL, that will trigger the layout load to rerun, however, if you navigate through your app (through a link or nav menu), the layout load will not rerun unless you exit that layout tree and re-enter it. I had to investigate this quite a bit, but when I found the gaping hole in my security I've been advocating against that way of doing things. It's on the SvelteKit post-1.0 roadmap to be addressed, so I'm interested to see how they handle it. I will create a video on this most likely.
@jamessummers2491
@jamessummers2491 Год назад
wouldn't this just clutter your handle hook? you'd be mixing permission checks along with user auth and probably some more stuff in one hook. wouldn't it be better to just check for permissions on each subsequent route with pageserverload? of course the benefit of handling all of it in the server hook would be that you have it one place but other than that i see no real advantage of this over checking it in the load function of each page. i may be wrong though and if i am, please do tell.
@Huntabyte
@Huntabyte Год назад
I typically do check in each page server load, however, for a massive group of routes like '/admin' where everything has auth requirements, this is an easy way to handle it.
@jamessummers2491
@jamessummers2491 Год назад
@@Huntabyte ah i see. since there can be a lot of nested routes you may want to group them in the handle hook. gotcha. great sveltekit content btw, keep it up!
@panosstavrianos1473
@panosstavrianos1473 7 месяцев назад
Wait, is this the best solution that we have? This is terrible, basically we disable CSR, so every request goes back to the server in order to check the secure cookie... There's got to be a better way.. BTW do we even care if the user deletes their token? We have to revoke it server side anyway. Am I missing something here?
@강용우-n2h
@강용우-n2h 7 месяцев назад
Standard of explain
@greendsnow
@greendsnow Год назад
So is this the ultimate fool proof way?
@Huntabyte
@Huntabyte Год назад
For summaries/groups of routes, yes. To be extra safe, you can always check on each +page.server and action as well.
@SumanthChinthagunta
@SumanthChinthagunta Год назад
Are you using @rometools ? Curious how it is working with SvelteKit
@el.bromas
@el.bromas Год назад
How can I protect layouts too? I created +layout.server.ts and I put the same code but it does not work
@lauritzoffe6749
@lauritzoffe6749 Год назад
I get the following Error: Error: Parse failure: Identifier 'Handle' has already been declared (3:11) I´m at 5:09 in the Video and my Code in the hooks.server.js is: import {Handle} from "@sveltejs/kit" export const Handle = async ({event, resolve}) =>{ const response = await resolve(event) return response } Does anyone know what i´m doing wrong?
@Huntabyte
@Huntabyte Год назад
If you’re not using typescript you don’t import ‘Handle’. Just do: export const handle = …
@lauritzoffe6749
@lauritzoffe6749 Год назад
@@Huntabyte ist that also for the next step in the +page.server.js? Because I get this error : Error: Invalid export 'PageServerLoad' in src/routes/+page.server.js (valid exports are load, prerender, csr, ssr, actions, trailingSlash, config, or anything with a '_' prefix) My Code: export const PageServerLoad = async ({locals}) => { console.log(locals); }
@mateoroldos6287
@mateoroldos6287 Год назад
You are the best 🫶
Далее
The Problem with Using Layouts for Auth
7:10
Просмотров 51 тыс.
Svelte 5's Secret Weapon: Classes + Context
18:14
Просмотров 23 тыс.
These Are Too Smooth 😮‍💨
00:57
Просмотров 2,8 млн
Learn SvelteKit Hooks Through 6 Examples
23:55
Просмотров 20 тыс.
10X Your SvelteKit Developer Experience in VSCode
10:00
How Fast Can I Fill My Inbox?
13:30
Просмотров 355 тыс.
Best UI Library for Svelte
16:13
Просмотров 109 тыс.
Microservices are Technical Debt
31:59
Просмотров 544 тыс.
React visually explained: 'use client'
15:57
Просмотров 53 тыс.
Svelte 5 Preview | Runes
12:40
Просмотров 78 тыс.