Better Protected Route Redirects

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ม.ค. 2025

ความคิดเห็น • 106

  • @luminox1
    @luminox1 ปีที่แล้ว +64

    It's the little things that makes the biggest differences when it comes to user experience.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +3

      💯. Thanks for the comment my guy!

  • @ynokenty
    @ynokenty ปีที่แล้ว +17

    Good one, thanks for bringing this up! One thing that's missing here is that you would want to urlencode the redirectTo parameter before appending it to your url because any additional search params will get lost if you had 2+ of them on your initial route.

  • @axbe_
    @axbe_ ปีที่แล้ว +4

    Very good reminder to use the most basic state management: URL!

  • @Khari99
    @Khari99 ปีที่แล้ว +8

    Thanks for all the hard work! Its little things like this I never would have thought of for my application that makes things much more polished

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You're very welcome

  • @vadimtea
    @vadimtea ปีที่แล้ว +8

    Thank you, I was just looking into it the other day. Your implementation seems easy and makes a lot of sense

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Glad it was helpful!

  • @jazzy4535
    @jazzy4535 ปีที่แล้ว +3

    Great video. I'm not sure if this is mentioned in the video but this only works with default action as far as I can tell, because named actions replace the search params. For now I have added redirectTo to the form in a hidden field but an alternative solution is welcome.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      You should be able to add additional query params to named actions. For example, if you had an action named `createItem`, you could do `?/createItem&redirectTo=whateveryouwant`. I haven't tried this specifically just yet, but I would think that would work.

    • @jazzy4535
      @jazzy4535 ปีที่แล้ว

      @@Huntabyte ah, of course. Thank you, will try it out

  • @XiaZ
    @XiaZ 10 หลายเดือนก่อน

    6:40 You can skip the `let message: string` declaration because Svelte's reactive statements will inject a let declaration on your behalf if said statement consists entirely of an assignment to an undeclared variable.

  • @mcdba41
    @mcdba41 ปีที่แล้ว +4

    Great idea ! Very useful
    Add in project via hooks
    const protectedRoute: Handle = async ({ event, resolve }) => {
    if (event.route.id?.startsWith("/(protected)")) {
    if (!event.locals.user) {
    const redirectTo = event.url.pathname + event.url.search;
    throw redirect(303, `/login?redirectTo=${redirectTo}`);
    }
    }
    return await resolve(event);
    };
    export const handle = sequence(auth, protectedRoute);

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Very good idea! Only problem is you lose the ability to define specific messages for each redirect if you wanted!

  • @Yoggan0
    @Yoggan0 ปีที่แล้ว +1

    I liked this video so much that I liked it twice without noticing! Keep it up!

  • @StarkTech47
    @StarkTech47 7 หลายเดือนก่อน

    Great video !!! I have noted something with the form, which is when the actions is default, it works great but when the actions is login for instance, the search in the event is changed to the action pass in the form so "?/login".

    • @estebantrillo3050
      @estebantrillo3050 6 หลายเดือนก่อน +1

      You can add something like this to the formAction
      ` `
      That would append the redirectTo to the URL without any issues

  • @KevinMacKenzie61
    @KevinMacKenzie61 ปีที่แล้ว +1

    Perfect timing. Just today I was thinking I need to do this and voila!

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      I read your mind!

    • @KevinMacKenzie61
      @KevinMacKenzie61 ปีที่แล้ว

      @@Huntabyte I was thinking that, but then I figured you shot the video yesterday. So you have made skills if you can read my mind before I have even had the thought.

  • @yokiano5646
    @yokiano5646 ปีที่แล้ว +1

    It's funny how it looks exactly the same like an app I created just now (we both used an untouched pico.css duh...), and I needed to solve that exact aspect that you are talking about, so cheers, nice tips.

  • @levicoderman
    @levicoderman ปีที่แล้ว +1

    Is it possible to use cookies instead of the url params for redirects?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Certainly! You’d just have to grab the cookie contents, convert it to JSON, and return it via the page data!

  • @bjul
    @bjul ปีที่แล้ว +1

    Great stuff! Appreciate this format.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thank you! More to come!

  • @asatorftw
    @asatorftw ปีที่แล้ว +1

    Great content Hunter! thanks

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thanks, and you're welcome!

  • @enumerodev
    @enumerodev 11 หลายเดือนก่อน

    Hey, thanks for this great video!
    Regarding routes; would you know if its possible to create fully dynamic routes using sveltekit? meaning the routes would come from an api and you basically would avoid the folder structure?

  • @willemdevries9890
    @willemdevries9890 ปีที่แล้ว +1

    Great video as always Hunter! Thanks for the tip

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thank you and you're very welcome!

  • @-ion
    @-ion ปีที่แล้ว +6

    Shouldn't the query parameters be escaped with encodeURIComponent?

  • @hiryuimajin
    @hiryuimajin ปีที่แล้ว

    Whenever I am developing I highly consider UX even if it beats the UI haha. Remember the UI designer aint gonna be the one who's going to be looking at the app all day. But it's a bliss when you work with a UI/UX designer, the consideration they put on the designs are tremendously helpful to developers.

  • @7heMech
    @7heMech ปีที่แล้ว +1

    Glad I subscribed.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Thank you! I am glad you did as well!

  • @thienhuynh7962
    @thienhuynh7962 ปีที่แล้ว +5

    A bit irrelevant a question but can you explain form validation on client side with use:enhance and server side with form actions (both using zod for validation)? In some tutorials I see people use one of the two validation methods instead of both. But when I ask on stackoverflow it is suggested that I do validation on both server and client.
    Btw great video, really help me with my project.

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Yeah! Check my recent video on forms out, that should answer your question! You should ALWAYS at a bare minimum validate on the server, but of course client is a nice UX touch.

  • @joshuaruf9807
    @joshuaruf9807 ปีที่แล้ว +1

    Great video as always! Out of curiosity, would the redirect link and message be better suited to a store?

    • @arturlinnik2128
      @arturlinnik2128 3 หลายเดือนก่อน

      I think the redirect link is better in the URL but the message should be in the store

  • @AndyIsHereBoi
    @AndyIsHereBoi ปีที่แล้ว +1

    this should have way more views

  • @bobbradleybell
    @bobbradleybell ปีที่แล้ว

    Nice. I will do this in my app today hehe.
    Btw, are you using any special software for zoom in/out effect in your vídeos?
    I have seen Screen Studio, which is fantastic but only available for MacOs

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      I do all the zooming in post production with premier pro!

  • @thedelanyo
    @thedelanyo ปีที่แล้ว +1

    This is handy. Thanks Sir

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Glad you think so! You're welcome!

  • @lasaucissemasquee4421
    @lasaucissemasquee4421 ปีที่แล้ว +1

    Thanks for the video.
    I'm not a huge fan of passing the message as a query param because it would allow anyone to forge what is gonna be displayed on the page itself.
    Could we do it differently ?

    • @minnow1337
      @minnow1337 ปีที่แล้ว +1

      I thought the same. Maybe you could write your own custom error hook that allows you to pass additional parameters in the response and persist them through client side routing or localstorage. Or maybe the up and coming view transitions api will help.
      The easiest and maybe most secure fix though might just be to cryptographically sign the redirect url on the server and append the token to it, then validate that token against the redirect url in the page server load.

  • @AB-gx5zj
    @AB-gx5zj ปีที่แล้ว +1

    Nice explainer!

  • @NotAllHeroesWearCapes-101
    @NotAllHeroesWearCapes-101 ปีที่แล้ว +2

    always impressive content

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +2

      Thanks for taking the time to comment on so many of my videos, I appreciate you!

  • @matthewpaquette
    @matthewpaquette ปีที่แล้ว

    This video is exactly what I needed. How did you do that??

    • @matthewpaquette
      @matthewpaquette ปีที่แล้ว

      My form action is overriding my url params. ex. "/login?log_in" Any thoughts on a solution?

    • @matthewpaquette
      @matthewpaquette ปีที่แล้ว

      my solution was to put the redirectto into the forms action="?/login&{data.fromUrl}"

  • @blucky_yt
    @blucky_yt ปีที่แล้ว

    I feel like the redirect message in the url is a bit too open, cause anyone can construct a link with instructions to go on a different website for malicious purposes and it would just be displayed on the webpage, I feel like that should be passed in a way that could only be sent from the server, like headers

  • @abdikarimissa5428
    @abdikarimissa5428 4 หลายเดือนก่อน

    I am new to the svelte/sveltekit but with the background of react/newxjs, I liked this video, it is very informative and the subject is explained well...
    I would like to see full authentication app {login credential, oauth, verification, two factor, and so on using sveltekit, lucia, tailwindcss, prisma, postgress such as neon)
    thank you.

  • @omomer3506
    @omomer3506 ปีที่แล้ว

    So events is a session or local storage?
    Didnt have a use case yet so didn't reas the docs

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Event is the RequestEvent. Locals is a custom object set by the `handle` hook that can be access from other server-side functions. I have a video on hooks if you want to learn more.

    • @omomer3506
      @omomer3506 ปีที่แล้ว

      @Huntabyte nice thanks alot, all your vidz are informative

  •  ปีที่แล้ว +1

    Nice, but... what's happens with a named action in login server file ? On my side, search params are "overwrite" by action's name, no ?

    • @pmj_studio4065
      @pmj_studio4065 ปีที่แล้ว +1

      I think in that case you can still put the redirect URL in the search params. Or in a hidden form field if you can't. However, I'm not sure if say POST actions should be automatically replayed after login 🤔

    •  ปีที่แล้ว

      @@pmj_studio4065 I succeeded by adding the parameter after the action name in the action attribute, like this: action="?/handleLogin&redirectTo=/toto"

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      Yeah it could be passed as a second param to the `action` if you wanted! I do this quite often.

    • @stokkmo
      @stokkmo ปีที่แล้ว

      @@Huntabyte How do I pass this as a second param to the action?

    • @stokkmo
      @stokkmo ปีที่แล้ว

      @@Huntabyte Further to my last I did manage to get this working on a named action by getting the search param from the page store and then setting it to the action parameter inside the form enhance function, but not sure if there is a way to do it without using enhance?

  • @maulanaiman4448
    @maulanaiman4448 ปีที่แล้ว +1

    the github link for the source code is not found..

  • @JoyofCodeDev
    @JoyofCodeDev ปีที่แล้ว +1

    🔥

  • @Rcls01
    @Rcls01 หลายเดือนก่อน

    Only works on the server if you have sessions / cookie. Does not work client side, if you have a token stored in memory, or localStorage.

  • @gorbulevsv
    @gorbulevsv ปีที่แล้ว +1

    It's a cool!

  • @xdega
    @xdega 9 หลายเดือนก่อน

    The concept is good, but not sure I like the idea of passing a querystring param with something so obvious like "redirectTo"... wondering if it would make more sense to encapsulate this in app state, which would be much harder to inject to. Just a thought. But thanks for sharing.

  • @marcoio8742
    @marcoio8742 ปีที่แล้ว

    Nice concept and I think it's important to keep in mind. However, I see a flaw with the design. I prefer also to hide particular UI elements when a user is not logged in. This is s simple example of course, but I would say that showing the account link to a non authenticated user id bad design

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Indeed it is, this design is for demonstration purposes.

    • @Jay-wx6hv
      @Jay-wx6hv ปีที่แล้ว

      How exactly would you implement this? If a logged out user can’t see the url they want to go back to … how will they get back to that url?

  • @jmrumble
    @jmrumble ปีที่แล้ว

    I see a potential bug. Try going to /account?update=true&message=bleh … which message will win? Will the message part of the query string be included on the redirect back to account?

    • @pmj_studio4065
      @pmj_studio4065 ปีที่แล้ว +1

      Yeah I think you would get redirected first to /login?redirectTo=/account?update=true&message=bleh, and then to /account?update=true - to make that work correctly you need to escape redirectTo with encodeURIComponent.

  • @pukarsharma8075
    @pukarsharma8075 ปีที่แล้ว +1

    I'm using the same approach but for server message, instead of adding it to search params, I'm using a library: `sveltekit-flash-message`

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Also a great library made by the creator of superforms :)

  • @ScriptRaccoon
    @ScriptRaccoon ปีที่แล้ว +1

    Very cool tip! This is how a convenient login process looks like. It is cool that it is so easy to implement with SvelteKit.
    Just a minor nitpick: it is a bit "ugly" that the message is passed as a URL parameter. It would be nice to have this as a page parameter and sent it somehow during the redirect, just so that it is not visible in the URL. Is this possible?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Hmm, I know it's possible to send via a request body but with the redirect method I'm not sure exactly how that would work. I will have to explore this a bit further

    • @raenastra
      @raenastra ปีที่แล้ว

      Haven't looked into this much, but would it be possible with a store?

  • @stepskop8670
    @stepskop8670 ปีที่แล้ว

    Content from URL passed directly to document? That smells like some juicy XSS

  • @mpiorowski
    @mpiorowski ปีที่แล้ว

    Amazing help and work :) Great stuff. One suggestion, because Your videos are like a perfect little tutorial / docs, maybe name them a little bit more informative like `Redirect to Protected Route after Auth` ? Later it will be very easy to find what You need :)

  • @RenCode
    @RenCode ปีที่แล้ว

    🔥 🔥

  • @chazzwhu
    @chazzwhu ปีที่แล้ว

    Would you put this in a hook or layout to prevent re-writing for every protected page?

  • @maskman4821
    @maskman4821 ปีที่แล้ว +1

    This is a valuable lesson, very useful, thank you Hunter, btw can you make a updated Sveltekit + Supabase auth + route guard turorial, Supabase has changed auth api/functions/expressions a lot these days, and also if possible make a chat app with Sveltekit + Supabase realtime, I think people want to have tutorial in this topic 🙏😘🤗🤓😎👍

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Already in the plans :)

  • @baixartv
    @baixartv ปีที่แล้ว

    I've watched your other video about protected routes and I think this way of protecting is not secure? Things have changed?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +2

      This is in a +page.server not a +layout.server, therefore it is secure 👍

    • @stefandevo
      @stefandevo ปีที่แล้ว

      @@Huntabyte would mean you have to check this in every page then? Hooks the best place to check, no?

  • @cholasimmons
    @cholasimmons ปีที่แล้ว +5

    UX designers don't get enough credit for all the migraines they save humanity from 😎

  • @mohabedr5030
    @mohabedr5030 6 หลายเดือนก่อน

    clickbait title, we all know this information.

  • @SebastianSastre
    @SebastianSastre ปีที่แล้ว

    But why using the load at all? Why wouldn't be better to do it in a hooks.server.ts handler?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      I hate to say it, but it depends. They both accomplish the same thing, this is just a simpler way to add granular control on a per-route basis. For example, on form actions for unauthenticate users I don't want to throw a redirect, I'd want to return an error. There are ways to accomplish this by accessing the request method via the hooks though if you prefer that.

  • @Thomas-pg1xi
    @Thomas-pg1xi ปีที่แล้ว

    I have a suggestion to make the message url safe use: `/login?redirectTo=${fromUrl}&message=${encodeURIComponent(message)}`