How to create Protected Routes and Authentication with React Router V6 2023

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ก.ย. 2024
  • Download the code here - github.com/Kod...
    In this video, we'll be learning how to create protected routes and authentication with React Router V6. Private routes allow us to control how specific paths are accessible, while authentication provides a secure way to ensure only authorised users can access our content.
    By the end of this video, you'll be able to create private routes and authentication in React Router V6, ensuring that your users are protected while they access your content
    Using React Router V6 we'll put together an App that has:
    1. Authentication wrapper using useContext and useReducer
    2. A custom component to build our routes and menu options
    3. A login page that calls a login function from our Auth Wrapper
    4. Dynamically build and set our menus and routes depending on the user's status
    #reacttutorial #reactrouter #reactjs

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

  • @vPopovPHX
    @vPopovPHX 7 หลายเดือนก่อน +13

    This video is just pure gold! Straightforward, concise, without any useless information. Amazing!

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

      Glad you enjoyed it!

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

    This video was exactly what I needed!! I've been confused with authentication and the bunch of different ways to do it and a video that cuts all the extra stuff and shows you the basics of how it works was perfect. Thanks so much, hope you make more videos was sad to see this was your latest on in a year.

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา +2

      thanks for the comment. More coming in October now I have more time

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

    You deserved more subscribers. You are a good example on what the developers should follow. I haven't followed yours but I created something that is similar and in tsx.

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

      I appreciate that!

  • @theonlymaster9374
    @theonlymaster9374 2 หลายเดือนก่อน +1

    This is a super useful video!!!, went through it, understood how everything is done, great video!!!

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      Thanks for the comment.

  • @DankoKralski
    @DankoKralski 11 หลายเดือนก่อน +2

    Amazing video! I love how there are people like you that go straight to the point and explain in plain English. Thanks a lot for the video! PS: Better teaching methods than my university (will definitely suggest to my friends)!

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

      Really appreciate the feedback!

  • @AIwithChinmay
    @AIwithChinmay 3 หลายเดือนก่อน +2

    straight and easy to understand thanks for the video.

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      Thanks for giving me that feedback

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

    Excellent content!!!!! Thank you very much 🤓 I would love to see another tutorial with your implementation of local storage.

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

      Great suggestion!

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

      @@kodieCode it looks like maybe you're not making these videos anymore, but consider this comment as another vote for a video on the local storage

  • @JasonJamesMoore
    @JasonJamesMoore 27 วันที่ผ่านมา +1

    Is it possible to work things this way using createBrowserRouter. Interested to understand this more, and see if I can use child routes with this kind of setup.

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา +1

      It is, yes, React Router has moved on a bit since this tutorial but it's still relevant.

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

    your content is amazing seriously. i learned so much from this single video of yours,

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

      Awesome, thank you!

  • @vaibhavdani4470
    @vaibhavdani4470 2 หลายเดือนก่อน +1

    Gold Gold Goldberg !! Happy to find this channel !!

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      Appreciate the comment.

  • @georgekopadze791
    @georgekopadze791 10 หลายเดือนก่อน +1

    Good luck with your channel. Liked your content

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

      I appreciate it!

  • @retrabaud-it
    @retrabaud-it ปีที่แล้ว +1

    yo bro
    thank u sooo much, i every day look yt, and try to learn something new, and u speak very easy for me! (sorry for my eng)
    gl bro at ur way!

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

      Thanks for the kind comments!

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

    I enjoyed the video and your accent, as well as learned from you

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

      Thanks for the feedback!

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

    How do we add google auth to this?

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

      I'm planning to do a video on various third party Auth libraries including Google very soon.

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

      @@kodieCode Thanks!

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

      @@kodieCode waiting for the video man!!

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

    Thank you! The map part is genius

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

      Thanks for feedback

  • @FreddyF1977
    @FreddyF1977 11 หลายเดือนก่อน +1

    Nice, simple, to the point.
    Thanks!

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

      Glad it helped!

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

    Cool video! Simple and well explained! It would be very helpful to see an implementation with local storage as well as role-based authorization. Regardless, thanks for the awesome content!

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

      Great suggestion!

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

    can you help me to do same thing in next.js,
    i have to do with user role based , their tho 2 user role , so how to do in next.js with typescript?

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      I don't use next.js as it's best IMO for websites and I'm mainly working on backend and admin panels

  • @IXBoyI
    @IXBoyI 11 หลายเดือนก่อน +1

    why on refresh it does not hold the private links? It resets and I have to click on "login" again so they pop up

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

      This is because in that tutorial we are not storing that the user is authenticated in the localStorage or a cookie. This means a refresh of the page restarts the state. I am working on a video to expand on this authentication method and show how to start storing user details including if the user is logged in so that a returning user might be automatically authenticated

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

    Great content. Please release a video about refresh and access tokens

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

      Thanks for the suggestion. I'll be working on that video soon

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

      Hey @@kodieCode any update?

  • @alexmuiruri502
    @alexmuiruri502 9 หลายเดือนก่อน +2

    Hi @kodie,
    Great content here. However, I'd prefer to show the login page if a user tries to access a private page

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

      This is easy enough to do with creating a default route pointing at the login page. That means any route not created based on their status, will show that page instead.

  • @dchudziak
    @dchudziak 29 วันที่ผ่านมา +1

    Isn't this method easy to bypass? User can easily change ahte isAuthenticated state or isPrivate consts. Is it okay to protect client routes like this and just protect private data on the backend?

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      In theory. the video was meant to help people that were struggling with the exact structure of routes and private content. There are many other things that should be used for security

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

    i love it great knowledge been passed here, thanks a lot

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

      My pleasure!

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

    I suggest that you make code along tutorials, with more catchy thumbnails. Your content is great, you will grow fast.

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

      Thanks for the tip

  • @natnaelhailu-com
    @natnaelhailu-com 11 หลายเดือนก่อน +1

    Thanks for the helpful tutorial!!!

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

      Glad it was helpful!

  • @alonsoestevam
    @alonsoestevam 4 หลายเดือนก่อน +1

    Why are the extension files `.js` instead of `.jsx`?

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

      Doesn't matter it's a framework

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      It won't make an difference.

  • @alexandrejunior4775
    @alexandrejunior4775 8 หลายเดือนก่อน +1

    It's amazing! thank you very much!

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

      Glad you like it!

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

    can you make a new video on how not to make user logout whenever the page refresh?

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      It's as simple as looking up the user details from the localStorage on a page reload.

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

    Great and concise 👍

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

      Glad it was helpful!

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

    How can I Implement roles of users?

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      You would just need to add additional layers to the user object and define that in the routes

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

    This only dynamicly shows the menu items. If a user is not logged in it still can go to the private route by typing it in. There is no redirect.

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

      The routes are only accessible when logged in as the routes are only defined when authenticated therefore not accessible. If a user not logged in tries to directly access a private route they will be shown the default controller

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

      Wow never thought of setting it up like that. Makes absolute sense. An aha moment for me. Thanks for the reply.

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

      No problem. The great thing about an overall Auth controller with useContext is that you can make just about anything easily conditional on the user being logged in.

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

      But what If the user enters to that route from a different tab, he is logged in but since the state of user logged in is only on the other tab, now he wont have access? @@kodieCode

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

      This is okay but how can we show 404 page and not authenticated user according to condition.

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

    Nice ❤

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      Appreciate it!

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

    Please I need a tutorial on how to use localstorage with this

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      This was with LocalStorage. I'm working on a JWT version

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

    heyy kodie how about make a simple tutorial that is in ts not js and handle auth via jwt cookie not token to be more secure then use R.R.D v6 to protect the routes

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

      Great suggestion. Thanks for the feedback.

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

      ​@@kodieCode
      Btw remember my words your channel will grow super fast cause everything is good the audio the video's quality and also the content keep up the good work

  • @investia_tech
    @investia_tech 9 หลายเดือนก่อน +1

    Good work!

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

      Thanks!

  • @levinmwanganyi3929
    @levinmwanganyi3929 5 วันที่ผ่านมา

    Thank you

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

    Really nice video, Thankyou.

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

      Glad you liked it!

  • @zstudio6127
    @zstudio6127 10 หลายเดือนก่อน +1

    thanks you for this tut

    • @kodieCode
      @kodieCode  9 หลายเดือนก่อน +1

      Welcome 😊

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

    Thank You so much!!!

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

      You're very welcome!

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

    Great content, thx!

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

      Thanks for the feedback

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

    yes pls do that user key and session expire thingi we do want it

    • @kodieCode
      @kodieCode  9 วันที่ผ่านมา

      It's possible to add a user lookup to the database on page load but I will add more to this soon

  • @goffardeveloper2056
    @goffardeveloper2056 10 หลายเดือนก่อน +1

    how to add role user sir

    • @kodieCode
      @kodieCode  8 หลายเดือนก่อน +1

      You would need to add an additional object against the user to define their role and then load the routes depending on what that role is allowed to access.

  • @ankoh69
    @ankoh69 11 หลายเดือนก่อน +1

    rooutah

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

      Thanks

  • @PeriklesPeriklesoglu
    @PeriklesPeriklesoglu 11 หลายเดือนก่อน +1

    thank you

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

      Welcome!