Define Google authentication routes
masterYou need to implement two routes to handle the OAuth 2.0 flow:
- The Login Route: Redirects the user to Google to begin the authentication process.
- The Callback Route: Processes the response from Google after the user authenticates and redirects them back to your application.
When defining the callback route, you can pass options to passport.authenticate such as failureRedirect and failureMessage to handle unsuccessful login attempts.
// 1. The route that initiates the login
app.get('/login/google', passport.authenticate('google'));
// 2. The callback route that Google redirects to
app.get('/oauth2/redirect/google',
passport.authenticate('google', { failureRedirect: '/login', failureMessage: true }),
function(req, res) {
// Successful authentication, redirect home
res.redirect('/');
});