Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Add joining of multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kentaro Wakayama committed Apr 9, 2014
1 parent f150407 commit 00f3940
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 118 deletions.
94 changes: 94 additions & 0 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,98 @@ exports.hasAuthorization = function(req, res, next) {
}

next();
};

/**
* Helper function to save or update a user.
* When the user is logged in, it joins the user data to the existing one.
* Otherwise it creates a new user.
*
* @author Kentaro Wakayama
*
* @date 2014-04-09
*
* @param {Object} req This is the request object which contains the user when he is signed in.
* @param {String} token This is the accesstoken.
* @param {String} tokenSecret This is the refreshtoken.
* @param {Object} profile This is the user profile of the current provider.
* @param {Function} done Callback to supply Passport with the user that authenticated.
*
* @param {Object} providerData This Object contains all data which is specific for the provider
* @param {String} providerData.provider This is the passport provider name.
* @param {String} providerData.idKey This is the Key / Attribute name for saving / retrieving the provider id.
* @param {String} providerData.name This is the user's name.
* @param {String} [providerData.email] This is the user's email.
* @param {String} providerData.username This is the user's username.
*
* @return {[type]} [description]
*/
exports.saveOrUpdate = function(req, token, tokenSecret, profile, done, providerData) {
var provider = providerData.provider;
var idKey = providerData.idKey;
var searchProviderKey = provider + '.' + idKey;
var searchObject = {};
searchObject[searchProviderKey] = profile.id;

if (!req.user) {
// no user active, this is a fresh login
User.findOne(searchObject, function(err, user) {
if (err) {
return done(err);
}
if (!user) {

var possibleUsername = '';
if (providerData.email) {
possibleUsername = providerData.email.split('@')[0];
} else {
possibleUsername = profile.username;
}

User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
user = new User({
firstName: providerData.firstName,
lastName: providerData.lastName,
username: availableUsername,
displayName: providerData.displayName,
email: providerData.email,
provider: provider,
});

user[provider] = profile._json;
user[provider].token = token;
user[provider].tokenSecret = tokenSecret;
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
});
} else {
user[provider].token = token;
user[provider].tokenSecret = tokenSecret;
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
}
});
} else {
// a user is already logged in, join the provider data to the existing user.
User.findById( req.user._id, function(err, user) {
if (err) {
return done(err);
}
if (user) {
user[provider] = profile._json;
user[provider].token = token;
user[provider].tokenSecret = tokenSecret;
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
}
};
7 changes: 6 additions & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ var UserSchema = new Schema({
created: {
type: Date,
default: Date.now
}
},
facebook: {},
twitter: {},
github: {},
google: {},
linkedin: {}
});

/**
Expand Down
45 changes: 14 additions & 31 deletions config/strategies/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy,
User = require('mongoose').model('User'),
config = require('../config');
config = require('../config'),
users = require('../../app/controllers/users');

module.exports = function() {
// Use facebook strategy
Expand All @@ -14,36 +15,18 @@ module.exports = function() {
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
return done(new Error('User is already signed in'), req.user);
} else {
User.findOne({
'provider': 'facebook',
'providerData.id': profile.id
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
User.findUniqueUsername(profile.username, null, function(availableUsername) {
user = new User({
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
email: profile.emails[0].value,
username: availableUsername,
provider: 'facebook',
providerData: profile._json
});
user.save(function(err) {
return done(err, user);
});
});
} else {
return done(err, user);
}
});
}

var providerData = {
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
provider: 'facebook',
idKey: 'id',
email: profile.emails[0].value,
username: profile.username,
};
users.saveOrUpdate(req, accessToken, refreshToken, profile, done, providerData);

}
));
};
42 changes: 13 additions & 29 deletions config/strategies/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
User = require('mongoose').model('User'),
config = require('../config');
config = require('../config'),
users = require('../../app/controllers/users');

module.exports = function() {
// Use google strategy
Expand All @@ -14,35 +15,18 @@ module.exports = function() {
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
return done(new Error('User is already signed in'), req.user);
} else {
User.findOne({
'provider': 'google',
'providerData.id': profile.id
}, function(err, user) {
if (!user) {
var possibleUsername = profile.emails[0].value.split('@')[0];

User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
user = new User({
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
email: profile.emails[0].value,
username: availableUsername,
provider: 'google',
providerData: profile._json
});
user.save(function(err) {
return done(err, user);
});
});
} else {
return done(err, user);
}
});
}
var providerData = {
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
provider: 'google',
idKey: 'id',
email: profile.emails[0].value,
username: profile.username
};
users.saveOrUpdate(req, accessToken, refreshToken, profile, done, providerData);

}
));
};
41 changes: 12 additions & 29 deletions config/strategies/linkedin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var passport = require('passport'),
LinkedInStrategy = require('passport-linkedin').Strategy,
User = require('mongoose').model('User'),
config = require('../config');
config = require('../config'),
users = require('../../app/controllers/users');

module.exports = function() {
// Use linkedin strategy
Expand All @@ -15,35 +16,17 @@ module.exports = function() {
profileFields: ['id', 'first-name', 'last-name', 'email-address']
},
function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
return done(new Error('User is already signed in'), req.user);
} else {
User.findOne({
'provider': 'linkedin',
'providerData.id': profile.id
}, function(err, user) {
if (!user) {
var possibleUsername = profile.emails[0].value.split('@')[0];

User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
user = new User({
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
email: profile.emails[0].value,
username: availableUsername,
provider: 'linkedin',
providerData: profile._json
});
user.save(function(err) {
return done(err, user);
});
});
} else {
return done(err, user);
}
});
}
var providerData = {
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
provider: 'linkedin',
idKey: 'id',
username: profile.displayName,
};
users.saveOrUpdate(req, accessToken, refreshToken, profile, done, providerData);

}
));
};
39 changes: 11 additions & 28 deletions config/strategies/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy,
User = require('mongoose').model('User'),
config = require('../config');
config = require('../config'),
users = require('../../app/controllers/users');

module.exports = function() {
// Use twitter strategy
Expand All @@ -14,33 +15,15 @@ module.exports = function() {
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done) {
if (req.user) {
return done(new Error('User is already signed in'), req.user);
} else {
User.findOne({
'provider': 'twitter',
'providerData.id_str': profile.id
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
User.findUniqueUsername(profile.username, null, function(availableUsername) {
user = new User({
displayName: profile.displayName,
username: availableUsername,
provider: 'twitter',
providerData: profile._json
});
user.save(function(err) {
return done(err, user);
});
});
} else {
return done(err, user);
}
});
}

var providerData = {
displayName: profile.displayName,
provider: 'twitter',
idKey: 'id_str',
username: profile.username,
};

users.saveOrUpdate(req, token, tokenSecret, profile, done, providerData);
}
));
};

0 comments on commit 00f3940

Please sign in to comment.