NodeJS & Express - Google OAuth2 using PassportJS

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.พ. 2025
  • 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.....
    Check out the code on Github: github.com/kri...
    Don't forget to Subscribe here: / @krisfoster1

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

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

    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

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

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

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

      Thank you for the kind words!

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

      @@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 2 ปีที่แล้ว

      Yeah I agree

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

      Absolutely, straight to the point and no waste of time

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

    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  3 ปีที่แล้ว +2

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

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

      Damn 🙄

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

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

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

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

  • @ilanle
    @ilanle 2 ปีที่แล้ว +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

  • @crazedbrazy617bh
    @crazedbrazy617bh 3 ปีที่แล้ว +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!

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

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

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

    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.

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

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

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

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

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

    Concise, precise, and the code is wise! Thank you for the tutorial.

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

    I'm kind of late to watch this. But this was super helpful. Thank you so much!

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

    Simplest Code and Simplest explanation. awesome

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

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

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

    You are one of the best Tutor.

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

    Thanks for this simple but clear video.

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

    Great video. Short and precise . Thanks alot

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

    10:21 The silence here killed me lol

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

    You're the best explain for this mate.

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

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

  • @TOMGAMING-hy9hi
    @TOMGAMING-hy9hi 3 ปีที่แล้ว +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

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

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

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

    Thank you, this helped me a lot

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

    Very helpful video it is short and suit ❤❤🎉

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

    the best video I’ve ever seen👍

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

    I used your code, but did not get refresh token, I tried option access_type: 'offline', but still it is not giving refresh token. Could you please tell me how can I get the refresh token. It would be helpfull if you could make a video on it. The solution is not present on internet.

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

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

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

    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?

  • @טלעמית-מ9כ
    @טלעמית-מ9כ 3 ปีที่แล้ว +1

    Great video, made it so easy. Tnx

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

      Thank you for the kind feedback & glad you enjoyed!

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

    the best work bro, thanks a lot...

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

    Good video! Thanks for sharing!

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

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

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

    Thanks Kris for this video

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

    Any tips on how to do this using the architecture Route/Controller/Service

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

    totally worth it 🔥🔥

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

    Thank you so clear explaining

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

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

  • @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??

  • @kirillpavlovskii8342
    @kirillpavlovskii8342 3 ปีที่แล้ว +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

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

    Excellent tutorial 👍🏽

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

    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 ปีที่แล้ว

      And how does express-session solve this?

    • @artax5005
      @artax5005 ปีที่แล้ว +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 ปีที่แล้ว +1

      @@artax5005 Wow this helps, thank you so much

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

    great tutorial thank you!

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

    best tutorial 2021

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

    Great tutorial video😀😀

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

    Thanks for the tutorial

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

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

    • @samart3010
      @samart3010 3 ปีที่แล้ว +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 11 หลายเดือนก่อน

      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

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

    I love you for this.

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

      Thank you, glad you enjoyed!

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

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

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

    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 2 ปีที่แล้ว

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

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

      Yes req.logout() needs a callback

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

    Thanks kris for this wonderful video and awesome explanation .
    I am watching this video in 2k24 but I am getting one problem after selecting my email account for sign in I am getting "This site can not provide a secure connection". Can anyone help me pls ..

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

    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?

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

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

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

    Thanks for your sharing. great tutorial.

  • @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!

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

    SO GOOD ❤

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

    nice tutorial !!.😁

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

    Great video.

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

    Thank you so much!!!

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

    Hey kris thanks a lot man

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

    So no DB to save the session?

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

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

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

    Cannot GET /auth/google/callback getting this error

  • @agix666
    @agix666 ปีที่แล้ว +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 11 หลายเดือนก่อน +1

      Thanks a lot!

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

    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!

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

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

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

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

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

      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

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

    2017 style express, niiiiice

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

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

  • @Dihan-fh5yx
    @Dihan-fh5yx ปีที่แล้ว

    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 10 หลายเดือนก่อน

      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

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

    thx man
    saved me

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

    Hi Kris. How can we send to mongodb? Thanks

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

    man you are awesome

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

    Bro!! Thank you..

  • @juanisidorogarcia1699
    @juanisidorogarcia1699 3 ปีที่แล้ว +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!

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

    When I click my auth link it just spins.

  • @valentin.stamate
    @valentin.stamate ปีที่แล้ว

    Well done

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

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

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

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

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

    you are grate

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

    Thanks man

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

    But show req.user=undefined

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

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

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

    does this work in react ?

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

      react works well with express, yes

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

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

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

    👌

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

    thankyou

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

    does anyone elses logout not work?

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

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

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

    this video is strangely high fps

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

    gracias bb

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

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

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

    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.

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

    Bro u need to stop doing that sound with ur mouth!!!!!!!!!!!

  • @docksfps
    @docksfps 2 ปีที่แล้ว +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("/");
    });
    });