← Back to all tutorials

Quick Auth Test

Test the complete authentication flow — log in with Google, verify the session is created, and confirm req.user is available in routes.

Quick Auth Test

Let us test the complete authentication flow — from clicking "Login with Google" to having req.user available in your routes.

Testing the Flow

  1. Start the app: node app.js
  2. Go to http://localhost:3000
  3. Click "Login with Google"
  4. Log in with your Google account
  5. Grant permissions on the consent screen
  6. You should be redirected back to your app

Checking req.user

// In any route, after login:
router.get('/google/redirect', passport.authenticate('google'),
    function(req, res) {
        // req.user is now the logged-in user
        console.log('User is:', req.user);
        res.send('Logged in as: ' + req.user.username);
    }
);

Checking if User is Logged In

app.get('/', function(req, res) {
    console.log('User:', req.user); // undefined if not logged in
    res.render('home', { user: req.user });
});

Debugging Checklist

IssueSolution
redirect_uri_mismatchMake sure callback URL matches Google Console exactly
req.user is undefinedCheck middleware order: cookieSession → passport.initialize → passport.session
User not saved to DBCheck MongoDB connection string and mongoose.connect()
Duplicate usersVerify you are checking googleId before creating
Cookie not persistingCheck maxAge and cookieKey are set

Key Takeaways

  • After successful login, req.user contains the full user object from MongoDB
  • req.user is undefined for unauthenticated requests
  • Check the middleware order and callback URL if things are not working
  • The session persists across requests until the cookie expires