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)

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?