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)

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?