passport.serializeUser(function(user, done) {
debugLog("serializing user:", {user: user});
return done(null, user);
});
passport.deserializeUser(function(obj, done) {
debugLog("serializing user:", {obj: obj});
User.findOne(
//search criteria
{ 'id': obj.id },
// Callback function if found.
function(err, user) {
if( err ) {
// Return a logged out user if not found.
debugLog(err, {message: "user not found during deserialize"});
return done(null, false);
}
else {
// call done and pass it with the User Model
// Pass it an instance of our User model if found.
debugLog("Passport deserialized user", {user:user});
return done(null, user);
}
}
);
});
//callback url
var callbackUrl = "http://localhost:1337/auth/google/callback"
if(process.env.HOST_DOMAIN)
callbackUrl = 'https://' + process.env.HOST_DOMAIN + '/auth/google/callback';
passport.use(
new GoogleStrategy(
// Google Settings.
{
clientID: googleApiConfig.googleAppConfig.clientID,
clientSecret: googleApiConfig.googleAppConfig.clientSecret,
callbackURL: callbackUrl
},
function(accessToken, refreshToken, profile, done) {
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.
debugLog("Found profile", {profile: profile});
User.findOrCreate(
//Search criteria
{ googleId: profile.id },
//Insertion criteria when search fails.
{
googleId: profile.id,
uuid: uuid.v1()
},
// Call back function.
function (err, user) {
//update refresh token for the user'
user.googleAccessToken = accessToken;
// Only update refresh token if one is provided.
if(refreshToken)
user.googleRefreshToken = refreshToken;
user.save(function(err){
errLog(err)
});
return done(err, user);
}
); //end User.findOrCreate()
}); // end of nextTick()
}//end callback function
) // end GoogleStrategy()
);
module.exports = {
http: {
customMiddleware: function(app){
debugLog('initializing express middleware for passport');
app.use(passport.initialize());
app.use(passport.session());
}
}
};