Last active
February 11, 2025 19:23
-
-
Save SinaMN75/dcba4850f8af6a268a7ca5166e860b1e to your computer and use it in GitHub Desktop.
Google Sign up/in in NodeJS and MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /routes/passport.js | |
const passport = require('passport'); | |
module.exports = (app) => { | |
/** | |
* SignIn. | |
*/ | |
app.get('/auth/google', passport.authenticate('google', { | |
scope: ['profile', 'email'] | |
})); | |
/** | |
* Callback from Google. | |
*/ | |
app.get('/auth/google/callback', passport.authenticate('google')); | |
/** | |
* Logout. | |
*/ | |
app.get('/api/logout', (req, res) => { | |
req.logout(); | |
res.send(req.user); | |
}); | |
/** | |
* Get current user. | |
*/ | |
app.get('/api/current_user', (req, res) => { | |
res.send(req.user); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const keys = require('./configs/keys'); | |
const mongoose = require('mongoose'); | |
const cookieSession = require('cookie-session'); | |
const passport = require('passport'); | |
require('./models/user'); | |
require('./service/passport'); | |
mongoose.connect(keys.mongoURI); | |
const app = express(); | |
app.use(cookieSession({ | |
maxAge: 30 * 24 * 60 * 60 * 1000, | |
keys: [keys.cookieKey], | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
require('./routes/authRoutes')(app); | |
app.listen(process.env.PORT || 3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "express", | |
"version": "0.0.0", | |
"private": true, | |
"main": "app.js", | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"engines": { | |
"node": "10.15.3", | |
"npm": "6.4.1" | |
}, | |
"dependencies": { | |
"cookie-parser": "~1.4.3", | |
"cookie-session": "^1.3.3", | |
"debug": "~2.6.9", | |
"express": "~4.16.0", | |
"http-errors": "~1.6.2", | |
"mongoose": "^5.5.3", | |
"morgan": "~1.9.0", | |
"passport": "^0.4.0", | |
"passport-google-oauth20": "^2.0.0", | |
"pug": "^2.0.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /services/passport.js | |
const passport = require('passport'); | |
const mongoose = require('mongoose'); | |
const GoogleStrategy = require('passport-google-oauth20').Strategy; | |
const keys = require('../configs/keys'); | |
const User = mongoose.model('user'); | |
passport.serializeUser((user, done) => { | |
done(null, user.id); | |
}); | |
passport.deserializeUser((id, done) => { | |
User.findById(id).then(user => { | |
done(null, user); | |
}) | |
}); | |
passport.use(new GoogleStrategy({ | |
clientID: keys.googleClientID, | |
clientSecret: keys.googleClientSecret, | |
callbackURL: '/auth/google/callback' | |
}, (accessToken, refreshToken, profile, done) => { | |
User.findOne({googleId: profile.id}).then(existingUser => { | |
if (existingUser) { | |
done(null, existingUser); | |
} else { | |
new User({googleId: profile.id}).save().then(user => done(null, user)); | |
} | |
}); | |
})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /models/user.js | |
const mongoose = require('mongoose'); | |
const {Schema} = mongoose; | |
const userSchema = new Schema({ | |
googleId: String, | |
}); | |
mongoose.model('user', userSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment