forked from sidpalas/storybooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brad Traversy
authored and
Brad Traversy
committed
Jun 19, 2020
1 parent
707962c
commit b5a2c68
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const GoogleStrategy = require('passport-google-oauth20').Strategy | ||
const mongoose = require('mongoose') | ||
const User = require('../models/User') | ||
|
||
module.exports = function (passport) { | ||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: '/auth/google/callback', | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
const newUser = { | ||
googleId: profile.id, | ||
displayName: profile.displayName, | ||
firstName: profile.name.givenName, | ||
lastName: profile.name.familyName, | ||
image: profile.photos[0].value, | ||
} | ||
|
||
try { | ||
let user = await User.findOne({ googleId: profile.id }) | ||
|
||
if (user) { | ||
done(null, user) | ||
} else { | ||
user = await User.create(newUser) | ||
done(null, user) | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
) | ||
) | ||
|
||
passport.serializeUser((user, done) => { | ||
done(null, user.id) | ||
}) | ||
|
||
passport.deserializeUser((id, done) => { | ||
User.findById(id, (err, user) => done(err, user)) | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
googleId: { | ||
type: String, | ||
required: true, | ||
}, | ||
displayName: { | ||
type: String, | ||
required: true, | ||
}, | ||
firstName: { | ||
type: String, | ||
required: true, | ||
}, | ||
lastName: { | ||
type: String, | ||
required: true, | ||
}, | ||
image: { | ||
type: String, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}) | ||
|
||
module.exports = mongoose.model('User', UserSchema) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const express = require('express') | ||
const passport = require('passport') | ||
const router = express.Router() | ||
|
||
// @desc Auth with Google | ||
// @route GET /auth/google | ||
router.get('/google', passport.authenticate('google', { scope: ['profile'] })) | ||
|
||
// @desc Google auth callback | ||
// @route GET /auth/google/callback | ||
router.get( | ||
'/google/callback', | ||
passport.authenticate('google', { failureRedirect: '/' }), | ||
(req, res) => { | ||
res.redirect('/dashboard') | ||
} | ||
) | ||
|
||
// @desc Logout user | ||
// @route /auth/logout | ||
router.get('/logout', (req, res) => { | ||
req.logout() | ||
res.redirect('/') | ||
}) | ||
|
||
module.exports = router |