Setting up Passport - Google - oAuth

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 370:- Setting up Passport - Google - oAuth

To set up passport-google-oauth20 in your application, follow these steps:

  1. Install passport and passport-google-oauth20 packages:

Copy code

npm install passport passport-google-oauth20

  1. Create a Google Cloud Platform project in the Google API Console.
  2. Add credentials to the project:
  • From the APIs & Services dashboard, select Credentials.
  • On the Credentials page, click Create credentials and select OAuth client ID.
  • Under Application type, select Web application.
  • Add a name for the credential, then add authorized JavaScript origins and authorized redirect URIs.
  • Click Create.
  1. Set up the passport strategy in your application:

javascriptCopy code

const passport = require('passport'); const GoogleStrategy = require('passport-google-oauth20').Strategy; passport.use(new GoogleStrategy({    clientID: process.env.GOOGLE_CLIENT_ID,    clientSecret: process.env.GOOGLE_CLIENT_SECRET,    callbackURL: '/auth/google/callback'  },  function(accessToken, refreshToken, profile, cb) {    // User authentication and/or registration code here    return cb(null, profile);  } ));

  1. Set up the routes in your application:

javascriptCopy code

const express = require('express'); const passport = require('passport'); const router = express.Router(); // Google authentication route router.get('/auth/google',  passport.authenticate('google', { scope: ['profile'] })); // Google authentication callback route router.get('/auth/google/callback',  passport.authenticate('google', { failureRedirect: '/login' }),  function(req, res) {    // Successful authentication, redirect to the home page    res.redirect('/');  });

  1. Use the passport.authenticate() middleware to authenticate requests to your protected routes:

javascriptCopy code

const express = require('express'); const router = express.Router(); // Protected route router.get('/protected',  passport.authenticate('google', { session: false }),  function(req, res) {    // The user is authenticated, respond with a success message    res.send('You are authenticated!');  });

Note: In step 4, the callback function receives an accessToken, a refreshToken, a profile, and a cb (callback) function. You will need to implement the user authentication and/or registration code in this function.

43. Social Authentication (Mini Lecture)

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support