Social Login | Google | Passport | Express JS
Go to the Google Cloud Console (https://console.developers.google.com/) and set up Google OAuth credentials.
Social Login | Google | Passport | Express JS
[embed]Passport.js Simple, unobtrusive authentication for Node.jswww.passportjs.org



Go to the Google Cloud Console (https://console.developers.google.com/) and set up Google OAuth credentials.
- Create a project.
- Enable Google+ API or Google Identity Services.
- Create OAuth 2.0 credentials.
- Set Authorized Redirect URI to: http://localhost:3000/auth/google/callback
Note your Client ID and Client Secret.
Create .env File
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
SESSION_SECRET=some_secret_key
Express Server with Google Login
// server.js
require('dotenv').config();
const express = require('express');
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const app = express();
// Google OAuth Strategy
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/auth/google/callback"
}, (accessToken, refreshToken, profile, done) => {
// Save or find user in DB here
return done(null, profile);
}));
// Routes
app.get('/', (req, res) => res.send('Home Page'));
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
(req, res) => {
// Successful authentication
res.send(`Hello, ${req.user.displayName}`);
}
);
app.get('/logout', (req, res) => {
req.logout(() => res.redirect('/'));
});
app.listen(3000, () => console.log('Server started on http://localhost:3000'));
Test the Flow
- Start your server: node server.js
- Visit http://localhost:3000/auth/google.
- Log in with your Google account
- You’ll be redirected back with your profile data
메타데이터
- post_id
- 70de5029c3bd
- slug
- social-login-google-passport-express-js-70de5029c3bd
- url
- https://medium.com/@rk85783/social-login-google-passport-express-js-70de5029c3bd
- canonical_url
- https://medium.com/@rk85783/social-login-google-passport-express-js-70de5029c3bd
- author_url
- https://medium.com/@rk85783
- status
- ok
- fetched_at
- 2026-06-25 16:53:31