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
  • Наука та технологія

КОМЕНТАРІ • 121

  • @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 3 роки тому +43

    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: ua-cam.com/video/5WehZTrgG8o/v-deo.html

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

      Yeah I agree

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

      Absolutely, straight to the point and no waste of time

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

    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 2 місяці тому +1

      Damn 🙄

  • @GlitchedFailure
    @GlitchedFailure 5 місяців тому +2

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

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

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

  • @abdulsamihamedi5584
    @abdulsamihamedi5584 6 місяців тому +1

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

  • @immensives147
    @immensives147 2 місяці тому +1

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

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

    Simplest Code and Simplest explanation. awesome

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

    Great video. Short and precise . Thanks alot

  • @abhishekalimchandani4351
    @abhishekalimchandani4351 10 днів тому

    Best tutorial for Google OAuth2 using PassportJS

  • @haoyuandong
    @haoyuandong 3 роки тому +5

    Thanks for this simple but clear video.

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

    You're the best explain for this mate.

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

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

  • @generalknow_ledge
    @generalknow_ledge 8 місяців тому

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

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

  • @willysnowman
    @willysnowman 2 роки тому +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 4 місяці тому

      Yes req.logout() needs a callback

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

    Thank you, this helped me a lot

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

    Thanks Kris for this video

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

    Thank you so clear explaining

  • @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: ua-cam.com/video/5WehZTrgG8o/v-deo.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!

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

    totally worth it 🔥🔥

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

    the best work bro, thanks a lot...

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

    the best video I’ve ever seen👍

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

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

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

    Thanks for your sharing. great tutorial.

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

    best tutorial 2021

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

    Very helpful video it is short and suit ❤❤🎉

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

    Excellent tutorial 👍🏽

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

  • @paritoshpanigrahi007
    @paritoshpanigrahi007 21 день тому +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.

  • @w.cahyanto7924
    @w.cahyanto7924 2 роки тому

    Great tutorial video😀😀

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

    Thank you so much!!!

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

    Good video! Thanks for sharing!

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

      Wil you try this: ua-cam.com/video/5WehZTrgG8o/v-deo.html

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

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

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

  • @boot-strapper
    @boot-strapper 2 роки тому

    2017 style express, niiiiice

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

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

  • @user-ky1ug2ex5x
    @user-ky1ug2ex5x Рік тому

    Thanks for the tutorial

  • @andrews13
    @andrews13 4 місяці тому

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

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

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

  • @omarruder4129
    @omarruder4129 8 місяців тому

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

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

    I love you for this.

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

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

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

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

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

    man you are awesome

  • @user-yn4qr7hu9r
    @user-yn4qr7hu9r 4 місяці тому

    thx man
    saved me

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

    So no DB to save the session?

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

    Great video.

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

    10:21 The silence here killed me lol

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

    When I click my auth link it just spins.

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

    Hey kris thanks a lot man

  • @valentin.stamate
    @valentin.stamate 8 місяців тому

    Well done

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

    nice tutorial !!.😁

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

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

    Cannot GET /auth/google/callback getting this error

  • @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: ua-cam.com/video/5WehZTrgG8o/v-deo.html

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

    Thanks man

  • @ameybelvalkar8877
    @ameybelvalkar8877 8 днів тому

    Awesome

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

    you are grate

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

    Hi Kris. How can we send to mongodb? Thanks

  • @abhinavbabu1237
    @abhinavbabu1237 18 днів тому

    👌

  • @ABUTAHER-wg7gz
    @ABUTAHER-wg7gz 2 роки тому

    But show req.user=undefined

  • @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 5 місяців тому

      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

  • @Dihan-fh5yx
    @Dihan-fh5yx 7 місяців тому

    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 4 місяці тому

      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

  • @kodeeater
    @kodeeater 7 днів тому

    thankyou

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

    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 9 місяців тому

      And how does express-session solve this?

    • @artax5005
      @artax5005 9 місяців тому +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) ua-cam.com/video/F-sFp_AvHc8/v-deo.html.

    • @W_Wadu
      @W_Wadu 9 місяців тому +1

      @@artax5005 Wow this helps, thank you so much

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

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

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

      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

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

    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 ?

  • @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 2 роки тому

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

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

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

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

    gracias bb

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

      ua-cam.com/video/5WehZTrgG8o/v-deo.html

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

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

  • @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 Рік тому +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...'); }
    });
    }
    });

  • @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("/");
    });
    });