React Login Authentication with JWT Access, Refresh Tokens, Cookies and Axios

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 959

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  Před 2 lety +65

    In this tutorial, you will learn how to apply a JWT user authentication strategy with access and refresh tokens to your React app. You will also learn how to create a private Axios instance and apply Axios interceptors to requests and responses. This tutorial is part 4 of a React User Login and Registration series. Links to all tutorials in this series are in the description. 🚀 If you're just getting started with React, I recommend my React JS for Beginners full course here: czcams.com/video/RVFAyFWO4go/video.html

    • @vickylance
      @vickylance Před 2 lety

      I have a doubt is it fine having so many 403 requests made and logged in the console? Or is calling refresh token api in a settimeout better?

    • @samiullahsheikh5015
      @samiullahsheikh5015 Před 2 lety

      Dave, I have the similar question. When should make the refresh token api call ?
      What is the best approach to hit refresh token endpoint to get new access token?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      @@samiullahsheikh5015 using the Axios interceptors detailed in this tutorial, the refresh token endpoint is automatically requested when needed.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      @@vickylance nothing wrong with a 403 if it is handled. The interceptors handle it. 💯

    • @broncoUkraine
      @broncoUkraine Před 2 lety

      Hi, thanks a lot, but with React 18 the last part with interceptors doesn't work. I receive CanceledError if using react 18 method render in the index.js, like const root = ReactDOM.createRoot(document.getElementById('root')); . And everything is fine when you use simple ReactDOM.render('code here").

  • @markoleptic4501
    @markoleptic4501 Před rokem +182

    This video in combination with the "JWT Authentication | Node JS and Express tutorials for Beginners" is the only tutorial/tutorial series that actually describes the full backend to frontend implementation of jwt authentication. AND provides the source code. It was frustrating finding so many videos that only did part of the implementation and expected the viewers to know how to handle the rest. Thank you.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem +13

      You're very welcome!

    • @luiscortes6563
      @luiscortes6563 Před rokem +2

      Yes thanks a lot! I attempted this about 2 yrs ago and couldn’t find a video on it being done the right way; they didn’t store access token in memory!

  • @zb2747
    @zb2747 Před 2 lety +34

    Thank you Dave - just watched your node jwt auth video with refresh tokens and now watching the implementation w/react on the client. Like someone said here there’s not to many dev/teachers who explain and implement jwt auth w/refresh end to end and with a database. Keep the videos coming my friend! Would love to see more intermediate - advanced topics especially in node. Peace and much love 🙏🏾

  • @user-di7mz3gz3i
    @user-di7mz3gz3i Před 6 měsíci +3

    hey dave, i love ur teaching style but , i want just one thing that , if u can do , can plz first explain , in which file what are u going to do , what is your thinking behind that file creation , i understand u breaking file in individual part for code modulizing but when we see the video , first we have to understand why this file is created and why u do that , what is ur thinking behind that file creation then we understand rest of , no doubt your teaching skill are far greater then any other teachers on youtube , if u can do this that help us a lot , love from India

  • @shashwa7
    @shashwa7 Před 2 měsíci +1

    36:27 one small addition in that catch block, is to check if the catched error code is due to 'cancelled req' due to aborting req when component unmounts. otherwise it will keep navigating back to login page even after success login! the code will look like:
    const getUsers = () => {
    try{
    //try block code
    } catch (err) {
    console.log("error fetching users", err);
    if (err.code !== "ERR_CANCELED")
    navigate("/login", { state: { from: location }, replace: true });
    }
    }

  • @noone_and_nobody
    @noone_and_nobody Před rokem +5

    This is gold! It just doesn't talk about the concepts instead develops per expectations of a real time user application. Absolutely loved it!

  • @memoryleakerz
    @memoryleakerz Před rokem +19

    I've seen many instructors Dave but you set a higher, newer, and better standard. You explain everything to the level of formatting your code and you master these skills with great knowledge. Awesome tutorial!!! you're awesome, subscribed for sure.

  • @kartikarora3521
    @kartikarora3521 Před 2 lety +4

    Thank You! I was waiting for this video. There is no video on CZcams which shows how to implement Refresh Token. Even I took a udemy course and it just showed me how to implement just Access Token and skipped the Refresh Token part. Thank You soo much!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +2

      Right on! 💯 I have also noticed this information is hard to find. I have included some resource links in the description too.

    • @kartikarora3521
      @kartikarora3521 Před 2 lety +1

      @@DaveGrayTeachesCode Thanks, I will read those articles too.

  • @kove5762
    @kove5762 Před 11 měsíci +2

    If you get 403 status when you try to refresh token, it's probably because during login cookies might not be saved in the browser. To fix it, you have to change siteName attribute from 'None' to 'Strict' when you are sending cookies ( Login and Refresh Token route)

  • @riteshjoshi0413
    @riteshjoshi0413 Před 2 lety +4

    I love how you simplify the topic while explaining sir.
    Thank You so much

  • @hikemalliday6007
    @hikemalliday6007 Před 3 měsíci

    These are baller vids man. Ive been at an internship for 3 months now, and the first couple months was a brain bleeding experience -- all of my self taught apps up to the point of hire were very one dimensional and basic, in terms of fetches, config, etc (they still are, but slightly better). After working in a pro setting and having to learn and use better practices such as custom hooks, acios config objects, etc, I can now follow and appreciate these videos even more. This is the perfect level of difficulty for me to improve my hobby applications. Great content, thank you so much!

  • @spirits__
    @spirits__ Před 2 lety +5

    You are on 🔥 with these advanced topics. Please keep them coming as there aren’t as many great videos on these topics. I just happen to be working on a new authentication system at work and trying to understand the differences between access, refresh token and the best practices for storing them and your video helped a lot ✊ and right on time

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      That's great to hear! Glad I could help and thanks for the support! 💯🚀

  • @a_basic_sheep_1993
    @a_basic_sheep_1993 Před 7 měsíci +2

    went from studying your videos to getting a job. Although we kind of not use react, but because of my skills I got the job. So A big thank you

  • @VinceKully
    @VinceKully Před rokem +4

    Hey Dave just wanted to point out that axios >= 1.0.0 (1.0 was released ~10 days ago) will cause your response interceptor to reject an error when retrying. There are a few lines in the axios source code that, when trying to normalize values, ends up returning any unknown value via toString, which ends up spitting out an entire function being toStringed as the error passed into the response interceptor.
    Downgrading axios to 0.27.2 allowed your solution to work perfectly.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem +3

      Thanks Vince - yes, technology keeps moving forward. Also note this tutorial uses React 17 and React 18 strict mode will trigger multiple requests.

    • @dukekim4816
      @dukekim4816 Před rokem +2

      Hi Dave! Loving the tutorials. Any plans on updating this to keep up with updates?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      @@dukekim4816 YT will not let me update videos.

    • @Iskarize
      @Iskarize Před rokem

      Do you think this will get fixed in the future?

    • @calinciobanu259
      @calinciobanu259 Před rokem

      @@DaveGrayTeachesCode Hey! Are you not going to say anything about a quick fix? I have been struggling with this problem for a couple of hours and just now saw this comment

  • @googlemani
    @googlemani Před 4 měsíci +1

    Enjoyed the content on JWT Authentication on React. I have bookmarked your other videos on the playlist. I highly recommend your Nodejs and ReactJS course.

  • @webblocksapp
    @webblocksapp Před 2 lety +4

    Hi Dave thanks a lot for this tutorial, it helped me a lot for implementing a refresh token flow at my React app. I would like to add a small fix to your guide, instead of using 403 response status at the interceptor, use 401. 401 refers to unauthorized, it means the client provides no credentials or invalid credentials, and 403 the client has valid credentials but not enough privileges to perform an action on a resource. If we validate vs 403, we can have the scenario of refreshing the token unnecessarily if the user doesn't have a role for a specific request.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      Thanks for the note! Not sure where I first learned 403 was the response to use for the expired access token but I stand corrected. 💯

    • @daniezra
      @daniezra Před rokem

      @@DaveGrayTeachesCode
      I had the same issue, the ChatGpt response about that is:
      "When an access token expires in API, you should return the HTTP status code 401 Unauthorized, along with a WWW-Authenticate header indicating the authentication method required to access the requested resource.
      The HTTP status code 401 Unauthorized indicates that the request lacks valid authentication credentials for the target resource, and the WWW-Authenticate header informs the client which authentication method to use to authenticate the request. This is the correct status code to use when an access token has expired or is invalid.
      The HTTP status code 403 Forbidden, on the other hand, indicates that the server understood the request, but refuses to authorize it. This means that the client has authenticated correctly, but does not have sufficient permission to access the requested resource. Therefore, it is not appropriate to use the HTTP status code 403 Forbidden when the access token has expired in API.
      I had the same issue, the ChatGpt response about that is:In summary, when an access token expires in Web API, you should return the HTTP status code 401 Unauthorized."

  • @TannerBarcelos
    @TannerBarcelos Před 4 měsíci

    This is something I still struggle with after 3 years experience. This cleared up all my confusions. Thanks as usual, Dave!

  • @thomasandre-lubin3899
    @thomasandre-lubin3899 Před rokem +4

    Amazing tutorials, I've rarely seen something that well explained, and detailed. The rythm is great too !

  • @andrewclarke6916
    @andrewclarke6916 Před 2 lety +2

    Phew! This was a tricky one and it took a while to get it working correctly but I got there in the end. I had the same cookie problem as Marcos but working fine now after adding 'secure: true' to the cookie settings.

  • @brajagopalmukherjee1588
    @brajagopalmukherjee1588 Před 2 lety +3

    Man waiting for full stack beginner friendly project

  • @dandelion0331
    @dandelion0331 Před rokem

    youtube now doesn't display dislike anymore. Dave if you see dislikes at the backend of youtube, don't frustrate, those dislikes are from bootcamp training people. we suport you. learned a lot from MERN stack series and this series. The redux series is little tough but I will spend more time on those. Thanks!

  • @ImperiousRex13
    @ImperiousRex13 Před 2 lety +4

    Hey Dave, I appreciate all your hard work in putting these videos together for those of us trying to learn something new. I enjoyed your video but I am having issues when logging in to see the Users list. I've seen that you answered similar questions about getting a CanceledError and I've also taken a look at your other videos describing these breaking changes. I've tried to implement your solutions while still working with strict mode but, I still receive errors.
    So far the only solution that has worked for me is to remove the AbortController in the Users component's useEffect but, is that appropriate?
    I would like to do things the right way and seeing as how you have the Abort Controller I just don't know what I should do.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +3

      Hi Rick. The AbortController issues an CanceledError by default when called. It is not a bad thing because it is expected to happen. With React 18 strict mode, React mounts, unmounts, and remounts each component. This creates one of those CanceledErrors. I did make a video about how to workaround this here: czcams.com/video/81faZzp18NM/video.html

    • @ImperiousRex13
      @ImperiousRex13 Před 2 lety

      @@DaveGrayTeachesCode Thanks for the response! I am currently watching the workaround video because at this point I considered removing the AbortController altogether. Do the errors also explain why, when I try to log in to see the users list, I was not redirected to see the list?
      When I try to log in, I can see the user object retrieved but all that happens is the Login form/component reloads.

  • @sammyattah4653
    @sammyattah4653 Před 2 lety +2

    Thank you Dave. I can't stop recommending you to my friends/colleagues. If one wants to learn modern react, your channel is highly recommended. You have helped me change some of my old codes.

  • @morphology5114
    @morphology5114 Před 2 lety +1

    In my app, I won't need registering, and I was searching for a tutorial on handling JWT authentication with react. You literally saved me posting this tutorial right now. I will try to implement only login using this video, I'm using Django for my backend. Thank you very much, very good and understandable tutorials + the small tips and tricks with the VS Code are also on point!

  • @dominiktworek6455
    @dominiktworek6455 Před 8 měsíci

    Hey, I am someone who knows react and js very little. I decided to build a full-stack application using React on TypeScript, SpringBoot on Java and MySql database. While I feel confident with Java and Spring, I struggle a lot with React. This series helps me a lot to implement a logging system and routing system. Some topics like cors weren't clear to me and with this series and with some ChatGPT explanation I got to the point where every basic info about HTTP-only cookies, tokens, react-router, etc became greatly allocated in my brain :). I would pay for a course like this, so I really think this is super valuable material.
    Peace and happy coding!

  • @jinge8943
    @jinge8943 Před rokem +1

    Thank you for your explanation.
    Can I have a question?
    If you don't store auth state in browser storage, when user reload the page, how to know if user is logged in or not?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      Go to the next video in this series to create a Persistent Login (link in description)

  • @user-ry4hu4br2z
    @user-ry4hu4br2z Před 6 měsíci

    Incredible! I had been looking for this content for some time, I was very dissatisfied with my authentication strategy, as I started in the area recently, I ended up following some practices, which based on initial knowledge, it is not possible to say whether it is a safe practice or not. your approach gave me good insights into how I can keep improving, thanks Dave, impeccable work. hugs from Brazil!

  • @ibraheemsulay
    @ibraheemsulay Před 2 lety

    This guy could explain a very technical concept to a toddler and the toddler would understand it with ease. Thank you for this wonderful tutorial

  • @Yogesh-kr7bo
    @Yogesh-kr7bo Před 4 měsíci +1

    can someone explain why the useAxiosPrivate uses a useEffect and why the unneccesary auth and refresh dependency?

  • @thomasmunguya2207
    @thomasmunguya2207 Před 10 měsíci

    I love how you explain concepts in detail. You go as far as explaining the motivation behind every decision made. Great tutorial and thanks!.

  • @demetrx7972
    @demetrx7972 Před rokem

    This one explained all I needed and relieved me of that pain related to access/refresh tokens that I've been having for the last couple of days. Thank you, Dave!

  • @kawsar.130
    @kawsar.130 Před rokem +2

    Your videos are making great changes to my skillset and improving standard programming concept everyday. You are awesome Dave Gray. I really appreciate your hard work.

  • @sadaliryu
    @sadaliryu Před 2 lety

    Thanks Dave Gray, i searched for this course and didn't find any clear and functional tutorial like your, Well Done

  • @hristopisarev1827
    @hristopisarev1827 Před rokem

    I have watched many tutorials and paid courses but this is next level - awesome explanation of everything ! Thank you!

  • @RaniLink
    @RaniLink Před rokem +1

    Thanks for the amazing video(s)!
    Question, in your useAxiosPrivate hook, why did you add the refreshToken hook in the useEffect dependency array? it's a static function, innit?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem +2

      You're welcome! Going back in time and my memory here... this was with React 17 that complained about things missing from the useEffect dependency array even when they weren't sometimes needed... so I usually added everything it complained about. Otherwise, many questions from viewers about the warning for the missing dependencies.

  • @kodewala
    @kodewala Před 11 měsíci

    A perfect solution to my every problem. Trust me this video is going to help me a lot.

  • @TheTRUEASIANZ
    @TheTRUEASIANZ Před 2 lety

    If you are using React 18, I suggest using a boolean ref with useRef before the getUsers function.
    In User.js:
    if (firstRenderRef.current) {
    firstRenderRef.current = false
    return
    }
    const getUsers = async () => {
    try {
    // something
    } catch (err) {
    console.error(err)
    navigate('/login', { state: { from: location }, replace: true })
    }
    }

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      A very similar suggestion / solution to the "fix" I suggest in my video here: czcams.com/video/81faZzp18NM/video.html - good stuff, James! 💯 I will also be sharing a re-work of this full auth flow soon which uses React Query and React 18.

  • @egorushakov2267
    @egorushakov2267 Před rokem

    Finally, a video that shows you how to securely authenticate users. Thanks Dave

  • @marcel5235
    @marcel5235 Před 2 lety

    Finally intermediate tuts for my job - appreciate your content, helps me get little bit higher and self sufficient as React dev at job field

  • @tech5956
    @tech5956 Před rokem +1

    Great tutorial😍, Keep sharing videos like this, Wish you more subscribers for your great efforts and time

  • @ronir.kpradhan1785
    @ronir.kpradhan1785 Před 2 lety +1

    Hey Dave, just wanna say thank you for this amazing tutorial you really made it crystal clear on how to handle the tokens. Much love and wish you good health 🙏

  • @MrQVeeBoo
    @MrQVeeBoo Před rokem

    just wanna note to myself or anyone who confused like me.
    32:00
    axios interceptors is the middle man who intercept between client and server both ways, request and response.
    the part that said !prevRequest?.sent means
    ?. operator is used to safely access properties or call methods on a possibly null or undefined value.
    null or undefined value in if statement is considered as false
    therefore, .....!prevRequest?.sent means
    if sent property doesn't exist, then undefined but not throw error and with the ! negates then the whole this of (!prevRequest?.sent) is true.
    thus, if sent doesn't exist then create this sent property, prevRequest.sent = true;

  • @miloheh
    @miloheh Před 2 lety

    Super helpful! I struggled making this year ago. I’m glad to see this explained because it seems much more manageable/clear coming from you!

  • @rajatgupta001
    @rajatgupta001 Před rokem +1

    Great tutorial, got so many doubts cleared. However I have 1 question: Since, we are already sending access token and at server can also access refreshToken from cookie (as withCredentials is true), we can simply verify accessToken at server and if it's expired, we can verify refreshToken and if refresh token is valid we can simply send back new access token. This way we don't have to make 2 (requestInterceptor and responseInterceptor) requests to the server and hence don't require responseInterceptor at all.

  • @mohajeramir
    @mohajeramir Před rokem

    Congratulations on 💯. What a great way to wrap up the year. Might be a good occasion to post a non-technical video. I am sure a lot of people would love to know you more.

  • @TheNiquitin
    @TheNiquitin Před rokem +1

    Great tutorial! Very helpful, and as somebody else mentioned, I love that you mention every shortcut you use in VS Code because that lets em improve my own workflow. I'm always postponing diving deeper into VS Code's tools and this kills two birds with one stone (my apologies to PETA members).

  • @TheLukjtas
    @TheLukjtas Před 2 lety +1

    Tus tutoriales son fantásticos! Estoy estudiando inglés por lo que se me complica un poco el idioma, pero tu contenido es de extrema utilidad.
    New sub!
    thx

  • @user-sn9ye9rh8f
    @user-sn9ye9rh8f Před 6 měsíci

    The best IT teacher over the world!

  • @TheJamesgggill
    @TheJamesgggill Před 2 lety

    This is probably the best JWT auth tutorial i've seen. Thank you!

  • @Dtree12345
    @Dtree12345 Před rokem +1

    I have zero idea what’s happening but I still watched-no clue why.

  • @moizratlamwala9377
    @moizratlamwala9377 Před 5 dny +1

    What if page is refreshed after loggedin? Our access token will be vanished from app’s state?

  • @GautamKumar-jx5su
    @GautamKumar-jx5su Před rokem

    All this interconnected tutorials feels like movies franchise :)
    love this tutorial btw.

  • @chans9055
    @chans9055 Před 2 lety

    Now I think I know how to apply access/refresh token strategy to web services. Thank you for the great tutorial!

  • @htoomaungthait1880
    @htoomaungthait1880 Před rokem

    Thank for showing way to implement role based authentication and access control in react.

  • @masonasa7009
    @masonasa7009 Před 7 měsíci

    thank you sir for this this truly raised my level up a notch and am happy this series teaches almost everything and i must say it did get a bit advanced for me lol

  • @Teddyrobinson
    @Teddyrobinson Před 6 měsíci

    Thank you for the tutorials!! Made it through all 4 and I'm happy that I did

  • @codernasir1549
    @codernasir1549 Před 2 lety

    A simple like or share is not enough for this rich tutorial. you deserve more. take love.

  • @jonathantang4429
    @jonathantang4429 Před 2 lety

    Your videos are one of the best out there. Each time there are anything that I don't understand from your video, I will go and research more on that portion and I will learn a lot. I got lost on the useAxiosPrivate hook portion but I just followed along the tutorial and it works!! Now I need to go learn about interceptors. Do you have any videos on it?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Glad I could help, Jonathan! 💯 This is the video I cover where I cover Axios interceptors overall. They intercept requests and responses and allow you to modify those requests and responses as they are intercepted. The Axios docs cover them here, too: axios-http.com/docs/interceptors

  • @testtest-vb2qn
    @testtest-vb2qn Před rokem +1

    For the people running into the Refresh Error from axios instance change this:
    prevRequest.headers['Authorization'] = `Bearer ${newAccessToken}`;
    To:
    prevRequest.headers = { ...prevRequest.headers, Authorization: `Bearer ${newAccessToken}` };

  • @thakursaad9364
    @thakursaad9364 Před rokem

    This whole playlist was of insane help. Don't know how to say thank you. But thank you anyway

  • @deepfuchsia7204
    @deepfuchsia7204 Před rokem

    One thingy I noticed. Let's say we open a page in a new tab, and this page has two different resources we need to get, let's say /cars to get a list of cars and /fruits to get a list of fruits.
    Those two request are sent immediatelly, without waiting one another. At the moment of time we don't have an access token, but we have a refresh token. Since the request are sent in parallel - we'll have the refresh token flow twice.
    So basically the more requests we issue - the more refresh token requests we'll get as a result.
    It would be nice to somehow delay all requests if refresh token request has started and hasn't finished yet.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      Sounds like you are using React 18 in strict mode. Fix for what you are describing: czcams.com/video/81faZzp18NM/video.html

  • @DH-jw4th
    @DH-jw4th Před 2 lety

    Wow, thanks for responding tho my request. This was worth the wait! Genius presented in a way that can be understood by the average react developper…

  • @henry-chung
    @henry-chung Před rokem +1

    What an amazing tutorial, Dave!
    Thank you very much for all your efforts putting in these videos!
    I've been wondering that why we shouldn't store JWT in local storage, because when we make a private request, we attach our JWT in the header ("Authorization": "Bearer ..."), so someone can copy that token from the request header. Why is this method more secured?

  • @Chrizz604
    @Chrizz604 Před 2 lety +1

    Wouldn't it make more sense to store the JWT expiration as a unix timestamp in the JWT and always check if the token has expired, so we do not get 403 responses at all?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      There is usually more than one way to solve a problem, and your method could be employed. I prefer the separation of concerns from frontend and backend which is demonstrated here and lets the backend handle expirations. That said, some may prefer to decode the JWT on the frontend and circumvent the backend check with a check of their own. The frontend is never 100% secure and the backend still needs to track expired tokens, so I'd still keep the backend checks in place either way.

  • @1Malak16
    @1Malak16 Před 2 lety

    Thank You! There is not so much high quality information regarding advanced topics such as authentification. Please keep on with such videos.

  • @profeskills768
    @profeskills768 Před 2 lety

    This is the best JWT-cookie auth tutorial i've seen. Thank you so mush!, all works for me . thank you again for this awesome Tutorial.

  • @ahmad-murery
    @ahmad-murery Před 2 lety

    I think I need to watch this video one more time,
    I feel like my brain got shrank a little, it's maybe the cold weather,
    Thanks Dave,

  • @kirillzlobin7135
    @kirillzlobin7135 Před 6 měsíci +1

    24:52 - I am always confused about this. Our auth and refresh - are an object and a function - reference type. When we use it in a dependency array - it will always "change" and make the component to be rerendered? No?

  • @waili4028
    @waili4028 Před 2 lety

    thank you so much for the video, you answered all my confusions already in the first few mins! enjoying the whole video now

  • @rishidubey8745
    @rishidubey8745 Před rokem

    your explanati level on is something else !!!

  • @lidorbenshimol1838
    @lidorbenshimol1838 Před 2 lety

    Hey Dave, Amazing tutorial as usual.
    Im very happy to find pro tutorials like that in youtube and want to say thank you!

  • @markosmk
    @markosmk Před 2 lety +2

    Hi Dave!, excellent video and explanation! Thank you! I have a doubt, it could be better to control the requests and responses with the axios interceptor, for example by putting the interceptors in the index.js, so in this way we do not repeat code, and control the redirection to the login also in the interceptors 🧐, I was thinking that it would be good, for example if we had more than 30 pages and components with authorization, we would need more than 30 redirections, it was just a thought not to repeat code if the app is big, thanks again for these videos! they are great! 👍

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +2

      If you were creating many components you could abstract more logic. Remember the app may also have public pages so blanketing everything in the index might not be ideal. Overall, this code could serve as a blueprint to be modified as needed. I'm glad you like the series and thanks for the kind words! 💯🚀

  • @dharmaraosalana641
    @dharmaraosalana641 Před 26 dny

    Good effort but step by step by approach makes understandable more

  • @yuhiahtyun5665
    @yuhiahtyun5665 Před 2 lety

    You're awesome Mr.Gray. Keep up your great work, your channel gonna be big in the future !

  • @web-dev-spider
    @web-dev-spider Před rokem

    Hi, Dave!
    First of all, thanks a lot for helpful tutorials you make. I have a question about a situation that refreshToken gets expired. Here's how things go in user's perspective.
    An admin user logs in => visits Home page => visits Admin page => waits 30s for refreshToken to be expired => visits Home page(!) => visits Admin page(!) => be redirected to Login page => can visit Home page by clicking browser's "go back" button(!)
    Those with (!) mark are not expected behaviours. Shouldn't user be logged out when refreshToken expires? Why a user can visit pages that require auth? What am I missing and what should I do?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      I'm not sure what you are missing, but you can possibly view cached pages from your history - however, reloading those pages won't work.

  • @al-doori2392
    @al-doori2392 Před rokem

    Thank you a lot lot I was struggling how to combine .NET Core API implementing JWT with React, may gold bless you and give you what you wish, you helped me a lot !!

  • @HayatoYano-qt8tu
    @HayatoYano-qt8tu Před rokem

    Very well explained. Helping me a lot, Mr. Dave. Cheers from japan

  • @miglrodri
    @miglrodri Před 2 lety

    This is great content, i watched all videos, probably forgot to like them all, but already subscribed to the channel. Thanks very much for the explanation 👏

  • @tabliqatchi6696
    @tabliqatchi6696 Před 2 lety +1

    TYSM Dave ♥. May I ask why you're ejecting axios interceptors? Their documentation is also not very clear on this one. If we need an axios instance without those interceptors we can just use another instance other than axiosPrivate, right? So...?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      Much like event listeners, if you do not remove the interceptors, they will begin to stack up. With interceptors, your requests will multiply. This will create unnecessary requests and unexpected behavior.

  • @jessbk1678
    @jessbk1678 Před 2 lety

    hey Dave just want to say this was a great tutorial, definitely has helped me alot to understand how to work with jwt and refresh tokens on the client side, can you do a tutorial on how to deploy with docker / kubernetes / enginx

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Thank you and great request! Docker is definitely on my list 💯🚀

  • @Olekiapi
    @Olekiapi Před rokem

    Thanks dave this was educative, I was having problems in implementing Auth on React.

  • @sultannasyrov4662
    @sultannasyrov4662 Před 2 lety

    Great course on react authentication. Thanks a lot. Exactly what needed.

  • @pravinkumarg8007
    @pravinkumarg8007 Před 2 lety

    This is next level tutorial. Thanks @DaveGray for an awesome tutorial :)

  • @laurraileanu8374
    @laurraileanu8374 Před rokem

    great tutorial Dave, really in-depth and easy to understand

  • @starnutoditopo7762
    @starnutoditopo7762 Před 2 lety +2

    Hi, Dave!
    Whould you consider a good choice to move the "redirection to login page" from the users component to the useAxiosPrivate hook?
    That is: in useAxiosPrivate.js,
    1) add the required imports
    import { useNavigate, useLocation } from "react-router-dom";
    2) initalize
    const navigate = useNavigate();
    const location = useLocation();
    3) replace the line
    newAccessToken = await refresh();
    with something like:
    try {
    newAccessToken = await refresh();
    }
    catch (err) {
    console.log("Cannot refresh token: redirecting to login page.", err);
    navigate('/login', { state: { from: location }, replace: true });
    }
    ?
    This should handle the redirection logic in a unique point in the application, avoiding to replicate it in every component.
    Thanks!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +2

      I think that's a good thought! 💯 You lose some flexibility for redirects based on specific requests, but overall, this might be all that most need. 🚀

  • @saadarman4718
    @saadarman4718 Před 8 měsíci

    Another well made and greatly explained tutorial.

  • @hamidabdolrahimi4606
    @hamidabdolrahimi4606 Před rokem

    Thank you man. Awesome, best tutorial ever. Keep it up

  • @gokulrajan6987
    @gokulrajan6987 Před 2 lety

    Very informative! The best I have seen so far!

  • @ksue1448
    @ksue1448 Před 2 lety

    The best Auth tutorial ever!! appreciate all the references links too

  • @abdelmoumenbenaida8037
    @abdelmoumenbenaida8037 Před 2 měsíci

    there's something missing
    how can use hooks useAxiosPrivate inside services library ( file contain only functions that call axios )

  • @pradeepmax1
    @pradeepmax1 Před rokem

    Best JWT Tutorial on YT 👍

  • @webcoder293
    @webcoder293 Před rokem

    I found a lot but this is the only video I've found on youtube with both access & refresh token used with proper purpose.
    I have one query, what if we use RTK (not RTKQ) and want to use useAxiosPrivate hook inside createAysncThunk to send/fetch data from server. How can we place axiosPrivate in that case as we can't use custom hooks outside component ot any other custom hooks. Again, thanks a lot for this video.👍

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      I made a video that applies this to Redux, but it may not apply it in the way you described... nevertheless, you can check out how it works here: czcams.com/video/-JJFQ9bkUbo/video.html

  • @skyyfiman
    @skyyfiman Před 2 lety +1

    Hello Dave Gray,
    This tutorial was very helpful, it gave me a few ideas when working with access and refresh tokens, also learned a few new things too, I was able to work JWTs into a react app project I’m working on that relies on Passport.JS and Redis for authentication.
    For anyone running the React and backend server on a different machine and accessing it from a browser on another machine. On the backend, you’ll need to remove “samesite” and set “secure” to false for the secure cookie within “handleRefreshToken” to be set in a browser. Secure will only work if everything is being accessed from the same machine or you are feeding an HTTPS cert with the request. Please correct me if I’m wrong because chrome and Brave under network had a very tiny yellow “!” error that was hard to find. No errors were showing under console from Chrome, Brave or FireFox.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      You're welcome! 💯 Just to add to your note which I believe is accurate.. In production, you will always want to use HTTPS instead of HTTP and add secure: true back to the cookie.

    • @mauroconsolani2576
      @mauroconsolani2576 Před 2 lety

      Skylark, I have to say it... I fu***** love you. You've just solved the issue that I've not been able to solve in the last 24hs. I was just getting a 401 with the sameSite="none" configuration, I dig into the whole web until I got to your comment. THANKS.

  • @abhishekghadge
    @abhishekghadge Před 2 lety +1

    Hi, where is the backend code?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Link to the Node JS course is in the description. It has a resources link in its description with source code for every chapter. You will want to grab the last chapter if you just want the code.

  • @mauronunes7196
    @mauronunes7196 Před rokem

    Insanity how good this is.

  • @davetinoco
    @davetinoco Před rokem

    Wouldn't you want to put the controller outside the useEffect?

  • @MASTERRAGE
    @MASTERRAGE Před 2 lety +1

    Exactly what I wanted ! Thanks

  • @zenec_
    @zenec_ Před 2 lety

    Great guide! almost feel like I need to rewrite all my code cause the tutorials I followed didn't cover half of what you did... but that will be for some other time cause I have other projects to finish x)
    But thx for sharing your knowledge ow wise man!

  • @hasanfawaz9282
    @hasanfawaz9282 Před rokem

    Thank you Dave for everything, I have learned so much from you. 💙💙

  • @techdiversdev4784
    @techdiversdev4784 Před 2 lety

    Great tutorial ! I finally pushed myself to learn React and tous helped me a lot, i improved so much. But there is one thing i can't wrap my head around: why having the short duration jwt in memory if we have one supposedly more exposed in cookie to generate other short duration jwt?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Thank you! 🙏 For these tutorials, I have extremely short token durations as examples. A more realistic duration for the access token is anywhere from 15 to 60 minutes (set to preference) and the refresh token often lasts a day or longer. There are links in the description to articles discussing this strategy, too.