Protected Routes in React using React Router

Поділитися
Вставка
  • Опубліковано 12 лис 2024

КОМЕНТАРІ • 213

  • @rogerodipo3499
    @rogerodipo3499 5 років тому +144

    You, sir, have a soothing voice.

  • @adeleke5140
    @adeleke5140 Рік тому

    Amazing 4yr old video that's still relevant today. Thank you for making this.

  • @suketupatel174
    @suketupatel174 4 роки тому +39

    if anyone is using routes with children:
    e.g.

    then do this:
    import React from "react"
    import {Route} from "react-router-dom"
    const ProtectedRoute = ({children,...rest}) =>{
    return (

    {children}

    )
    }
    export default ProtectedRoute;

  • @olendaelhousen1552
    @olendaelhousen1552 5 років тому +19

    Thanks - this was really helpful. I like that you kept everything simple and didn't get overly complicated, that made it very easy to pick up the concept and start coding.

  • @Elliewebd
    @Elliewebd 2 роки тому

    Now, I really didn't expect this to end up being a perfect ASMR video

  • @vishwasr8446
    @vishwasr8446 3 роки тому +3

    Mention of "withRouter" saved my live🙏🏻🙏🏻🙏🏻🙏🏻

  • @stevendiaz7374
    @stevendiaz7374 5 років тому +24

    omg i've been looking this for 2 hours, thank you a lot :DDD

    • @slmshady539
      @slmshady539 3 роки тому +1

      I spent entire 2 days looking for this.

  • @morapedimasima9753
    @morapedimasima9753 4 роки тому +3

    10/10 honestly, great video and explanation!

  • @simonkalu
    @simonkalu 5 років тому +3

    Thanks, Bruh. You did justice to this topic. Cleared all doubts

  • @Softwareengineeringdev
    @Softwareengineeringdev 6 років тому +7

    You are very good teacher

    • @nickkarnik9688
      @nickkarnik9688 6 років тому +1

      Thank you so much! I really appreciate the kind words.

  • @ankitavay1947
    @ankitavay1947 4 роки тому

    Sir, You pick an easy way to teach , I like this, Few can do this.
    It is very hard to teach simple.

  • @bidhyapokharel4687
    @bidhyapokharel4687 4 роки тому

    Thank you so much for such a beautiful Video....Had not slept since last night since I was trying to do it by reading Documentations...You're so great....Wish I had explored this thing before!!

  • @recepozturk6711
    @recepozturk6711 4 роки тому

    smooth voice and clear explanation.thanks

  • @ricardinhofreestile
    @ricardinhofreestile 4 роки тому +1

    the best tutorial on React Router I saw... nice broth

  • @MrEdward2553
    @MrEdward2553 4 роки тому +1

    Thank u from Colombia. It was very helpful for me

  • @christianalejandroherrejon6819
    @christianalejandroherrejon6819 4 роки тому +1

    I am grateful

  • @TS-qf2km
    @TS-qf2km 5 років тому +5

    You solved my problem. Thanks so much!

  • @robindalehayden
    @robindalehayden 3 роки тому

    Very clear explanation, thanks

  • @ashishchiluka8912
    @ashishchiluka8912 4 роки тому +1

    Best tutorial I saw for react router can u make video on design patterns in react

  • @erichcasagrandeperusso5493
    @erichcasagrandeperusso5493 4 роки тому +2

    Really efficient tutorial man! thanks for your help, cheers from Brazil ;)

  • @chrisdanevalla3293
    @chrisdanevalla3293 5 років тому +7

    Just like what I need

  • @nagarajukp7820
    @nagarajukp7820 4 роки тому

    Really awesome video
    Very simple and subjective

  • @shibah9345
    @shibah9345 3 роки тому

    Quality content right there! Thanks!

  • @syednoohu8692
    @syednoohu8692 4 роки тому +2

    New to ProtectedRoute...sorry if my question is naive.
    Q : Arguments in the ProtectedRoute we destructing {component : Component, ...rest}, my doubt is
    why not the assignment is other way around, i.e. {Component : component, ...rest}, since we are passing the props as 'component' only, not 'Componenet'
    Thanks

    • @ammarfy
      @ammarfy 4 роки тому

      const { component } = props ; // how you normally do to destructure an object
      const { component: Component } = props; // Destructure an object then give it a new variable name, Component
      In React every component name should always start with Uppercase because of JSX rules. Hence, assigning 'component' to Component is needed.

  • @Dipenparmar12
    @Dipenparmar12 3 роки тому

    Good one. simple and superb .

  • @vickyvirat5166
    @vickyvirat5166 5 років тому +1

    superb sir, you explained it very well

  • @Jwaana
    @Jwaana 4 роки тому +3

    I really like this tutorial. It's perfect ! Thank you very much ! :)

  • @prathameshchavan3953
    @prathameshchavan3953 3 роки тому

    Thanks, good explanation.

  • @kingofgods-w3t
    @kingofgods-w3t 4 роки тому

    Thanks you very much for the video. i got stuck with this issue and you helped me solve it

  • @JoshAmore31
    @JoshAmore31 4 роки тому

    Straight to the point. Thanks!

  • @kyoungd
    @kyoungd 5 років тому +1

    Simple and elegant. Nice.

  • @yanurmal
    @yanurmal 3 роки тому +1

    Great tutorial, easy to follow. Btw, what font do you use?

  • @tube-rp1nb
    @tube-rp1nb 2 роки тому

    thanks this was really help full .thanks alot

  • @Ixtinkt
    @Ixtinkt 3 роки тому +8

    The last thing that I would like to see is try to get access to /app when you logged out

    • @iigho
      @iigho 3 роки тому +1

      yup, I m too :(

    • @bringinglife2
      @bringinglife2 3 роки тому +1

      Me too. I was literally waiting for that! 😅

  • @rafihasan4061
    @rafihasan4061 3 роки тому

    The Video was really helpful........Thanks a million..

  • @bharathynasar3338
    @bharathynasar3338 3 роки тому

    Great...it worked. really helpful. thanks

  • @ahmadyahya1919
    @ahmadyahya1919 3 роки тому

    nice tutorial, thanks

  • @hileamlakmulugeta5959
    @hileamlakmulugeta5959 4 роки тому

    This is exactly what I needed

  • @SyedShujashah
    @SyedShujashah 2 роки тому

    3:00
    Thank you for these awesome videos, Here's a tip for best practices for new JS or react enthusiasts.
    When you're just returning some JSX from a component, you can go ahed and remove curly braces and 'return' syntax such as:
    ```
    const Hello = () => ( Hi \> )
    ```

  • @faradniftaly8893
    @faradniftaly8893 4 роки тому

    Awesome job, keep the good work ...

  • @muhriddinxusniddinov9015
    @muhriddinxusniddinov9015 2 роки тому

    it's amazing tuturial

  • @otwoc
    @otwoc 6 років тому +1

    Thanks for sharing this project! good luck for you.

  • @ytwatcher181
    @ytwatcher181 3 роки тому

    Thank you so much for amazing explanation!

  • @zakiarahmoune7458
    @zakiarahmoune7458 2 роки тому

    Very useful .. THanks

  • @md.mainuddin8211
    @md.mainuddin8211 4 роки тому

    Just awesome!!! Thanks a lot.

  • @Max-kg9sl
    @Max-kg9sl 4 роки тому

    Thanks men, thats what i have been searching for :)

  • @AnteZivkovic
    @AnteZivkovic 3 роки тому +2

    What if the routes I want to protect use the render prop because they render components that have props themselves? I get errors using this method.

  • @pasindupansilu6046
    @pasindupansilu6046 3 роки тому

    Thank you man. You saved me ❤️

  • @JsWithRaj
    @JsWithRaj 4 роки тому

    It really helped a lot, thank you!

  • @yaofab7284
    @yaofab7284 2 роки тому +1

    HOw is protected if anyone can modify the frontend source code?

  • @zoeygrimes07
    @zoeygrimes07 4 роки тому +1

    thanks dude!

  • @theethicalh3707
    @theethicalh3707 3 роки тому

    thank you very much sir

  • @nassar_productions
    @nassar_productions 4 роки тому +3

    importing auth in different js files is creating instances of them. How are you managing to set login= true in one page, then importing auth in another page and the value is still true??

    • @nguyenhanh9479
      @nguyenhanh9479 3 роки тому

      this is just example, in real project this will be handle by backend.

  • @prezesgustaf7268
    @prezesgustaf7268 4 роки тому +1

    So where should we read the data? Store user object in LocalStorage and then read data from it? Its good practise? so what if someone manualy change localStorage and change e.g role from user to admin?

  • @kumargagare6154
    @kumargagare6154 4 роки тому

    Thanks it really helped me

  • @kawding_sikul2629
    @kawding_sikul2629 4 роки тому

    Very usefull and short one

  • @256k_
    @256k_ 6 років тому +10

    Auth is something that's been really confusing to me to be honest, im very familiar with react and JS and everything in this video but i would relaly appreciate if there could be more in depth videos like this (which was great) about auth itself, different ways of handling auth in an application and whether write your own auth like you did or maybe use a package to handle it?

    • @nickkarnik9688
      @nickkarnik9688 6 років тому +5

      Thanks, @fayekhelmi! I wanted to break the videos down to focus on specific areas. Originally, I planned on creating the server side using PassportJS and wiring everything, but that meant getting into a bunch of other things at the same time. I might do a combined video showing everything end-to-end in the future . The next video might just focus on integrating PassportJS.

    • @millexuan
      @millexuan 6 років тому

      @@nickkarnik9688 any quick hints on how to handle the passport redirect callback from the Express server in the React auth file? Great video!

    • @yousof77
      @yousof77 6 років тому +2

      The Net Ninja has a great series on Passport.JS and OAuth if that's any help.
      ua-cam.com/video/sakQbeRjgwg/v-deo.html

  • @jeyankarthikeyan
    @jeyankarthikeyan 4 роки тому +1

    Is it protected But how , One can change the the state of authenticated from developer options

  • @PeterPalmerSoftware
    @PeterPalmerSoftware 3 роки тому

    thanks, simple solution ✅

  • @TheShubham67
    @TheShubham67 3 роки тому

    man you are awesome!

  • @heunsigjo7484
    @heunsigjo7484 4 роки тому

    You are a genius

  • @piriyadarrshans988
    @piriyadarrshans988 3 роки тому

    Thanks, sir its very usefull

  • @hafsaelkoumikhi9200
    @hafsaelkoumikhi9200 2 роки тому

    thank you so much

  • @RN-ru2rj
    @RN-ru2rj 5 років тому

    Excellent info.. It's a gem.👍 Thanks a ton 🙏

  • @drlucky5474
    @drlucky5474 4 роки тому

    Good Tutor sir. Thank....

  • @preparedmedusa2544
    @preparedmedusa2544 4 роки тому

    seems like you were using some device with butterfly keyboard, so annoying. I am suffering too) and ty for this short and useful explanation btw

  • @zeeshanalikhan8258
    @zeeshanalikhan8258 4 роки тому +1

    how can i set role based routes for admin and normal user??

  • @gaos94
    @gaos94 6 років тому

    Really good, thanks!

  • @mertcancetinkaya7648
    @mertcancetinkaya7648 4 роки тому

    Thank you for this video. I want to ask a question. Is it higher order component?

  • @obedsayyad
    @obedsayyad 3 роки тому +1

    this function expires the login while refreshing please help me how to set session logout time

  • @ArshdeepSingh-vw2uj
    @ArshdeepSingh-vw2uj 4 роки тому

    I wish I could press the like button 100 times 😄

  • @lilpece1950
    @lilpece1950 5 років тому

    you're a fucking god for this. thank you.

  • @ykk522
    @ykk522 3 роки тому

    what is the IDE used in the video? and which is the recommended community IDE's available

  • @vishwasr8446
    @vishwasr8446 3 роки тому

    🙏🏻🙏🏻🙏🏻 thanks and thanks and thanks🙏🏻🙏🏻🙏🏻

  • @shanindrakumar9944
    @shanindrakumar9944 4 роки тому

    great vid

  • @oasane
    @oasane 3 роки тому

    This is been very helpful, cleared few questions I had. Thank you.

  • @josephsam2145
    @josephsam2145 3 роки тому

    Thanks man....

  • @sergeymigel4680
    @sergeymigel4680 4 роки тому

    Thank you!

  • @davis4010
    @davis4010 4 роки тому

    I suppose in real life situation that auth.isAuthenticated call should be made on every protected view attempt? Or just save it in state? I tried to do axios get and confirm the jwt token is valid, yet it seems somehow that check fails - undefined is returned when protectedrout component waits for that reply. Hmm, a little bit lost there.

  • @akashchokshi2283
    @akashchokshi2283 3 роки тому

    I tried solving a use case using this approach but could not solve it. Have 2-3 open routes, 2-3 protected-routes, and a wild card route at the end of router switch. The protectedRoute being a component renders everything and starts executing whatever logic is inside it. For example, I hit a wild card route /asd and I want to redirect all wild card route to /home. Since protectedRoute component comes before the wild card route in App.js/.tsx the wild card route is never hit and I am redirected to the else condition logic inside my protectedRoute.

  • @ErickG
    @ErickG 3 роки тому

    If you are using jwt, can you control access that way (like if they have admin privileges?)

  • @mohamedalhoony
    @mohamedalhoony 4 роки тому

    thank you

  • @princedikko2472
    @princedikko2472 Рік тому

    this method "push() doesn't work in current version of react, can you please upgrade the video to the newer technologies

  • @arunkaiser
    @arunkaiser 5 років тому +2

    Can Any one help me ? i am getting the below error ----- >
    ./src/protected.route.js
    TypeError: Cannot read property 'forEach' of undefined

    • @dragonminja246
      @dragonminja246 5 років тому

      I have the same issue when copying it over to react

  • @cosmin4437
    @cosmin4437 4 роки тому

    what about if i want to write it like this with the new API :
    instead of
    how should component be?

  • @gauravoberoi962
    @gauravoberoi962 4 роки тому

    what about if i am signed in, and i click on browser back button? will it shoow me the signed in page? if yes, then then it seems wrong. if no, then how to do it?

  • @amirsohel9260
    @amirsohel9260 4 роки тому

    Can you please tell me which code editor you are using ?

  • @akhiljayachandran3738
    @akhiljayachandran3738 4 роки тому

    hi , can you put some video related to how to use pagenotfound component and which will not impact private route.

  • @emrahkorkmaz3991
    @emrahkorkmaz3991 4 роки тому

    the intro voice is 100% similar to sale commercial ads voice :)

  • @maheshtak5120
    @maheshtak5120 5 років тому +2

    I would like to know, what happenes if without clicked on login i tried to visit app page through url...
    Will it visit to app page or it will give 401 error as wrote in Protecting Route ?

    • @TheAqui2000
      @TheAqui2000 4 роки тому +2

      I know it's a year later, but for anyone wondering now it would just redirect you back to whatever page you set it to in your private route . So if you go a protected route url without logging in, in your private route component use the Redirect component to take the user back to the main page if not logged in.

  • @TheAlyZidan
    @TheAlyZidan 5 років тому +5

    Bro Good tut ;) !
    this step of passing a component @ 08:55 was somehow confusing , can you please amplify this step :)

    • @alextkd2003
      @alextkd2003 5 років тому +4

      it is destructuring, you can use ****** in App.js function; and ***const SecretRoute = ( { Component, ...rest } )*** in protected.route.js. A component have to be named capitalize, so he use the syntax { component: Component } because the father component have the prop name "component". :)

  • @webmintutorials8939
    @webmintutorials8939 5 років тому

    One thing I noticed is that every lesson on React about Protected Routes and user authentication are copied straight up from the reacttraining.com.
    This example doesn't solve any problem on a major application where security and performance it's on account. In a real world scenario, you'll have a server-side authentication flow such as PassportJS with an endpoint (API) where you can make a AJAX request to check if user is authenticated and have access to certain routes.

    • @keviin608
      @keviin608 5 років тому +1

      Yes, because a basic react tutorial for beginners is going to show you how to make an enterprise ready application... 🙄

  • @tvpdomo
    @tvpdomo 3 роки тому

    What names this plugin for dependency management?

  • @MrChealsea007
    @MrChealsea007 3 роки тому

    I am wondering, what will happen if you pass random url name, when you are still logged in. In my code, I couldnot show 404 page not found in such condition.

  • @xuxunguinho
    @xuxunguinho 3 роки тому

    thank you very much for the help, but this only works until you update the page

  • @chessjames
    @chessjames 3 роки тому

    you'd get a million views if you added asmr to the title :p

  • @Osuran_Bogaa
    @Osuran_Bogaa 2 роки тому

    when i was refresh /app profile, thats redirect to homepage. Why? i dont want to redirect from /app to homepage, i want stay in /app after logged in. How can i do it ?

    • @guillaumechiasson1913
      @guillaumechiasson1913 2 роки тому

      app.layout.js , line 11.
      I think this is where you would find your answer.

  • @bilawalali6810
    @bilawalali6810 4 роки тому

    how to use partial components in other component. but not in login component remianing SPA

  • @ianuzzi
    @ianuzzi 4 роки тому

    What VS Code extension are you using for dependency management?

    • @amirovuz
      @amirovuz 3 роки тому

      He was not using VS Code. He was using Sandbox codesandbox.io/s/ol6z72kjy9?file=/src/index.js

  • @kvieres
    @kvieres 4 роки тому

    is it the right way to do this?, there isn't a plugin or something that avoid put this interventions in each part of my code..