Skip to content

Instantly share code, notes, and snippets.

@ferreiro
Last active November 9, 2016 19:43
Show Gist options
  • Save ferreiro/7b8ead4ab4d051ae825eb726be68f7ea to your computer and use it in GitHub Desktop.
Save ferreiro/7b8ead4ab4d051ae825eb726be68f7ea to your computer and use it in GitHub Desktop.
Configuration
'use strict'
const passport = require('passport')
const configuration = require('../config')
const tokens = configuration.tokens
// User models
const UserDB = require('../api/v1/database/user')
// Passport strategies modules
const GoogleStrategy = require('passport-google-oauth2').Strategy
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Google profile is
// serialized and deserialized.
passport.serializeUser(function (user, done) {
done(null, user)
})
passport.deserializeUser(function (obj, done) {
done(null, obj)
})
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
// --- GOOGLE STRATEGY -------------------------
// ---------------------------------------------
const GoogleOptions = {
clientID: String(tokens.google.CLIENT_ID),
clientSecret: String(tokens.google.CLIENT_SECRET),
callbackURL: String(tokens.google.CALLBACK_URL),
passReqToCallback: true
}
const GoogleCallback = function (request, accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Google profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Google account with a user record in your database,
// and return that user instead.
// http://stackoverflow.com/questions/13272824/combine-two-or-queries-with-and-in-mongoose
const query = {
$or: [
{ 'email': email },
{ 'services.google.googleID': googleID }
]
}
UserDB.getUser(query, (err, user) => {
if (err) return done(err)
let newUser = {}
newUser.username = googleID
newUser.email = email
newUser.name = {}
newUser.name.first = name.first
newUser.name.last = name.last
newUser.services = {}
newUser.services.google = {}
newUser.services.google.id = googleID
newUser.services.google.token = accessToken
newUser.services.google.image = image
if (user) {
// We have found the user
// The user was already on our database.
// Update the data
const query = {
'username': user.username
}
UserDB.updateUser(newUser, query, (err, user) => {
if (err) return done(err)
return done(null, newUser)
})
} else {
// User not found in our database.
// Creating a new user in our database for first time
UserDB.createUser(newUser, (err, user) => {
if (err) return done(err)
return done(null, newUser)
})
}
})
})
}
try {
passport.use(new GoogleStrategy(GoogleOptions, GoogleCallback))
} catch (e) {
console.log('----> Problems creating a google strategy')
console.log('----> Possible: clientID not defined')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment