JWT Authentication Tutorial - Node.js and React

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

КОМЕНТАРІ • 191

  • @jeminaldennis325
    @jeminaldennis325 3 роки тому +43

    you're the MAN... long live Lama Dev

  • @augustzilakovs3105
    @augustzilakovs3105 11 місяців тому +2

    After hours of googling and looking at other guides this is by far the best one. Straight to the point and covers exactly what you need to know about how to use JWT. Thank you.

  • @javhaasuhochir8126
    @javhaasuhochir8126 2 роки тому +11

    You are the best teacher I ever seen In my school and UA-cam. So easy, so clear. This is the best tutorial I ever seen in my life. thanks my friend. You really saved my life from struggling with jwt for months. Really THank you man.

  • @Pareshbpatel
    @Pareshbpatel Рік тому +2

    A very easy-to-follow tutorial on JWT Authentication in Node/React. Thank you, Lama Dev
    {2023-03-22}

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

    Best channel on UA-cam for MERN stack mastery.

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

    YOU TOOK MY REQUEST THANKS, DUDE! ALWAYS WILL GET SUPPORT.

  • @msunrl8577
    @msunrl8577 Рік тому +1

    dude wtf, best tutorial of jwt, after checking like 10
    Thanks!

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

    Thanks Man, I am from Venezuela, I don't understand much English, but I can tell you that your video is the best, coz there are not videos in Spanish that explained as I can refresh the access tokken.

    • @Jimmy-zt5io
      @Jimmy-zt5io Рік тому

      yo tmb soy de Venezuela bro, lo malo aqui es que si uno refresca la pagina se se cierra la sesion del usuario

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

    thank you for tutorial, JWT is mandatory nowadays.

  • @khalidrafi1111
    @khalidrafi1111 5 місяців тому +1

    UPDATE: at 55:20 the correct import syntax would be "import { jwtDecode } from "jwt-decode";". Then also update the initialization of the decodedToken later on by replacing "const decodedToken = jwt_decode(user.accessToken);" with "const decodedToken = jwtDecode(user.accessToken);"

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

    Thank you ! I was waiting for the full reactjs+nodejs make e-commerce website

  • @wasteghost
    @wasteghost Рік тому +1

    Really helpful tutorial, I couldn't find a good one for some time and yours is perfect!

  • @papesonko
    @papesonko Рік тому +1

    Simple clear and concise, thank you very much lamadev !

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

    I was waiting for the jwt tutorial. It's the best as always

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

    The only insightful video on jwt, THANKS!

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

    best tutorial to learn understand and implement JWT. Great work!

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

    It seemed really easy thanks for the tutorial.. Gonna revise again while making netflix clone (streaming platform) ☺️☺️

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

    5 Minutes in I knew it's gonna be a great tutorial. Was right. Thank you!

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

    I love you so much. I want to support you but I lost my job for 4 months.

  • @Matheus-lk9lh
    @Matheus-lk9lh 3 роки тому

    Your content is the best on internet, thank you very much

  • @next_codes
    @next_codes 3 роки тому +18

    Hi Lama, Thanks for this great Tutorial, I want to specially request if you'd explain how to store the refreshToken in real Mongodb Database and not locally as an Array. I also would love to see what the refreshToken model looks like. would you create a separate schema for the refreshToken or just add it to the existing user model as type "Array" Please anyone can help me in this area, it's really challenging me.

    • @klhmia
      @klhmia 2 роки тому +2

      I think it's most secure to create the 'refreshToken' array as part of the User schema. This is what I've done. Then, instead of checking the local array as in this example, we would call an async method to find the user in our DB by Id (which we get after verifying token payload and returning the payload in the callback of 'jwt.verify()'), then check this User.refreshTokens array to see if it includes the refresh token and either add or remove depending on your action.

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

      I would also like to see that and the other way that @Karsen LH said

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

      I have been able able to resolve the challenge. Thanks

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

      @@next_codes You did it by yourself or with help of other tutorial?

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

      @@jimenakatu1178 I decided to try completing a full CRM /ERP project and along the Line I met more complex logics and situations. That exposed me to wider knowledge, turning back to check on this one, i found out it was just a walk over. I did it myself.

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

    really awesome tutorial, love the way you explain things

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

    You are the best Lama~!! The content is so informative!! Thank you so much~!

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

    Thank you very much, I really love your videos, thank you for making us better everyday

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

    Love your videos, thanks for the uploads

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

    you are my best teacher.

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

    Thank you Lama, you are the greatest

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

    I love your videos!. Thank you so much, greetings from Colombia

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

    Thanks for your great tutorial

  • @ianfrye8988
    @ianfrye8988 10 місяців тому

    I love your videos. Thank you for this!!

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

    Why is there a different endpoint to exchange the refresh token for a new access token? This seems like it would make building the UI very difficult as you need to try to hit the endpoint, see if the error is unauthorized, if it is, get a new access token and try again. I think this should be done automatically on every request. If the access token is expired, given them a new access token and continue processing the request.

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

      thats what he did right?

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

      @@saravanan925 He created an endpoint to exchange the refresh token for an access token. Is that what you're asking?

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

      Did you watch till the end? There's a function on the frontend that automatically refreshes the access token if it is expired (No UI included)

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

      @@marcusaureliusregulus2833 yeah, why? why not do that on the backend?

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

      @@TomDoesTech I guess it could work either way.

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

    very well explained !! thank you for JWT tutorial

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

    Helpful information, thanks

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

    Another quality content.

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

    Thanks very much, tutorial was very helpful.

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

    Awesome JWT tutorial

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

    You are doiing an amazing Job. Thanks

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

    Nice work. Just want to know why not making use of cookies ?

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

    Abi videoların baya güzel yeni fark ettim kanalı. :) Teşekkürler videolar için.

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

    when do we use JWT over something like Auth0 or Google Firebase Authentication?

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

    Thanks for your great tutorial Şafak. Should we use NextJs framework for fullstack javascript applications? what do you suggest ?

    • @RiteshKumar-dx8wj
      @RiteshKumar-dx8wj 3 місяці тому

      you were ahead of your time when you asked this question XD

  • @Harsh-eo7xo
    @Harsh-eo7xo 3 роки тому +1

    You are amazing.
    Love you.

  • @bingsenlim3121
    @bingsenlim3121 3 роки тому +16

    Hi I have a question. It seems like in this tutorial, you are always logged out after you refresh. In your React Social Media tutorial, you stored your user object in localStorage to let the user remain logged-in after page refresh. My question is, since you need accessToken to do stuff like delete posts etc, wouldn't you need to store accessToken in localStorage also? If you don't store accessToken in localStorage (only store in useState or useContext), after the user refresh the page he/she cannot delete post anymore since the accessToken state/context will be null. But if you store it in localStorage, doesn't this defeat the purpose of jwt, because hackers can see it from localStorage. So how do I use JWT, and at the same time, pass down this accessToken if my user refreshes the page?

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

      same problem . did u solve it ?

    • @bingsenlim3121
      @bingsenlim3121 3 роки тому +13

      @@developerbox3079 This is what I saw from someone's post from stack overflow:
      1.When you do log in, api sends 2 tokens (Access token, Refresh token) in response to the client.
      2.The access token will have less expiry time (may be 15min) and Refresh will have long expiry time (may be 7 or 60 days )
      3.The client (Front end) will store refresh token in his local storage and access token in cookies.
      4.The client will use an access token for calling APIs. But when it expires, pick the refresh token from local storage and call auth server API to get the new token.
      5.Your auth server will have an API exposed which will accept refresh token and checks for its validity and return a new access token.
      6.Once the refresh token is expired, the User will be logged out.

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

      @@bingsenlim3121 thank you! I will search more about it

    • @SF-qf4fu
      @SF-qf4fu 3 роки тому +2

      that's because he is giving the token a expiration of 5 sec, put 1d or in miliseconds 86400

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

      Please can you tell at which section of social media tutorial he save user in localStorage. I want to see but unable to find that part.

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

    You are just fantastic

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

    lama please make video on otp and email verification. You have helped us alot thank you so much

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

    Awesome explanation. Thanks.

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

    my brain explode XD ,ty for the great content

  • @nuruhussen-wo9wg
    @nuruhussen-wo9wg 6 місяців тому

    best explanation

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

    This is a really straightforward tutorial. Thanks Lama. Is there a reason why we must include "Bearer" in the authorization string?

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

      Is "Bearer "

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

      You can add anything before the access token but it should be separated by space.
      eg: BeN .
      The concept is to split the value of the key 'authorization' in 2 parts. So after splitting, 'BeN' & '' will come in an array. You have to take only the real token so that it can be verified with the generated one. That's why lama has used
      const token = authHeader.split(" ")[1] ;

  • @user-ul2mw6fu2e
    @user-ul2mw6fu2e 3 роки тому +1

    why you dont use refresh token in ecommerce project.I am little bit confuse with that

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

    Very helpful content thanks👍

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

    Thanks for this great Tutorial...

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

      hey at 23:27 can you explain me what "req.user = user "doing ?

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

    God bless you

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

    thank you sir i was wating for this

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

    super nicely done tutorial

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

    i faced a problem in auth so i am writing this to help other :
    if your access token expire after some time then In axios Instance interceptor add "" config.headers["authorization"] = "Bearer " + accessToken; "" because after the token is refresh the axios instance headers will not update automatically .
    I Hope i was able to explain what i want to say.

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

    awesome tutorial

  • @amanagarwal8009
    @amanagarwal8009 2 місяці тому

    i have a question. When i reload the page... it will ask for login credentials again and again.... How do i tackle that?

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

    It would be wonderful if you could use this tutorial on blog project. I really confessed. I can do only Auth . But i can not implement on blog project.

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

    Perfect!!!!!! i will buy u a coofe! cheers from Perú
    btw you should make an example like Google auth i mean when you login from other country or device and google send you an alert if you are loggin from there... i hope you can understand thanks!

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

    Thanks for this tutorial

  • @birkbirk1204
    @birkbirk1204 Місяць тому

    hocam selamlar, ben daha yeni başladım kodlamaya, videonuzu izledim teşekkür ederim sonra, uyguladım kendi uğraştığım bir siteye, access token ve refresh token elde ediyorum. refresh tokenları backende yönlendirdim orada tutuyorum. örneğin bir post atma işlemi yaparken kullanıcının access tokena erişimi olduğunu kontrol ediyor ekstra olarak, backende bakıp refresh tokenlede eşleşme şartı koydum. video başında dediğiniz gibi kullancı 2 nin sadece ismini bulup işlem yapamıyor. yeterli midir basit bir websitesi için

  • @MohitKhatri-y5i
    @MohitKhatri-y5i Рік тому

    You're gem 💎!

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

    Are you from nepal coz you have name started with lama and here we have lamas and sherpas in himalaya😊

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

    Hi Lama, very appreciate for your tutor, If possible can you teach us how to pass axiosJWT in this video to contextAPI

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

    But, we miss logout from react. How can we stay logged in. Should I store refresh token in browser cookies or local storage

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

    Hi Lama, Thanks for the tutorial. I'm however stuck when logging out. I'm trying to log out by redirecting back to another page but it keeps failing. Kindly assist

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

    thank you ,great tutoriel

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

    Great tutorial thx! For registering, could you show how to implement admin approval needed before users can log in? This way only approved friends can create an account and sign in.

    • @klhmia
      @klhmia 2 роки тому +2

      Just create a separate database of 'temp' users to hold registered users waiting for approval. Allow admins to elevate those 'temp' users to 'active' users, moving them to the 'active' database which is the one you would used to sign users in.

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

    Why did you have to put Bearer in the request header value? I don't understand. Why can't we just put the token there without the bearer.

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

    I have a confusion. While logging out since we are only removing the refreshToken and not the accessToken, wouldn't they be technically logged in for the duration of the access token?

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

      yes they would! Thats why you don't keep the duration of accessToken for more than 10-15 mins.

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

    Lama we can't add new lines in the blog description. Can you please see to it?

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

    help, I'm doing this for my website but when user refreshes the page, the status is gone... wtf? Also, what's the point of a refresh token if you only bind it to a specific axios instance. So, if I go afk for 15 mins, I'll still get logged out, unless i delete a user.

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

    Do you have a project to configure interceptors for the entire app?

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

    Why you are using state variable to store the user Data ? Can we store it in cookies/local storage ?

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

    You are the great 👍👍 man

  • @ananthac6472
    @ananthac6472 5 місяців тому

    why should we use the word Bearer before the token? , thanks :)

  • @mrlectus
    @mrlectus 3 місяці тому

    What i was looking for was req.user = user;

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

    awesome video

  • @codersdance
    @codersdance 6 місяців тому

    So with refresh everything will be lost beacuse of the state?

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

    Okay I have a question.anyone help ?
    When logging out why do I need to verify?I mean the goal is to delete the refresh token from the array.Its all about the refresh token!
    So why bother sending the access token as well??
    We did not send the access token when refrshing,,because that was also about the refrsh token!

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

    What do delete user means? If the user is deleted how were we getting the access token with new refresh token even after deleting when we refresh?

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

    i didn't understand "req.user = user", req.user stand for what? in 23:30

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

      here we have assigned new property to req as like req have body , header now req also have user property. I hope u got it

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

    Hi,i tried using it with the blog project i'm stuck.
    Could you do the jwt tutorial with the blog project please

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

    Thanks lama

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

    How am I supposed to store the refresh token in my mongo database? should every user have their own refresh token so i call it like 'user.refreshToken' or should they be completely separate from users ?

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

      You can add a new refresh token in users model.

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

    the proxy doesn't work, any advice? :/

  • @adarsh-chakraborty
    @adarsh-chakraborty 2 роки тому

    Should i save refreshTokens in my User model on mongodb?

  • @sayidradhinpm8761
    @sayidradhinpm8761 Рік тому +1

    Amazing....

  • @okonkwo.ify18
    @okonkwo.ify18 2 роки тому

    Best practice is to store refresh token in an httpOnly cookie

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

    love you so much

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

    Is it possible to store JWT token on Server side not on Client side Cookies???

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

    can you do some MERN Ecommerce tutorial with authenticatiom

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

    so refresh token is only there for refreshing access tokens?

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

    hi, I want that when a person reload page then it still logged in. How to implement this in jwt. I implemented in using local storage but its not good as discussed.

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

      have you figured it out?

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

      @@Ahmadavocado yes. Just simply store the user id and jwt token in local storage. Then u are good to go

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

      @@islamicinterestofficial why not store it in cookuies and enable httponly? localstorage is not so safe

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

      @@Ahmadavocado Its upto you. By the way cookies are also not safe. We're just storing which no one can understand because we only know the key not anybody

  • @thatsmeee111
    @thatsmeee111 9 місяців тому

    Who can tell me why we create a new instance axios please i can't understand that

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

    what keyboard you are using?

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

    Please can some one tell me how to link backend with React?I tried to login many times but nothing happened :(

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

    Thanks man

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

    How can I make the client part tho..