Skip to content

Commit

Permalink
Updated model and passport strategy structures
Browse files Browse the repository at this point in the history
  • Loading branch information
tostartpressanykey committed Feb 27, 2015
1 parent 8c6d0c0 commit cc3eece
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 35 deletions.
8 changes: 7 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

// The main email in the schema is
// what we'll be searching to update the others.

// TODO - If Facebook or Google email is different it would currently be different users.
// - Need to lookup user.email to see if any one of googles or facebooks email matches

var UserSchema = new Schema({
email: { type: String, index: { unique: true} },

local: {
email : { type: String },
Expand All @@ -25,7 +31,7 @@ var UserSchema = new Schema({
},

registered: { type: Date, default: Date.now }

});


Expand Down
99 changes: 65 additions & 34 deletions passport/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,45 @@ module.exports = function(passport){
return done(err);

if (user) {

// if user.google.id === undefined save google data to document
if(user.google.id === undefined) {
saveGoogleData(profile, user);
}

console.log('hitting another return');
// if a user is found, log them in
return done(null, user);
} else {
// if the user isnt in our database, create a new user

// if the user.email isn't in our database, create a new user
var newUser = new User();

// set all of the relevant information
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.google.name = profile.name.givenName + ' ' + profile.name.familyName;
newUser.google.email = profile.emails[0].value; // pull the first email

console.log(newUser);
// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
// setting default email as primary index for document.
newUser.email = profile.emails[0].value;

// save the rest of google data and save the new user
saveGoogleData(profile, newUser);
}
});
});

var saveGoogleData = function(profile, user) {
// set all of the relevant information
user.google.id = profile.id;
user.google.token = token;
user.google.name = profile.name.givenName + ' ' + profile.name.familyName;
user.google.email = profile.emails[0].value; // pull the first email

user.save(function(err) {
if (err)
throw err;

console.log('saving google data');
return done(null, user);
});
}

}));


Expand All @@ -110,8 +125,8 @@ module.exports = function(passport){

// asynchronous
process.nextTick(function() {
console.log(profile);
// find the user in the database based on their facebook id

// try to find the user based on their email
User.findOne({ 'email' : profile.emails[0].value }, function(err, user) {

// if there is an error, stop everything and return that
Expand All @@ -121,30 +136,46 @@ module.exports = function(passport){

// if the user is found, then log them in
if (user) {
return done(null, user); // user found, return that user

// if user.facebook.id === undefined save facebook data to document
if(user.facebook.id === undefined) {
saveFacebookData(profile, user);
}

// user found, return that user
return done(null, user);

} else {
// if there is no user found with that facebook id, create them
var newUser = new User();

// set all of the facebook information in our user model
newUser.facebook.id = profile.id; // set the users facebook id
newUser.facebook.token = token; // we will save the token that facebook provides to the user
newUser.firstName = profile.name.givenName
newUser.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

// save our user to the database
newUser.save(function(err) {
if (err)
throw err;

// if successful, return the new user
return done(null, newUser);
});

// if the user.email isn't in our database, create a new user
var newUser = new User();

// setting default email as primary index for document.
newUser.email = profile.emails[0].value;

// save the rest of fb data and save the new user
saveFacebookData(profile, newUser);

}

});
});


var saveFacebookData = function(profile, user) {
user.facebook.id = profile.id; // set the users facebook id
user.facebook.token = token; // we will save the token that facebook provides
user.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
user.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

user.save(function(err) {
if (err)
throw err;

// if successful, return the new user
return done(null, user);
});
}
}));

}

0 comments on commit cc3eece

Please sign in to comment.