NodeJS & Express - Google OAuth2 using PassportJS

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.พ. 2021
  • Learn how to protect your API routes using OAuth2 with Google as an identity provider.
    We will be creating a REST API using NodeJS & Express. Then we will be protecting some API routes behind authentication. These routes will require the user to authenticate using OAuth2 with Google. We will be using PassportJS (www.passportjs.org/packages/pa....
    Check out the code on Github: github.com/kriscfoster/node-g...
    Don't forget to Subscribe here: / @krisfoster1
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 114

  • @crazedbrazy617bh
    @crazedbrazy617bh 2 ปีที่แล้ว +6

    Great video, have been struggling with this for the past few days, your the first one to tie in all the pieces, authentication, user session, and log out. Thank you good sir!

  • @ilanle
    @ilanle ปีที่แล้ว +3

    I have seen a lot and I am impressed by how you manage to get a confusing subject through and so consicely and simple to understand

  • @jeanlucas9702
    @jeanlucas9702 ปีที่แล้ว +7

    Dude this is the best video I've ever seen about Google OAuth with passport you have no idea how many videos I watched to use Google OAuth with passport a big thank you from Brazil

  • @arnabbanik6403
    @arnabbanik6403 2 ปีที่แล้ว +7

    Really amazing tutorial, actually the only one where I understood what each part of the code was doing.

  • @LeighBriody
    @LeighBriody ปีที่แล้ว +1

    Such a lack of videos explaining this topic , thank you man !

  • @tonienguix4834
    @tonienguix4834 2 ปีที่แล้ว +41

    one of the best code tutorials I've ever watched. Clear, concise and simple. Fantastic video 👏

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว +1

      Thank you for the kind words!

    • @tonienguix4834
      @tonienguix4834 2 ปีที่แล้ว

      @@KrisFoster1 hey Kris! sorry I sent you a question on twitter. How can I send the user object to the client? thank you!

    • @orenzbeda6072
      @orenzbeda6072 2 ปีที่แล้ว

      try this one: th-cam.com/video/5WehZTrgG8o/w-d-xo.html

    • @uk4876
      @uk4876 ปีที่แล้ว

      Yeah I agree

    • @anfaenfpanthqwefbdas
      @anfaenfpanthqwefbdas ปีที่แล้ว

      Absolutely, straight to the point and no waste of time

  • @itsnobledean9450
    @itsnobledean9450 5 หลายเดือนก่อน

    This video was full of value. I was able to complete it, but even better, I was able to complete it, and now I feel comfortable with utilizing passport/google-oauth2 in my projects moving forward.

  • @codingrussell138
    @codingrussell138 2 ปีที่แล้ว +10

    Had to comment.
    Went from never working with passport or Oauth to implementing 5 different types of login on my test app. Amazing tutorial.

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว +2

      That's great to hear @Dev Dev Goose. Well done!

    • @immensives147
      @immensives147 หลายเดือนก่อน +1

      Damn 🙄

  • @abdulsamihamedi5584
    @abdulsamihamedi5584 5 หลายเดือนก่อน +1

    it helped me bro, always keep your tutorials as clear as this!!!

  • @GlitchedFailure
    @GlitchedFailure 4 หลายเดือนก่อน +1

    To the point, goes step by step. Well done!

  • @Bl4sTTer
    @Bl4sTTer 2 ปีที่แล้ว +1

    Most helpful video I found about OAuth2 with Node.
    Thank you

  • @immensives147
    @immensives147 หลายเดือนก่อน +1

    Other than him, no one was helpful to me for this particular problem, Thanks Man 👍

  • @firojsiddiki2750
    @firojsiddiki2750 2 ปีที่แล้ว +2

    Simplest Code and Simplest explanation. awesome

  • @dremmaokoh5011
    @dremmaokoh5011 ปีที่แล้ว +2

    Great video. Short and precise . Thanks alot

  • @haoyuandong
    @haoyuandong 3 ปีที่แล้ว +4

    Thanks for this simple but clear video.

  • @dwijvirani7919
    @dwijvirani7919 ปีที่แล้ว

    Thanks bro for the detailed explanation. It was a great help

  • @generalknow_ledge
    @generalknow_ledge 7 หลายเดือนก่อน

    Your explanation is awesome dude thankyou very much for the knowledge ❤

  • @taufiksatya9695
    @taufiksatya9695 11 หลายเดือนก่อน

    You're the best explain for this mate.

  • @DEV_XO
    @DEV_XO 3 ปีที่แล้ว

    Yo mate, great video.
    Do you know if there is a way to get rid off the "#" at the end of the route?
    I mean, redirecting without the slash sign at the end.
    Ty!

  • @TOMGAMING-hy9hi
    @TOMGAMING-hy9hi 2 ปีที่แล้ว +1

    a very big big thanks to you sir for giving such an amazing explanation.............!!!!!!!!!!

    • @orenzbeda6072
      @orenzbeda6072 2 ปีที่แล้ว

      Maybe you will like this also so much simpler: th-cam.com/video/5WehZTrgG8o/w-d-xo.html

  • @indraargamuria4774
    @indraargamuria4774 2 ปีที่แล้ว

    This is such a great tutorial Mr Kris
    I can retrieve the email displayName with no problem, but how do I know the other Function? Like Retrieve the photo profile or other information?
    I hope you see my question here, Cheers to you!

  • @codinginflow
    @codinginflow 2 ปีที่แล้ว +2

    Thank you, this helped me a lot

  • @ayhamahmad6184
    @ayhamahmad6184 2 ปีที่แล้ว +1

    Thank you so clear explaining

  • @poeeiphyu6931
    @poeeiphyu6931 3 ปีที่แล้ว

    Thanks for your sharing. great tutorial.

  • @harounach
    @harounach 3 ปีที่แล้ว +1

    Thanks Kris for this video

  • @info662
    @info662 2 ปีที่แล้ว

    Subscribed!
    Hey, what if I wanted to return a token instead of a session id?

  • @vidyashet800
    @vidyashet800 4 หลายเดือนก่อน

    Was so confused with Google Auth, Thank you so much for this wonderful tut

  • @andriizilnyk4927
    @andriizilnyk4927 2 ปีที่แล้ว +1

    the best work bro, thanks a lot...

  • @blacktipe9922
    @blacktipe9922 2 ปีที่แล้ว

    can we somehow edit this page with some better style? Like an Logout button on the top right corner and some general design instead of the white page?

  • @user-gv2cj5so6q
    @user-gv2cj5so6q 2 ปีที่แล้ว +1

    Great video, made it so easy. Tnx

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว

      Thank you for the kind feedback & glad you enjoyed!

  • @andrews13
    @andrews13 3 หลายเดือนก่อน

    So, will the Node.js application use the email address as some Primary Key in the database to identify a user?

  • @freddy9292
    @freddy9292 3 ปีที่แล้ว +1

    best tutorial 2021

  • @kelwong116
    @kelwong116 2 ปีที่แล้ว +1

    Good video! Thanks for sharing!

    • @orenzbeda6072
      @orenzbeda6072 2 ปีที่แล้ว

      Wil you try this: th-cam.com/video/5WehZTrgG8o/w-d-xo.html

  • @omarruder4129
    @omarruder4129 7 หลายเดือนก่อน

    Do you know how I can use this same session in my google extension, so that it automatically logs in the user to my extension?

  • @harpuneetkalsi5960
    @harpuneetkalsi5960 ปีที่แล้ว

    is cookie saved in browser in case of token based authentication, i can't find cookie in my browser although when i am am using cookie based authentication , then only i can see cookie saved in my browser??

  • @pritamdas1156
    @pritamdas1156 2 ปีที่แล้ว

    how do we use(logIn with google) password js with express js in react native

  • @chris0628
    @chris0628 ปีที่แล้ว

    Excellent tutorial 👍🏽

  • @Robytsu
    @Robytsu ปีที่แล้ว

    So no DB to save the session?

  • @willysnowman
    @willysnowman ปีที่แล้ว +4

    Below seems to work for logout needing a funcion issue in latest version of express.
    app.get('/logout', (req, res, next) => {
    req.logout(function (err) {
    if (err) {
    return next(err);
    }
    // if you're using express-flash
    // res.flash('success_msg', 'session terminated');
    res.redirect('/');
    });
    });

    • @avalancherelapse
      @avalancherelapse ปีที่แล้ว

      very important comment here, thank you! kris foster should pin this!!!

    • @marcofari532
      @marcofari532 3 หลายเดือนก่อน

      Yes req.logout() needs a callback

  • @usmanshahid1557
    @usmanshahid1557 ปีที่แล้ว

    bro if we login again after logged out ,then popup window did not appear ,why ?

  • @w.cahyanto7924
    @w.cahyanto7924 ปีที่แล้ว

    Great tutorial video😀😀

  • @ledeveloperstack7421
    @ledeveloperstack7421 3 ปีที่แล้ว

    help, I get an error of type TokenError: client_secret is missing

  • @osamaayub6004
    @osamaayub6004 หลายเดือนก่อน

    Cannot GET /auth/google/callback getting this error

  • @user-ky1ug2ex5x
    @user-ky1ug2ex5x 11 หลายเดือนก่อน

    Thanks for the tutorial

  • @elmasx
    @elmasx ปีที่แล้ว

    Hi Kris. How can we send to mongodb? Thanks

  • @atv8992
    @atv8992 3 ปีที่แล้ว

    InternalOAuthError: Failed to obtain access token
    why am i getting this?

  • @thalist5522
    @thalist5522 ปีที่แล้ว +1

    Thank you so much!!!

  • @kirillpavlovskii8342
    @kirillpavlovskii8342 2 ปีที่แล้ว +1

    You used your personal client google id, but how to get it from the user when he hit use button authenticate with Google on my website ?

    • @orenzbeda6072
      @orenzbeda6072 2 ปีที่แล้ว

      This is simpler: th-cam.com/video/5WehZTrgG8o/w-d-xo.html

  • @timlinator
    @timlinator 2 ปีที่แล้ว

    When I click my auth link it just spins.

  • @boot-strapper
    @boot-strapper 2 ปีที่แล้ว

    2017 style express, niiiiice

  • @jay_0906
    @jay_0906 9 หลายเดือนก่อน

    the best video I’ve ever seen👍

  • @Dihan-fh5yx
    @Dihan-fh5yx 6 หลายเดือนก่อน

    It works from browser, but I can't get it working from postman. Even after getting the access token and adding it to authorization header. Can anyone help?

    • @nazirkhan-jj8mz
      @nazirkhan-jj8mz 2 หลายเดือนก่อน

      As you have to use your google account which is supposedly the logged in google account in your browser but not in postman. That's the reason its not working in the postman

  • @vishaldinesh
    @vishaldinesh ปีที่แล้ว

    Hey kris thanks a lot man

  • @juanisidorogarcia1699
    @juanisidorogarcia1699 2 ปีที่แล้ว +4

    great job! Thank you very much for the content ^^
    A query, what is the difference between oauth2 and oauth20?
    - Passport-google-oauth20
    - Passport strategy for Google OAuth 2.0
    Thank you so much for everything!

  • @Speztra
    @Speztra 2 ปีที่แล้ว +1

    I love you for this.

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว

      Thank you, glad you enjoyed!

  • @ABUTAHER-wg7gz
    @ABUTAHER-wg7gz ปีที่แล้ว

    But show req.user=undefined

  • @W_Wadu
    @W_Wadu 8 หลายเดือนก่อน

    15:08 What do you mean when you said we need to allow the user to be sent as part of the request? I don't really get it.

    • @W_Wadu
      @W_Wadu 8 หลายเดือนก่อน

      And how does express-session solve this?

    • @artax5005
      @artax5005 8 หลายเดือนก่อน +1

      Express-session allows to create a session (req.session object) and can store the session into a database. To store a session where an user is authenticated, Passport comes into play, it's Passport with the serializeUser function that saves inside the session created by Express-session, a property => passport: {user: id} and then Express-session can store the session + the authenticated user inside a db and the server send an http response with the header set-cookie and now the browser saves the cookie and the user is authenticated. If you want to undersand how it works in details, watch the local authentication with Passport here (the first 3 hours) th-cam.com/video/F-sFp_AvHc8/w-d-xo.html.

    • @W_Wadu
      @W_Wadu 8 หลายเดือนก่อน +1

      @@artax5005 Wow this helps, thank you so much

  • @yabuking84
    @yabuking84 2 ปีที่แล้ว +1

    Question: why do you need to serialize or deserialize user data? stated at 8:47? Thanks

    • @samart3010
      @samart3010 2 ปีที่แล้ว +1

      I haven't watched this vid yet but serialize means you are making cookie and deserialize means extracting data from that session cookie. The result of the serializeUser method is attached to the session as req.session.passport.user = {}.

    • @IonizedComa
      @IonizedComa 3 หลายเดือนก่อน

      I know this is 2 years old but just leaving it for someone here who is also curious,
      the serialization defines how you want to attach the user data to the session, for instance if you wanted to filter the user data to like 3 attributes in you would define 'done' inside the serialization method as:
      ```
      passport.serializeUser((user, done) => {
      done(null, {
      id: user.id,
      email: user.email,
      cover: user.cover
      });
      });
      ```
      this reduces the size of your session data by excluding unecessary fields
      in the video for the sake of simplicity the guy just attached the entire payload
      your deserializeUser function would just assign the data in req.session.passport.user to req.user to make it easier for use

  • @sanketkothiya547
    @sanketkothiya547 ปีที่แล้ว

    totally worth it 🔥🔥

  • @thinhle9325
    @thinhle9325 ปีที่แล้ว

    nice tutorial !!.😁

  • @valentin.stamate
    @valentin.stamate 7 หลายเดือนก่อน

    Well done

  • @cookie2glue
    @cookie2glue ปีที่แล้ว

    10:21 The silence here killed me lol

  • @user-yn4qr7hu9r
    @user-yn4qr7hu9r 3 หลายเดือนก่อน

    thx man
    saved me

  • @auinspiration03
    @auinspiration03 2 ปีที่แล้ว +1

    you are grate

  • @AndrewTSq
    @AndrewTSq ปีที่แล้ว

    i cant get it to work with private ip, how did you do that?.. google just blocks my request.

    • @IonizedComa
      @IonizedComa 3 หลายเดือนก่อน

      make sure you assign test accounts to OAuth consent screen in google cloud platform, if your application is still in testing and not production it may only allow test account access

  • @bloggrammer
    @bloggrammer 11 หลายเดือนก่อน

    Great video.

  • @lukaakhalbedashvili7498
    @lukaakhalbedashvili7498 2 ปีที่แล้ว

    does this work in react ?

    • @tonienguix4834
      @tonienguix4834 2 ปีที่แล้ว

      react works well with express, yes

  • @naderhany3639
    @naderhany3639 2 หลายเดือนก่อน

    man you are awesome

  • @UtkarshSingh_gt7
    @UtkarshSingh_gt7 9 วันที่ผ่านมา

    Thanks man

  • @dragtr0n
    @dragtr0n ปีที่แล้ว

    Bro can you mentor me a little over discord? I am self taught. Just learnt mongoose node and express.

  • @DanOhCaptainDaniel
    @DanOhCaptainDaniel ปีที่แล้ว

    20:04, I am still unable to login..

    • @DanOhCaptainDaniel
      @DanOhCaptainDaniel ปีที่แล้ว

      got it, had to change the callbackURL to not include auth

  • @tomasonderco7324
    @tomasonderco7324 ปีที่แล้ว

    does anyone elses logout not work?

    • @docksfps
      @docksfps ปีที่แล้ว

      I solved the logout problem with this :
      app.get("/logout", (req, res) => {
      req.logout(req.user, err => {
      if(err) return next(err);
      res.redirect("/");
      });
      });

  • @DanOhCaptainDaniel
    @DanOhCaptainDaniel ปีที่แล้ว

    11:09 my nodemon keeps crashing for whatever reason. It doesn't like the passport.authenticate line

  • @andres201999
    @andres201999 2 ปีที่แล้ว

    gracias bb

    • @orenzbeda6072
      @orenzbeda6072 2 ปีที่แล้ว

      th-cam.com/video/5WehZTrgG8o/w-d-xo.html

  • @ImDino
    @ImDino 2 ปีที่แล้ว

    this video is strangely high fps

  • @thecontinugouios
    @thecontinugouios 2 ปีที่แล้ว

    ummmm, hello brother. Sorry i just have a small critic for you but its ok, its a good video. But you don't need to copy paste the code. You need to code it from scratch so i can understand step by step.

  • @agix666
    @agix666 11 หลายเดือนก่อน +1

    I just followed this and it was great but when it was time for the logout I had issues with just *req.logout();* and *req.session.destroy();*
    As of 7/23 this worked instead
    req.logout((err) => {
    if (err) { res.send(err); }
    else { req.session.destroy((err) => {
    if (err) { res.send(err); }
    else { res.send('Logging out...'); }
    });
    }
    });

    • @MunibDalati
      @MunibDalati 3 หลายเดือนก่อน +1

      Thanks a lot!

  • @docksfps
    @docksfps ปีที่แล้ว +1

    I solved the logout problem with this :
    app.get("/logout", (req, res) => {
    req.logout(req.user, err => {
    if(err) return next(err);
    res.redirect("/");
    });
    });