2.6.4.1 OAuth2 Flow

1. Frontend: User selects “Sign in with Google.”

2. Redirect to Google’s OAuth2 endpoint with your client ID and scopes.

3. Callback: Google redirects back to your specified endpoint with a code.

4. Backend: Exchanges code for tokens, then retrieves user info (email, name).

5. Account Linking: If the user’s email already exists, log them in; otherwise, create a new user record.

Example (Node.js + passport-google-oauth20 or custom logic):

// backend/src/routes/authRoutes.js
const router = require('express').Router();
const passport = require('passport');

// Google login route
router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] }));

router.get('/google/callback', passport.authenticate('google', { session: false }),
  (req, res) => {
    // Successful authentication
    // Generate JWT and redirect or respond with token
  }
);

module.exports = router;

Last updated