Go to Google Cloud Console → APIs & Services → Credentials → Create OAuth 2.0 Client ID. Set the authorized redirect URI to /api/auth/google/callback.
npm install passport passport-google-oauth20
const passport = require('passport');
const GoogleStrategy =
require('passport-google-oauth20').Strategy;
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret:
process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/api/auth/google/callback',
}, async (accessToken, refreshToken,
profile, done) => {
try {
let user = await User.findOne({
googleId: profile.id
});
if (!user) {
// Check if email exists (link accounts)
user = await User.findOne({
email: profile.emails[0].value
});
if (user) {
user.googleId = profile.id;
await user.save();
} else {
user = await User.create({
name: profile.displayName,
email: profile.emails[0].value,
googleId: profile.id,
avatar: profile.photos[0]?.value,
});
}
}
done(null, user);
} catch (error) {
done(error);
}
}));
app.get('/api/auth/google',
passport.authenticate('google', {
scope: ['profile', 'email']
})
);
app.get('/api/auth/google/callback',
passport.authenticate('google',
{ session: false }),
(req, res) => {
const token = jwt.sign(
{ userId: req.user._id },
process.env.JWT_SECRET,
{ expiresIn: '7d' }
);
res.redirect(
`${process.env.FRONTEND_URL}` +
`/auth/callback?token=${token}`
);
}
);
"Login with Google" button that redirects to the OAuth flow. Handle the callback: extract token from URL, store it, redirect to gallery.
git switch -c feature/PIXELCRAFT-051-oauth
git add server/ src/
git commit -m "Add Google OAuth with Passport.js (PIXELCRAFT-051)"
git push origin feature/PIXELCRAFT-051-oauth
# PR → Review → Merge → Close ticket ✅
OAuth separates authentication from your application.
The "authorization code" flow has multiple redirects for security.