Protected Routes in ReactJS - React Router Dom

Поделиться
HTML-код
  • Опубликовано: 3 окт 2024
  • In this video I will teach you guys how to create Protected Routes in ReactJS. With protected routes, you can limit access to certain pages. This is great for an authentication system, where you only want people who are logged in to access a page.
    -
    Please leave a comment on what topic you guys want me to cover next!
    -
    ❤️ Support the Channel: / pedrotech
    📞 Tutoring Session: www.fiverr.com...
    💻 PedroTech Discord: / discord
    -
    Social
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    Website: machadopedro.com
    Twitter: / pedrotech_
    Linkedin: / machadop1407
    Instagram: / _pedro.machado_
    Github: github.com/mac...
    Email: machadop1407@gmail.com
    Tags:
    Protected Routes
    Fetch
    ReactJS Tutorial
    ReactJS and MySQL
    NodeJS Tutorial
    API Tutorial

Комментарии • 159

  • @johnpaulpineda2476
    @johnpaulpineda2476 2 года назад +20

    Whenever i search a specific video about react. I always find Pedro's tutorial much more understandable compare to the others. :)

  • @swardrishtistudio9727
    @swardrishtistudio9727 3 года назад +5

    Trust me your channel is literally underrated.... I mean seriously man you just blew up my mind... keep posting such informative videos...god bless you

  • @FrankUnderwoood
    @FrankUnderwoood 3 года назад +13

    Amazing tutorial man! Clearly explained and straight to the point! I'm new to React and to React Router Dom, and I've been having troubles implementing a working authentication system. This helped me a lot. Thank you so much for making it!

  • @fancyAlex1993
    @fancyAlex1993 3 года назад +2

    It actually works. You actually did a better job than the react router documentation itself.

  • @rayahmed2202
    @rayahmed2202 3 года назад +4

    Thanks friend. Was confused about state in another video, but that was cleared up here.

  • @yhr4052
    @yhr4052 3 года назад +4

    You sir, are a rising star. Your content proved useful in my case. You have my encouragements for your channel.

  • @Dlntck
    @Dlntck 3 года назад +4

    Your videos are always on point, very clear. Thank you !

  • @tarikchuery3261
    @tarikchuery3261 3 года назад +4

    Garotão, parabéns pelo conteúdo! Fera demais a ideia de criar um ProtectRoute!!

  • @reallegend1
    @reallegend1 3 года назад +3

    I really like your way of explaining, hope you and your channel all the best .

  • @sukhvsin2
    @sukhvsin2 3 года назад +5

    Great Content Man!!
    I'm always excited to see your content Keep it up ;)

  • @ferrisosman9460
    @ferrisosman9460 3 года назад +1

    The best explanation on youtube by far

  • @pjguitar15
    @pjguitar15 2 года назад

    Thank you so much. This is probably the simplest and easy to understand tutorial about Protected Routes

  • @brunofunnie
    @brunofunnie 3 года назад +3

    8:06 just a tip, if you have a 'if..else...' statement which return things, you don't need to use the 'else' statement just add a 'return' after an 'if' closure

  • @Lindaine
    @Lindaine 3 года назад +2

    Hey man, I just wanna say that I cannot wait for my semester to be over so that I can watch all of your videos! I have a PERN stack project this semester and It's kicking my ass now because I never learnt React and Node before doing that project.

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Awesome! I am excited to end my semester too to have more time to post video! I am glad you like to watch them!

  • @ilhamadiputra4745
    @ilhamadiputra4745 3 года назад +3

    Nice content, hope your channel get bigger & more subscriber!

  • @bibeksapkota1602
    @bibeksapkota1602 3 года назад +1

    loved your explanation. I hope you will continue providing us more and more videos.

  • @physcript2436
    @physcript2436 3 года назад

    whenever i forgot how to do protected route i always come here hahaha fourth time

  • @itscartmanbrah4856
    @itscartmanbrah4856 3 года назад +3

    Thanks for your work man 💪

  • @SiCrip09
    @SiCrip09 3 года назад +1

    The most elegant way, thank you! 🚀

  • @mingyangli9171
    @mingyangli9171 3 года назад +6

    Great content man. Could you do a video on integrating protected routes into the UI? I'm talking about implementing react-router with nav menu bar, I'm having a hard time achieving this. Cheers:)

  • @Jerrynibba
    @Jerrynibba 2 года назад

    you sir are doing the gods work , thank you so much , cant really thank you enough

  • @codingphoenix6993
    @codingphoenix6993 3 года назад +1

    Wow ♥️♥️♥️ thankyou so much
    What a clear explanation thankyou so much for this video

  • @webdev2851
    @webdev2851 3 года назад +2

    thanks man u saved me.. I am looking for this whole day

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Glad I could help!

    • @webdev2851
      @webdev2851 3 года назад

      @@PedroTechnologies i have some doubts i certain usecases please help me

  • @samyogdhital
    @samyogdhital 2 года назад

    Love your way of explanation brother! thanks for helping me alot!

  • @natanaelsilva5959
    @natanaelsilva5959 3 года назад +1

    bro, a tip would be to try to put your videos with subtitles in Brazilian Portuguese, because I am your faithful follower and with subtitles it would be better. hugs !

    • @PedroTechnologies
      @PedroTechnologies  3 года назад

      Thank you for being a loyal follower! É didicil fazer legenda em portugues mano kkkkkkk porque nao vai automaticamente, eu teria que botar eu mesmo. Mas se tiver qualquer duvida é so perguntar.

  • @gourojusanjaykumar8819
    @gourojusanjaykumar8819 3 года назад

    thank you very much, perfect video i was searching all day

  • @gracesonsydonfernandes7443
    @gracesonsydonfernandes7443 3 года назад +1

    the world needs u

  • @programmerrdai
    @programmerrdai 3 года назад +2

    Great Content keep up the good work

  • @wilsonwg
    @wilsonwg 3 года назад +1

    This video saved a lot of time for me, thx!

  • @kasunjalitha2300
    @kasunjalitha2300 3 года назад +1

    Thank you so much!!! The best explanation 🔥🚀

  • @seyitanoke
    @seyitanoke 3 года назад +1

    Great Video! Very Helpful

  • @jothinayagan.iyyappan
    @jothinayagan.iyyappan 3 года назад +1

    Great explanation. Thanks man.

  • @YolkWorshiper
    @YolkWorshiper 3 года назад +2

    i appreciate this my guy

  • @nixelev4041
    @nixelev4041 3 года назад +1

    In Germany we call you "Ehrenmann"!

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Hahaha just looked at what this means and I appreciate it!

  • @ajaykumar-nx7kb
    @ajaykumar-nx7kb 3 года назад +1

    Very nice video man, thanks for your amazing work, please keep continue on react... 👌👌👌

  • @computinginfo3187
    @computinginfo3187 3 года назад +1

    Sending the state(from), while redirecting, is something most people don't cover. Thank you so much. Jumping to 1 url, every time I reloaded the page... Was really bothering me. Again. Thanks!

  • @bhagabatiprasad2612
    @bhagabatiprasad2612 3 года назад +1

    Thanks bro. I was looking for it. Your instruction skill is amazing. Just make a specific video on withRouter.

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Thank you! Yeah I agree i didn't explain withRouter very well lmao

  • @sanohawa
    @sanohawa 3 года назад +1

    Thank you for this video!

  • @harshilbodara4348
    @harshilbodara4348 3 года назад +1

    i appreciate this my guy and it is very intelignes man

  • @alanhcrdz
    @alanhcrdz 3 года назад

    Thanks, man, for an amazing explanation!

  • @kazi004
    @kazi004 3 года назад +1

    Thanks man, great and useful content, subscribing :)

  • @jcavenue
    @jcavenue 3 года назад

    Great! Thank you so much!

  • @kipkoechtoo9457
    @kipkoechtoo9457 3 года назад +1

    Very helpful. Thank you.

  • @alexdaltsis2941
    @alexdaltsis2941 3 года назад

    Thank you man, you made it very clear :D

  • @f3-faithfitnessfinance
    @f3-faithfitnessfinance 3 года назад +1

    Woohoo awesome!

  • @modoumbye2258
    @modoumbye2258 3 года назад

    Great video professor you safe some souls. Thank you very much but please do a video in the case someone has different navigation menu from different users.

  • @rizaradiarivaldo
    @rizaradiarivaldo 2 года назад

    Great content. Very helpfull Thanks

  • @tabmax22
    @tabmax22 2 года назад +1

    please do again for functional based components

  • @richardsangohil
    @richardsangohil 2 года назад

    Thank you for this video

  • @computerscience1099
    @computerscience1099 3 года назад +1

    nice video man .. keep it up

  • @rizqyhbb
    @rizqyhbb 2 года назад +1

    Thankyouu!! this is amazing tutorial!! could you tell us which react and react router dom version is being used in this tutorial ?

    • @PedroTechnologies
      @PedroTechnologies  2 года назад

      Thank you! This video was made a while ago so I would say it was react router V5 and below.

  • @nikitalistopad2635
    @nikitalistopad2635 3 года назад +1

    thank you, so good guide

  • @drewyoungthomas92
    @drewyoungthomas92 3 года назад +1

    Hey PedroTech, I'm curious about how you do the line cleanup. Is there a keyboard shortcut youre using to stack your code like that? thanks for all the good work! You've been helping me out a lot with my PERN personal project.

  • @ManojSingh-of5ep
    @ManojSingh-of5ep 3 года назад +1

    withRouter is not a hook.. its a higher order component.. Greate video btw

  • @jakakrajnc7538
    @jakakrajnc7538 3 года назад +1

    What the f**k is Google doing with hindering our boy Pedro in moneitzing his great content? Wtf?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Lmaoo im monetized now!

    • @jakakrajnc7538
      @jakakrajnc7538 3 года назад

      @@PedroTechnologies Thank god! Otherwise Wojcicki would have received a very angry letter on her desk 😄 But I'm glad we resolved it civilly. Keep up the amazing content! 😎👌 #pedrotech

  • @rommyarb
    @rommyarb 3 года назад +1

    Thank you!

  • @singhsaheb4603
    @singhsaheb4603 2 года назад

    Thsnk you !!

  • @pgameplay9639
    @pgameplay9639 3 года назад +1

    Thank you!!

  • @davidhernandez9275
    @davidhernandez9275 2 года назад +1

    Thank you! Just one question: is there any change in this workflow with the newest version of React-Router-dom? Or do we still need to do it like in the video? Thanks!

  • @WitlessMean
    @WitlessMean 3 года назад +1

    please someone knowledgeable answer :D.
    few things about the code I don't really understand if anyone would care to explain. First, we pass isAuth, component, and ...rest to the ProtectedRoute. Then, since ProtectedRoute is rendering a Route, we pass ...rest to route. I don't really understand why we need to pass ...rest though, considering that we're using the other props that we didn't pass in this same way, inside of the route (speaking about Component and isAuth). So I'm saying that we use Component and isAuth inside of the Route, but we don't directly pass them there like we pass ...rest, so why do we need to pass ...rest?
    Secondly, with that said, do we even end up using ...rest? We use props in the render method , but not ...rest. Is ...rest and props the same thing here?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад

      Hey, so the reason why I wrote ...rest is not because of possible props but for possible properties and parameters someone might want to put to their component. If you still have some doubts please let me know!

    • @WitlessMean
      @WitlessMean 3 года назад

      @@PedroTechnologies ohhhhhhhhh! that makes perfect sense now actually. thanks so much for the reply, happy to subscribe. awesome vids and explanations.

  • @rockycalo2608
    @rockycalo2608 3 года назад +1

    thanks Pedro

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      Your welcome! Hope you enjoy it!

    • @rockycalo2608
      @rockycalo2608 3 года назад

      @@PedroTechnologies it helps a lot all of your videos some are not yet done that I watched because of code that need to finish activity in school.

  • @aristosxanthus514
    @aristosxanthus514 2 года назад

    Creating a dedicated ProtectedRoute for every component is redundant. Is there a way to pass in all components you want to be protected in one statement?

  • @goodfella9779
    @goodfella9779 3 года назад +2

    Awesome vid bro. Saved me a ton of time. But there's something that's not quite clear for me: why do we need to pass the 'from' key into the Redirect component alongside with pathname? Is it something about redirecting a user to the page he had been trying to access via changing the URL before logging in?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад

      Yeah, so this part is quite confusing. But the idea is that you want to determine that you are redirecting from the previous location and not from the protected route!

  • @MrSuperfra89
    @MrSuperfra89 2 года назад

    how do you not get ncaught Error: [ProtectedRoute] is not a component. All component children of must be a or ??

  • @chaswards
    @chaswards 3 года назад +1

    Great video, but what is stopping someone from changing the state in the browser using React Dev Tools? Honestly curious because I feel like that would be something super easy to do, unless I am missing something?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +2

      Good question! So I focused on just implementing the routing aspect of this. However in theory you would probably need to be authorized and authenticated in the backend. THerefore even if u change the state to say u are logged in, no api requests would be allowed because u wont be authorized/authenticated.

  • @0pticle
    @0pticle 3 года назад +1

    Thanks for the helpful video! Quick question. How would I change the isAuth state in App.js if I'm using a login screen under a route? Like in this example, if I was at /profile and the logout button was there instead, how would I be able to click the logout button there and update App.js isAuth state?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +3

      I would create a context state by using the context API!

  • @aragaochannel
    @aragaochannel 3 года назад +1

    OBRIGADAAAAA ME SALVOU!

  • @benjaminnopper3895
    @benjaminnopper3895 3 года назад

    You forgot one point: How to pass information to the protected route:
    You need to add props like this into it:

  • @sarpercelebioglu
    @sarpercelebioglu 3 года назад

    great video.

  • @10trends57
    @10trends57 3 года назад +1

    Very Good

  • @willyhorizont8672
    @willyhorizont8672 2 года назад

    sir can you make video of combining protected route and React.lazy and React.Suspense please

  • @asalentstech1767
    @asalentstech1767 3 года назад

    i have my user dashboard protected now and if not logged in routes to login form but now i need to connect this to my login can you do a part 2 on how to connect this isAuth with user login page

  • @seethalramakrishnan2778
    @seethalramakrishnan2778 2 года назад

    Hi , how we can check the availability of access token to check user is authenticated in this scenario?

  • @vafa2007
    @vafa2007 3 года назад +1

    Hey great video!
    I have a question about how secure this method is. Since the authentication check is based on the value of a single variable which is stored in the front end (browser), is this method prone to hacking? Could someone use the browsers console to manually change the authentication varaible to true and thus bypass having to login?
    How would this compare to creating a static web page for the login page and only sending the React App after the user is authenticated?
    Is there another video which can break this down for me?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +3

      Well, this wouldn't have any implications with the backend since to make a secure api you should use a method of authentication anyways. So even if the user manages to bypass the secure route, the page wouldn't show anything because they are not authenticated in the backend, meaning they are not passing a sessio variable or a jwt in their api calls.

  • @JavascriptForEverything
    @JavascriptForEverything 3 года назад

    Thanks

  • @mys8091
    @mys8091 2 года назад

    any idea what to use instead of withRouter thats in this video?

  • @manish-fp1fb
    @manish-fp1fb 2 года назад

    What is wrong if i am going to do like this
    {
    auth ?
    (routes to all my protected routers) :
    (routes to all my unprotected routers)
    }

  • @adamjones4690
    @adamjones4690 3 года назад

    Hey another great video!
    Wondering if you know the reason for an error i am getting... (I have tried to join this video with your 2nd in the login series (one with cookies))
    I am getting the following when trying to load the protected page - in my case called Home: Error: Invariant failed: You should not use outside a
    I believe it to be something around the line here:
    import React from "react";
    import { withRouter } from 'react-router-dom'
    function Home() {
    return this is a test;
    }
    export default withRouter(Home);

  • @deeppatel2819
    @deeppatel2819 3 года назад

    Hey buddy, Great video. Just wanted to add a small thing..Is there any way when the user redirects to login it pops a flash message such like "you need to login to access this page" something like that

  • @sukhvsin2
    @sukhvsin2 3 года назад +1

    Can you make some videos on Python Django Rest Framwork?

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      I love python, but I am only experienced with Flask. I was already planning on doing a flask series in the future. If iI end up learning django I will definitiely make videos about it!

  • @brandonthomas7698
    @brandonthomas7698 3 года назад

    Only problem im having is on page refresh it sends me back to "/"

  • @bhavyanayyer3391
    @bhavyanayyer3391 3 года назад

    if we dont have button like i have login page then who should we do

  • @maxaquilino7264
    @maxaquilino7264 2 года назад

    Good tutorial, it'd be great if you could do the same for React Router v6

  • @akashshrestha01
    @akashshrestha01 2 года назад

    great

  • @ruqayyahsara222
    @ruqayyahsara222 3 года назад +1

    I have a doubt. Please help me. I understood the whole protected routes thing. My doubt is if I refresh the protected page, it automatically logs out. How do I keep the state to log in even after refreshing the protected route?

    • @ruqayyahsara222
      @ruqayyahsara222 3 года назад

      Can you please make a short video about that? Thank you.

    • @PedroTechnologies
      @PedroTechnologies  3 года назад +1

      If you want to keep logged in you can do that in many ways. One way is by storing the userId in the sessionStorage. And every time your page renders you check if there is an id in the storage, if there is it means its logged in!

    • @ruqayyahsara222
      @ruqayyahsara222 3 года назад

      @@PedroTechnologies I did that. Thank you ☺️

  • @ritrikrohra3289
    @ritrikrohra3289 2 года назад

    please make with REACT ROUTER DOM 6(six)

  • @vamsee7875
    @vamsee7875 3 года назад

    How can do like this for mern app ?

  • @michaelhorvilleur8898
    @michaelhorvilleur8898 2 года назад +1

    As always, he doesn't assume any knowledge of the viewer. Good work!

  • @ThColinPereira
    @ThColinPereira 3 года назад

    absolute "banger"

  • @billpapas3054
    @billpapas3054 3 года назад

    Great tutorial but unfortunately this is not even the 1/10 of making an authentication system in React :(

  • @Brennnann13NY
    @Brennnann13NY 3 года назад

    where can we find this source code?

  • @HyuLilium
    @HyuLilium 3 года назад

    For some reason it is not working for me. It is entering the ProtectedRoute component and the condition isAuth is true, but it just won't render the Profile component, i've tried component={Profile} and component={}, none worked.

    • @HyuLilium
      @HyuLilium 3 года назад

      Seems I had imported BrowserRoute as Route and that's why it was not working

    • @PedroTechnologies
      @PedroTechnologies  3 года назад

      It should be BrowserRouter as Router instead. Let me know if it works!

    • @HyuLilium
      @HyuLilium 3 года назад

      @@PedroTechnologies It worked, thanks

  • @deveshrawool6035
    @deveshrawool6035 3 года назад

    How do I manage state though? It turns out whenever I route to a different path my previous states are lost, for instance whenever I am redirecting from login page to user page the auth state is lost and set to false and I am again redirected to login page. Do I need to use redux for the same? I am new to React js. If anyone can redirect me to a video I will be grateful.

  • @furkankurt5331
    @furkankurt5331 2 года назад +1

    why does this fucking library change their code in every 5 days.

  • @oussemabouyahia474
    @oussemabouyahia474 Год назад

    you could have make it more practical by using input with specified email/password that grant acces to each route after login

  • @nowekonto2
    @nowekonto2 Год назад

    I think it's time for a new version of 'protected routes' tutorial. This seems outdated.

  • @IdiotOfftheInternet
    @IdiotOfftheInternet 3 года назад

    I'll not recommend this video to learn react authorization.

    • @PedroTechnologies
      @PedroTechnologies  3 года назад

      I understand! Any particular reasons? I like constructive criticism cause I grow from it :)