Using Google Auth - With a Link

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 371:- Using Google Auth - With a Link

To use Google Auth with a link, you need to create a link that redirects the user to the Google Auth URL with the required parameters.

Here's an example of how you can create a link in HTML:

phpCopy code

<a href="/auth/google">Sign in with Google</a>

Here, the href attribute points to the /auth/google endpoint on the server, which handles the authentication with Google.

On the server side, you can use the Passport.js middleware to handle the authentication flow. Here's an example of how you can set up the Google Auth strategy with Passport.js:

javascriptCopy code

const passport = require('passport'); const GoogleStrategy = require('passport-google-oauth20').Strategy; passport.use(new GoogleStrategy({    clientID: GOOGLE_CLIENT_ID,    clientSecret: GOOGLE_CLIENT_SECRET,    callbackURL: "/auth/google/callback"  },  function(accessToken, refreshToken, profile, cb) {    // Here, you can save the user's profile to the database or perform any other action    return cb(null, profile);  } ));

Here, GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are the client ID and client secret obtained from the Google Developer Console.

The callbackURL specifies the URL that Google will redirect the user to after they have authenticated. This URL should correspond to the route that handles the callback from Google.

To initiate the authentication flow, you can define a route on the server that redirects the user to the Google Auth URL. Here's an example of how you can define such a route:

javascriptCopy code

app.get('/auth/google',  passport.authenticate('google', { scope: ['profile'] })); app.get('/auth/google/callback',  passport.authenticate('google', { failureRedirect: '/login' }),  function(req, res) {    // Successful authentication, redirect home.    res.redirect('/');  });

Here, the /auth/google route initiates the authentication flow by redirecting the user to the Google Auth URL. The scope parameter specifies the permissions that your application is requesting from the user.

After the user has authenticated, Google will redirect them to the callback URL specified in the strategy configuration. The /auth/google/callback route handles this callback and authenticates the user using Passport.js. If the authentication is successful, the user is redirected to the home page. If the authentication fails, the user is redirected to the login page.

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