The Right Way to do Auth with the Next.js App Router!

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

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

  • @filipesommer8253
    @filipesommer8253 7 หลายเดือนก่อน +10

    Great guide! two small things:
    1. the middleware.ts file works based on being in the same directory as the app/ folder, not necessarily the root of your project.
    2. supabase content in general should push users to doing auth checks (incl. redirecting to the login page!) using middleware instead of showing that redirect example in layout.tsx for security reasons in the way nextjs layouts work

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

      Why 2?

    • @FloCloud9
      @FloCloud9 5 หลายเดือนก่อน +1

      Agreed since the middleware kicks in before anything else on nextjs, Nice one

  • @Maraciuca_RCK
    @Maraciuca_RCK 7 หลายเดือนก่อน +20

    the real ones, know this was posted yesterday

    • @omegahawkeye
      @omegahawkeye 7 หลายเดือนก่อน +3

      Literally thought the same just know. Already saw this stuff. Also read the tutorial for it already

    • @JonMeyers
      @JonMeyers 7 หลายเดือนก่อน +1

      We were running an experiment. You passed the test!

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

    You just don't know how useful these are

  • @golodiassaid4879
    @golodiassaid4879 3 หลายเดือนก่อน +5

    How many people use github to login? No one except developers basically. I don't know why tutorials often go for the least relevant example.

  • @piotrra6281
    @piotrra6281 6 หลายเดือนก่อน +2

    Thanks for the great tutorial Jon (as always).

  • @kvetoslavnovak423
    @kvetoslavnovak423 7 หลายเดือนก่อน +1

    Thank you. Is there any estimations when SSR beta ends and we may use it in production?

  • @matthaigh27
    @matthaigh27 7 หลายเดือนก่อน +3

    After /auth/callback finishes and I redirect, user is null in the subsequent middleware call

  • @FernandoHernandez-pz9im
    @FernandoHernandez-pz9im 3 หลายเดือนก่อน

    way too clear, better than the documentation, thank you so much!

  • @himesh_89
    @himesh_89 7 หลายเดือนก่อน +1

    Can we cover more react stuff with supabase?

  • @BJRNextCode
    @BJRNextCode 7 หลายเดือนก่อน +2

    Why you alaways don't upload videos on Supabase? I am big fan of you brother. you are such a good teacher

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

      Thanks! Glad you’re enjoying the videos! 🙌

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

    Careful here. If you use the Auth inside the root layout, it will force all of your static pages into dynamic.

  • @janglad9136
    @janglad9136 7 หลายเดือนก่อน +8

    Sorry but handling auth in layouts in Next is not secure at all and IMO reason for this video to be redone. Send a get request to the "protected" page through something like Postman with an RSC header and you'll be able to see the protected content in there.

    • @Tanner-cz4bd
      @Tanner-cz4bd 7 หลายเดือนก่อน +1

      thats crazy

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

      Are you positive that is the case, even in this example at 12:40 where middleware is used?
      I'd like to here the @supabase response too :)

    • @janglad9136
      @janglad9136 7 หลายเดือนก่อน +2

      @@ewwitsantonio yes. You could put a redirect("/login") without any conditionals in the layout and the RSC payload would still include the protected page when sending a GET request. Jolly Coding recently did a video explaining it a bit more in depth, but it could also be helpful to read up on how RSC are rendered and sent to the client (something I should do too tbh lol, definitely don't fully understand it yet either).

    • @dawidwraga
      @dawidwraga 7 หลายเดือนก่อน +3

      If you use middleware auth check then you are safe and the layout auth check is redundant in this video

    • @janglad9136
      @janglad9136 7 หลายเดือนก่อน +1

      @@dawidwraga correct, middleware for protecting pages is the way to go.

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

    Really nice one !! Can you go further with customs claims to set customs roles ? 🙏🏼

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

      That would be a great video! Will add it to the list! 🙌

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

    is it possible to protect API routes/endpoints with Supabase auth? Let's say for example you have an API route that calls openai GPT completion and you don't want everyone to be able to hit that endpoint and cost you money?

  • @tw04
    @tw04 7 หลายเดือนก่อน +4

    But cookie based authentication forces to break SSG of Next.js, because it requires server run on every page render.. You should avoid using it on public pages.

    • @janglad9136
      @janglad9136 7 หลายเดือนก่อน +2

      yes apart from the big security flaw in doing it like this, this is indeed another reason to use middleware for protecting pages.

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

      Yeah, for SSG, or public pages in general, the user does not exist and build time so there are no cookies 👍

    • @janglad9136
      @janglad9136 7 หลายเดือนก่อน +1

      @@JonMeyers might be missing something but if you're calling cookies in a route (or through a function that that route uses) it can not be statically generated at all right? So you'd only be able to SSG public pages with this approach.

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

    Can you show us how to do supabase auth with prisma?

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

    if i want to get user details / auth from a client component do i have to create a separate supabase client?

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

    Thanks a lot for this because the docs on your nextjs middleware guide is different and i could'nt make it working until now.

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

      Agreed! The docs are out of date. I spent hours trying to get auth setup. This video saved me.

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

    Hey, I have a question. I have deployed an application that does not need any authentication. There is a function inside the code that makes some calculations upon the text that comes from the client by the text input form. Since this function runs on the client side, it exposes the supabase url and the key on console. How can I handle this problem? Thank you in advance.

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

      I am not an expert but looking at it from a high level, you need to make all the calls to supabase from a middleware or server component, and just call those from your client.

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

      @@pablorodriguez196 Thank you for the response, this function requires the input that comes from the client therefore it can not be a server component, but as you said, middleware might be the solution

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

      @@odogru77 How about create a server action and call that action inside your function?

  • @drancerd
    @drancerd 7 หลายเดือนก่อน +1

    Fantástico! Y como se hace?

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

    Why is it reuploaded??

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

      It was a test! You passed!

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

    why reupload?

    • @JonMeyers
      @JonMeyers 7 หลายเดือนก่อน +1

      It was an experiment. You passed the test! 💯

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

    google provider doesn't redirect to the provided url - redirectedUrl. instead, it redirect to home page with the code, since exchangeCodeForSession(code) is in the route.ts of auth/callback ... this doesn't work

  • @Neqxy
    @Neqxy 3 หลายเดือนก่อน +1

    bruhh, now deprecated

  • @lostinthenarrativve
    @lostinthenarrativve 5 หลายเดือนก่อน +1

    This guy's so annoying and despicable.

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

    And this is how to make things more complicated lol. come on..you can create better solution to handle the ss auth...make auto linking between sessions and jwt

  • @budiman-kr5ug
    @budiman-kr5ug 7 หลายเดือนก่อน

    supabase.auth.onAuthStateChange not triggering when i do login using server action and login the user at the server then redirect user to protected page. i listen auth change in my main layout (client component). public & protected page using the same layout (client component)