Node.js API Authentication With JWT

Поделиться
HTML-код
  • Опубликовано: 3 окт 2024
  • In this overview we will take a look at Node.js and JSON web tokens. I will show you how to create a route to generate a token and use that token to make a request to a protected route.
    SPONSOR:
    Check out Coding Dojo - www.codingdojo....
    CODE: Github Repo With Example
    github.com/bra...
    💖 BECOME A PATRON: Show support & get perks!
    / traversymedia
    ONE TIME DONATIONS:
    www.paypal.me/t...
    VISIT MY WEBISTE: Check Out My Udemy Courses
    www.traversymed...
    FOLLOW TRAVERSY MEDIA:
    / traversymedia
    / traversymedia
    / traversymedia
    / discord

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

  • @RealToughCandy
    @RealToughCandy 6 лет назад +194

    Thank you Patreon patron for this request and thank you Brad for the tut!

    • @TraversyMedia
      @TraversyMedia  6 лет назад +14

      You're very welcome. Great channel by the way, guys check it out

  • @aiwithrudiyt
    @aiwithrudiyt 4 года назад +5

    Brad comes along to save the day once again. Honestly bro I don't know how you do it.. 5 days of pain, dozens of videos and dozens of blog posts with unclear non-relevant over engineered information, then you come along and in one fell swoop answer EVERY SINGLE QUESTION I had around the topic in a clear, detailed and swift manner. Thank you brother once again...

  • @emmanuel4699
    @emmanuel4699 6 лет назад +146

    You're a role model to us all. I plan to improve my Patreon participation this coming year. I've grown in confidence as a software dev and have a job now thanks to your videos. Overall my life has seen a lot of improvement as a result of this channel, So thank you and Merry Christmas, Brad.

    • @TraversyMedia
      @TraversyMedia  6 лет назад +13

      Thank you, Merry Christmas to you as well :)

    • @Zawodowieec
      @Zawodowieec 4 года назад

      Great to hear buddy, hope you're still doin' well :)

  • @chriszhang3629
    @chriszhang3629 6 лет назад +3

    You are not the only one who makes video about JWT, however, you deliver the knowledge in clearest and easiest way. Thank you Brad!

  • @keyone415
    @keyone415 4 года назад +24

    In 2020, with v12.15.0 everything still works exactly the same, I just run through your tutorial. Thank you Brad!

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

      That's good, because I'm going to do this tutorial soon.

  • @desbutler4257
    @desbutler4257 5 лет назад +7

    One of the shortest, yet most succinct, useful tutorials I've seen. Brad rocks!

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

    I always struggle understanding the auth stuff but you have managed to explain it so well. Thank you so much for this tutorial Brad!

  • @adriatic123
    @adriatic123 5 лет назад

    I needed a refresh about JWT functionalities and this little course was very helpful. Just one mention. Token name that we send with res.header can be anything for example 'auth-token', not just 'Authorizatiion'. And token value in a name-value pair doesn't have to start with 'Bearer' word, it's enough to send token only. In that case we do not have to split result into name 'Bearer' and token value.

  • @devonmarantz3324
    @devonmarantz3324 5 лет назад +1

    Thank you so much! I’ve been struggling trying to make a protected route with passport for the past month. I couldn’t get it working on a side project I was working on would get super frustrated. I watched this video last night and was able to get it working today! You are a God send! 🙏🏽

  • @thexs1118
    @thexs1118 6 лет назад +17

    I was literally just looking up how to do this and this video pops up. This video is a God send lol. Perfect timing.

  •  5 лет назад +1

    I came from the MERN series part 9 to learn more about JWT before implement it to the project. And I made it even it is my first time using auth. Thanks, Brad. 👏🏽 Excellent content as always!!

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

    This is an excellent, no-nonsense, straight-forward explanation of using JWTs to protect API routes, thanks.

  • @jaboyak
    @jaboyak 6 лет назад +2

    This is exactly what I needed. Short, sweet, and to the point! Have a great holiday, man!

  • @gmjitendra
    @gmjitendra 6 лет назад

    Don't dislike knowledge. Knowledge can be appreciate only. It's not a movie which can be like/dislike. Humble request to the people. Brad I really appreciate your knowledge which improve my understanding and skills. Thank you Brad for this fantastic tut.

  • @nosferathu258
    @nosferathu258 5 лет назад +2

    Reaaaaaly liked the straightforwardness of the tutorial. Implemented it on an existing server in under 40min thanks to your tutorial, keep up the good work :D

  • @Somsubhra_Das
    @Somsubhra_Das 5 лет назад +2

    Man I love this tutorial... Other RUclipsrs take more than 1 hr to explain just this. But you did it in less than 25 mins..

  • @ajit555db
    @ajit555db 5 лет назад +1

    Brad Bhai (brother in India), thanks a lot for the succinct explanation..always love the explanation in code.

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

    Brad, your videos are amazing. Whenever I need to cut through the b.s. and learn something quickly, I come to your channel. This video is an enormous help to me. Thanks!

  • @alexn2566
    @alexn2566 5 лет назад +34

    You could have handled the JWT verification in the middleware, to keep responsibilities nicely separated. Other than that, nice tutorial :)

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

      That also gives the nice advantage that the token itself doesn't get passed on the request object twice (req.token = bearerToken is no longer required).
      May not seem like much, but if you ever need to scale an application, even a few bytes of data per request can save a lot in the long run.

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

      Yes.
      The authentication should be done at the middleware.
      What Brad did works better for authorization once in the route handle.

  • @shashanksshetty9870
    @shashanksshetty9870 6 лет назад +10

    Merry Christmas Brad. I wanted this video badly. I was stuck with server side route authentication. Thanks for it and keep the good work going 😊. If possible please make a video using front end application, so that we will get more clear cut idea on this.

    • @Baal93Ash
      @Baal93Ash 4 года назад

      yes please, agreed !

  • @mohammedabdel-baki7253
    @mohammedabdel-baki7253 3 года назад

    Clear & to the point without irrelevant information.
    Thanks Brad you've been helping me so much for almost a year.
    Cheers 🍻

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

    Probably the best JWT tutorial on the internet.

  • @GeekMustHave
    @GeekMustHave 6 лет назад +1

    Merry Christmas to you and your family. You have been a big help and inspiration over the last year. I appreciate you and the extreme effort you put into helping people to learn. As always keep broadcasting!!

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

    This is the course I've been looking for! Brad, you are the best!!

  • @danielkimani5431
    @danielkimani5431 6 лет назад +1

    I have been waiting for this Brad, I can't thank you enough

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

    thanks its really helpful video, my boss was struggling with this authentication for a very long time, but now we will prove that who is the boss

  • @arpitagrawal7771
    @arpitagrawal7771 5 лет назад +1

    I have seen two of your videos and found this channel very helpful. Thank u :)

  • @vidalroland
    @vidalroland 6 лет назад

    This is exactly what I was looking for !
    Now I know how to make it work. Clear and simple.

  • @ЕгорЗахаров-д4й
    @ЕгорЗахаров-д4й 4 года назад

    Awesome! I wish each my question on the Internet had such a clear explanation

  • @muhammadyasirjaved6377
    @muhammadyasirjaved6377 4 года назад +1

    Simple, To the point and concrete. Thanks Buddy!

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

    Best video on node and jwt so far. Thanks a lot.

  • @kirillfishuk5553
    @kirillfishuk5553 4 года назад

    Very useful even after 3 years! Thank you Brad!

  • @devlondres
    @devlondres 6 лет назад +1

    Thanks for recording this video. Things are a lot clearer now.

  • @slicerabbit6166
    @slicerabbit6166 5 лет назад +2

    you shouldn't store the jwt in local storage, as it opens an attack vector for xss. an httponly cookie is the preferred way. now, it means that your rest api no longer is stateless, but it sure makes it way more secure.

  • @bouidiaabdelrahmene1617
    @bouidiaabdelrahmene1617 6 лет назад +1

    I am a huge fan of your tutorials, thank you so much for all these great videos

  • @gulfcoastflyfishing8458
    @gulfcoastflyfishing8458 6 лет назад

    Doing the lords work. Thanks a bunch, this just helped me through the toughest part of my current project.

  • @sabinhertz4145
    @sabinhertz4145 4 года назад +1

    This video was very well done, very informative, and exactly what I was looking for.
    Thank you, I learned much more than I bargained for.

  • @BlakeEdwards333
    @BlakeEdwards333 6 лет назад

    Best technical videos on youtube, keep up the good work!

  • @2wheelsonline
    @2wheelsonline 6 лет назад +1

    I love watching your tutorials and this is brilliant. Many thanks!

  • @billkariri
    @billkariri 5 лет назад +1

    I found this video at the right time, thanks Brad

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

    now i understand jwt and how to use it... thanks Brad!

  • @Juzzh
    @Juzzh 6 лет назад

    You have some of the best web tutorials I've found yet. I bought also your Udemy MERN stack course, can highly recommend! Thanks.

  • @jorgerenteral
    @jorgerenteral 5 лет назад +3

    The verifyToken middleware should do the token validation and, in case of, check the token is valid from the DB.

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

    october 2021 still works!
    thx brad

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

    That was a huge amount of help , really easy to understand and you explained it in a calm , quite and nice manner which is really important

  • @kedarnadkarny4718
    @kedarnadkarny4718 6 лет назад

    I implemented this 100% successfully. Thanks, Brad.

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

    This is exactly what i needed, amazing stuff!!!

  • @jazzpote4316
    @jazzpote4316 4 года назад +6

    17:57 Beware: do not store JWTs in LocalStorage. Use an http-only cookie instead. Any javascript has access to locastorage, therefore by using it as your secure storage you lay your app open to xss attacks.

    • @TheAndre2131
      @TheAndre2131 4 года назад

      Well, localStorage is generally the preffered method. Moreover cookies are not any safer, they are prone to CSRF attacks as well

    • @jazzpote4316
      @jazzpote4316 4 года назад

      @@TheAndre2131 Yep, that's why we invented CSRF tokens!

  • @safwanmohammed7715
    @safwanmohammed7715 4 года назад

    Very nice explanation with working code examples

  • @peter11612
    @peter11612 4 года назад +2

    Thanks for the video, clear and easy to understand!

  • @neoskribe8986
    @neoskribe8986 6 лет назад

    Thanks Brad, great video, happy holidays.

  • @Xero1216
    @Xero1216 6 лет назад

    Thank you, i learned how to use a token to access protected routes. LOVE IT MAN!

  • @gicharu
    @gicharu 5 лет назад +1

    Thank you so much for your tutorials. They are very informative. You are an inspiration to us all. Could you do a refresher for the PostresSQL tut?

  • @rp962
    @rp962 6 лет назад +1

    Well, this is one of the breakthrough demos of oauth present in youtube. Great job man.

  • @belscode3941
    @belscode3941 4 года назад

    Thank you brad for this awseome tutorial , just a note we should send only the id in the payload as far as read in articles

  • @markleodionisio2669
    @markleodionisio2669 4 года назад +1

    Thank you sir, youve been such an inspiration for me, get well soon.

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

    Great work helping the community in a short video with excellent and straight forward professional explanation.

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

    thanks traversy, such a legend for me and an inspiration...

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

    Very clear explanation. Thanks Brad.

  • @tusharmaurya1668
    @tusharmaurya1668 4 года назад +1

    This is exactly what i was looking for!

  • @MrXmen262
    @MrXmen262 4 года назад +1

    Thanks Brad , I hope that you do a full mean stack project

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

    you saved my day ! again!
    thank you brad!

  • @guyfrompei
    @guyfrompei 6 лет назад +1

    Very informative and easy to follow. Thanks!

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

    Thank you SO MUCH for your lesson. It really helped me a lot!!!

  • @urbanmunda2451
    @urbanmunda2451 4 года назад

    Really great explanation sir... Simple and worthy ...

  • @exapsy
    @exapsy 6 лет назад

    I love you. Thanks so much for the video. Crystal Clear in all aspects and I got the exact and even more information I needed.

  • @rupinderkaur304
    @rupinderkaur304 4 года назад

    Sir really awesome video. Made me understand completely about jwt

  • @varolo74
    @varolo74 6 лет назад

    Thank you from Italy, very clear and helpful!

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

    5 years later…. Still gold

  • @adamcrap
    @adamcrap 5 лет назад

    Awesome stuff brad as always!

  • @ZaidKhanPathan
    @ZaidKhanPathan 5 лет назад +1

    Those who wants to know, what next() function is? i.e. In verifyToken(req, res, next).
    verifyToken is a middleware function in express, which take req, res and next parameters.
    Calling next will move to succeeding middleware function.
    More details here: expressjs.com/en/guide/writing-middleware.html

  • @LongqiZhang
    @LongqiZhang 5 лет назад

    Make things pretty clear. Simple and Clean. Thanks.

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

    Woww this cleared my all the doubts. Thank you so much ❤

  • @alexcharlton2316
    @alexcharlton2316 6 лет назад

    Great tutorial, clear & simple to follow!

  • @xshad3x348
    @xshad3x348 6 лет назад

    Hi Brad, cool video and thanks, i needed a refresh! i think only that should be better to handle the verify in the middleware to keep the api clean, but great! thanks.

  • @norrisdamianus5250
    @norrisdamianus5250 5 лет назад

    very good, simple and clear tutorial

  • @akshayshelke5833
    @akshayshelke5833 5 лет назад

    A very nice tutorial sir , you r a role model for all of us , thank you sir !!

  • @kazz94
    @kazz94 5 лет назад +3

    Your explanation was perfect. Thank you so much!!
    You have my like and my sub :)

  • @dgdev1024
    @dgdev1024 6 лет назад

    Thank you for this! I can expand upon this and save a good few lines of code and a dependency with it.

  • @gk0r
    @gk0r 5 лет назад

    Excellent video. Short and to the point. Thank you for not building a client and using PostMan instead.

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

    Thank you so much..
    So clear and lucid explanation

  • @nimasoufiloo2626
    @nimasoufiloo2626 6 лет назад

    Perfect video as always. short and useful.

  • @DerJP
    @DerJP 6 лет назад +28

    One suggestion: Wouldn't it be more convenient to verify the jwt in the middleware and then attach the authData to the req object? This way you wouldn't have to verify the token in every route that you define. :)
    Otherwise, as usual, awesome tutorial, have a great great holiday!

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

    Very well explained . Thanks so much for this video

  • @niccubagonoc2153
    @niccubagonoc2153 6 лет назад +1

    Perfect! Thank you, Brad! My hero!

  • @yadavlokesh
    @yadavlokesh 5 лет назад

    Ton of thanks Brad for putting up this tutorial. it's really easy to follow and understand the every bit of code. Thank you again.
    Just a quick question, did you put up any tutorial for Login / registration user flow? If yes, please share link to the same.

  • @binayakgshankar9188
    @binayakgshankar9188 5 лет назад +1

    Wonderful tutorial. Please make a tutorial on Node+Express+Angular+JWT+Socket

  • @Dinesh-ql1ok
    @Dinesh-ql1ok 6 лет назад

    Congrats 🎊 Brad for 500k subscribers ✌️👍👏🎈🎁💐

  • @Huongpham-gz1vm
    @Huongpham-gz1vm 5 лет назад

    This turorial was help me solve my problem. Thanks!

  • @husniddinqurbonboyev2634
    @husniddinqurbonboyev2634 5 лет назад

    Thanks Brad for such a useful content!

  • @sirruffalot
    @sirruffalot 5 лет назад +1

    Thank you great tutorial. Helped me a lot.

  • @nhulongctk10
    @nhulongctk10 5 лет назад +23

    Why do we need the Bearer string in the authorization? instead of header['authorization'] = '' ?

    • @xNameless
      @xNameless 5 лет назад +45

      There are different types of Authorization, such as Basic, Bearer, Digest, HOBA, OAuth - even Amazon Web Services has one called AWS4-HMAC-SHA256. The idea behind it is allowing multiple ways of authenticating with the same API, or server. This dates back to the HTTP/1.0 standard from W3C :)

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

    Which playlist is this video in? This video will definitely help me to learn, thanks for uploading this video.

  • @sawyerrken8112
    @sawyerrken8112 5 лет назад +3

    Awesome tutorial. Thanks for the time and effort put into this. I would rather rename that verifyToken middleware to something else like getToken to better capture what the middleware does.

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

      Agree. Was about to comment this.

  • @eqdatshit8406
    @eqdatshit8406 6 лет назад +1

    Just what i needed !

  • @K-Raman
    @K-Raman 4 года назад

    Great video but for who already know why jwt is needed and has theoretical knowledge of token👍❤️👌🥳

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

    Thanks for all! Nice videos, you are the best!!!

  • @jarecturner5602
    @jarecturner5602 6 лет назад +5

    Again, you're amazing.

  • @JohnMcCullough97
    @JohnMcCullough97 6 лет назад

    Great video. Succinct and informative. Thanks. Merry Christmas.

  • @aparvaiz1999
    @aparvaiz1999 6 лет назад

    Brilliant video Brad! Thanks a lot!