Episode 17 of 21
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
- Start the app:
node app.js - Go to
http://localhost:3000 - Click "Login with Google"
- Log in with your Google account
- Grant permissions on the consent screen
- 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
| Issue | Solution |
|---|---|
| redirect_uri_mismatch | Make sure callback URL matches Google Console exactly |
| req.user is undefined | Check middleware order: cookieSession → passport.initialize → passport.session |
| User not saved to DB | Check MongoDB connection string and mongoose.connect() |
| Duplicate users | Verify you are checking googleId before creating |
| Cookie not persisting | Check maxAge and cookieKey are set |
Key Takeaways
- After successful login,
req.usercontains the full user object from MongoDB req.userisundefinedfor unauthenticated requests- Check the middleware order and callback URL if things are not working
- The session persists across requests until the cookie expires