Session Authentication in Express

Поділитися
Вставка
  • Опубліковано 16 лис 2018
  • Hey guys, in this video we will implement session-based authentication in Node.js using Express, a minimalist framework for web servers, and express-session, an Express middleware for server-side sessions. We will develop the login, logout, and register functionality, as well as discuss security and scaling. To get the most out of this video, I recommend that you start with Authentication on the Web • Authentication on the ... which lays the theoretical groundwork for this episode.
    We begin with a detailed walkthrough of express-session and its configuration options. Once we set up the sessions, we will wire them up to server routes, which will entail input validation, user lookup, and error handling. We will also protect the routes with guards, or middleware, to check for authenticated users. Finally, we will demo the app, and observe the session lifecycle to nail down the mechanics of session management. We will then conclude with final thoughts about enhancing security and deploying to production.
    express-session www.npmjs.com/package/express...
    Redis session store github.com/tj/connect-redis
    Notes github.com/alex996/presentati...
  • Наука та технологія

КОМЕНТАРІ • 270

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

    Hey guys, it's Alex again. I know the config for express-session can feel a bit daunting at first (I had to read through very carefully myself), so I created a reference guide that explains each option in detail. You can find it along with other presentations on GitHub github.com/alex996/presentations/blob/master/express-session.md

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

      please if i may ask ,which algorithm used to hash session ID sent in the cookie?

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

      @@medi7573 HMAC SHA256 github.com/tj/node-cookie-signature/blob/4496ae0795ef0fb6303184e1f44370546663e2e4/index.js#L20

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

      Thank you. It is really daunting.
      The way the documentation of express-session is written assumed that readers already know a pretty big deal.

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

      my app works fine on localhost but it doesn't login when i deploy it to heroku. Any help?

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

      Thank you so much

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

    For people who are watching this video : at 20:10 in the newest versions of Express, body-parser package is now built-in in Express. All you need to do is use it as a middleware like this : app.use(express.json()) and app.use(express.urlencoded({extended: true}))
    Thanks for the video!

  • @MarkDeibert
    @MarkDeibert 4 роки тому +11

    A year and a half later and this is still *the best* express-session tutorial. By far! Thank you for this Alex!

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

    At first I was like "wow dude this guy is way too fast", but after watching a lot of many other tutorials I realized yours are actually the best ones, straight to the point, while most of the others just do a 2 hours video for something that could've been explained in 30.
    I surely can't fully understand this in 40 minutes, but I sure can pause the video and/or rewatch all the parts I didn't get at first and I'll know that I won't waste time since there's no dead moment
    Great job and thank you :^D

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

      yeah this video is still sol relevant and to the point

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

    This video has been so useful to me that I wish I could 'like' it many times. I've checked it three times already as a refresher. Thank you, Alex. 👍

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

    Also, we can chain our routes if the have the same path like, "/login". Example:
    app.route("/login")
    .get(function (req, res) {
    res.send('Login page"')
    })
    .post(function (req, res) {
    res.send('post login')
    });

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

    Hey there! I have to give you a huge thumbs up! This is one of the best tutorials I have ever watched! Everything you said made sense and you didn't speak to hear yourself talk. You got straight to the point and everything was clear and concise. Thank you! I liked and subscribed and will be watching more of your videos. Once again...GREAT JOB! Wish others made excellent tutorials like this one. Take care, Lee

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

    Coming from perhaps the absolute beginner in Node, thank you. I not only understood the basics of session management, you helped me improve my workflow as well. Currently trying to adapt this with your suggestions (database, validation, hashing...). Running into issues, but I'm confident I'll crack it soon. I'm grateful. Have a sub.

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

    Great job Alex. Really useful. I was doing well until around 30:00. It got over my head afterwards. But I discovered your new Authentication/Authorization series and will be going those for sure. Thanks for getting me up and going in a very short time. Cheers.

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

    I made a ridiculous amount of progress just from a few of your videos! My latest commit is the biggest I've ever made! Keep it up, and thank you!

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

    I'm still in the middle of this.. but this is way too good so far. It's clear, concise and to the point. Thanks for your time buddy!

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

    love the pace, love the clear language. thank you !! :D

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

    Thank you for this video. This is a great tutorial, I could learn, understand, and now I'm going to try to implement all the 'to dos' by myself.
    Even today, almost 3 years later, It is not so common to find content abou session auth (JWT all over the place), let alone a so well produced material, so congratulations and thank you again.

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

    Saved my life. So many hours and days of struggles and finally I have solved my problem thanks to you! Keep up the good work!

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

    This channel is probably the best thing I found on the internet

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

    A BIG THANK YOU for the level of detail and simplicity of this authentication tutorial, it has cleared my confusion on this topic as to when does the client know the user is still connected, or how the session is checked and validated! I've honestly seen this video and watched it on 1.5x before and I didn't get the pointers, so I watched a few more videos on the same topic but got really confused because there's no clear explanation how they validate the session data, how logging in and out works when the session is created/destroyed or how the server uses the session to determine the current user! So I finally watched your video on normal speed, and got the ideas right! I know how JWT authentication works already, it works on the payload, but this session authentication using express has eluded me, so thanks again!

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

    tried many tutorials for authentication.This is by far the best explaination I have listened to.Cheers :)

  • @SwapnilSoni
    @SwapnilSoni 4 роки тому +5

    Simple, clean and quality voice == best tutorial

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

    Great tutorial!! You explained it so clear and simple! Thanks!

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

    I liked this tutorial. Best among youtube videos I watched to meet my session management requiremnent with Node.js

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

    Thank you so much alex. From last 3 days i was trying to learn how to implement session based authentication and i failed but from this video, I learned it easily.

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

    Thank you BRO! From Russia with love

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

    This is the best video on NodeJS authentication I have seen. Thanks

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

    Thanks for this tutorial. Completely clear of express-session

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

    Wow u have even written everything in the comments on the notes too. That is really awesome.

  • @bright.fantine
    @bright.fantine 4 роки тому

    Hey I'm french and I watched your video , which helped me a lot for my backend school project... You speak a little to fast but I understood almost everything I needed to haha. thanks!

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

    Thanks man! This is exactly what i looking for. Nice work!

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

    Very good explanation. I'm doing this for the first time and it makes complete sense

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

    You are so knowledgeable. I've leant quite a few useful things that I didn't know.

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

    You explained this really well, thank you.

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

    Exactly what i was looking for. Thanks Alex. Love from India

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

    You are the realm the real teacher, thank you!

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

    Its good to watch u video. Content is explained very nice and straight to the point. Keep up making more video.

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

    Exactly that i was looking for..Thank you so much

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

    thanks for the video, it helps a lot
    waiting for the next tutorial!

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

    Bro u r seriously amazing. This is the first video I have watched of urs and I am a fan of ur to the point videos with all the minor explanations as well.
    Btw I watched at 1.5x speed 😂. Perfect

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

    Omg Struggled for so many days and now it's clear!

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

    thanks for the video, its helping me with my project!!

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

    Great, very clear explanation!

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

    Many many thanks for this tutorial. Ofcourse I understood the principle of cookies but never knew how to use it in my requests. Thanks!

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

      Sir i didn't get why you use redirectHome into login POST request?

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

    That's a really nice tutorial. Impressed !

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

    you are one of the best Sire! Thanks a very lot!

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

    This was super helpful. Thanks you very much.

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

    Thanks, Alex, that was amazing!!!!!!!!!!

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

    Absolutely the best Alex, no doubts. I own you a lot

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

    Great explanation , thank you !

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

    thank you so much you don't know how much this helped

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

    Thank you for this very useful one!

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

    Wow you explain very well! thanks

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

    So glad I found this video

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt 5 років тому

    Thank u for this awesome video and series

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

    Happy new year dood!

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

    thank you for sharing how session express works.

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

    Thumbs up, you are a real prodigy!

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

    Great tutorial , Thanks buddy!

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

      Sir i didn't get why you use redirectHome into login POST request?

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

    thank u so much, I really benefitted after sean this video

  • @heunsigjo7484
    @heunsigjo7484 11 місяців тому

    awesome tutorial. Thank you so much

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

    Thanks and keep up the good work! :)

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

    This is the best tutorial out there.

  • @user-pt7tv
    @user-pt7tv 4 роки тому

    Very informative, thanks!

  • @kofuku1344
    @kofuku1344 4 роки тому +6

    On my recommended, Im so glad I clicked it lol
    Sub!!!

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

    you have really great knowledge

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

    Thank you for this amazing series....🤗👍👍

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

      Sir i didn't get why you use redirectHome into login POST request?

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

    this video is awesome, thanks man

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

    Thanks for this amazing tutorial

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

    Super useful. Thanks :)

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

    really helpful i learn a lot today....kep it up...thanks

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

    Amazing job man!

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

    this is an amazing tutorial!

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

    quick and best explained tutorial.

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

    From one random guy to another, Thank you. :)

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

    Thx for great tutorial :D

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

    hi, This was a great training. Thank

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

    Great tutorial on authentication.

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

    How do I give this video 10 thumbs up?
    It deserves it.

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

    you are great man thanks

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

    Thank you this was helpful

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

    You Motivate ME .... Thanks man --->

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

    Wow. Perfect. A way to create a register, login, session and logout in 1 video. Just what I needed.
    I looked at your presentations on github and you go into detail on how to use redis to store a session ID. Any chance you can give a quick explanation on how to store the session ID on mongoDB? Thanks

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

    Thank you master!

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

    Amazing tutorial 🤩

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

    Great Tutorial Man :)

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

    UA-cam clearly works for the sake of this channel.

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

    This was really helpful! FYI, the custom middleware redirectLogin wasn't working me for some reason. I had to use the hasOwnProperty method:
    const redirectLogin = function(req, res, next) {
    if (!req.session.hasOwnProperty.call(req.session, 'userId')) {
    return res.redirect('/account/login');
    }
    next();
    };

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

    RICARD, QUE GUAPO DEJAR A ESTE CHAVAL ENSEÑARNOS LO QUE TU DEBERÍAS EH, TA WAPA LA FAENA DE PROFE DEL CHILL HACIENDO TUS OTROS TRABAJOS EN VEZ DE ENSEÑARNOS EHHH.
    Np, te quiero

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

    At first I thought... damn!! dude has no chills at all, then with the little humour at around16:10 I was like, whew! he's human after all :) great video

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

    Thank you!

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

    helped a lot thanks

  • @user-bo4qo1vz1j
    @user-bo4qo1vz1j 4 роки тому

    Thanks a lot!

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

    Thank you~

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

    Thank you! Was that real speed of your coding? Amazing!

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

    Thank you so much for this video....let say if i save token in cookie at client shall i need to manually send it along with header OR it will be auto sent if i stored in cookie..?

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

    Excellent

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

    very helpful

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

    THANK. YOU.

  • @hungnguyen-iv4qg
    @hungnguyen-iv4qg 3 роки тому

    Thank you

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

    amazing

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

    thank you

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

    Hey, I would like to clarify some doubts. If the maxAge is set to a timethen wouldn't the cookie be deleted by the browser even though there is user activity? Wouldn't that affect user experience? Is there some kind of best practice?

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

      express-session has a rolling option to extend the expiry date on subsequent requests. You may want to be careful so as not to extend it indefinitely though; that's where the absolute timeout comes in

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

    Great video! Is there a copy of the code available anywhere?