user-identity

This commit is contained in:
Berkeley Martinez
2016-04-21 11:06:45 -07:00
parent 2224b8dec1
commit 428cf8135e

View File

@ -40,49 +40,45 @@ export default function(UserIdent) {
cb = options; cb = options;
options = {}; options = {};
} }
var autoLogin = options.autoLogin || !options.autoLogin; const autoLogin = options.autoLogin || !options.autoLogin;
var userIdentityModel = UserIdent; const userIdentityModel = UserIdent;
profile.id = profile.id || profile.openid; profile.id = profile.id || profile.openid;
userIdentityModel.findOne({ return userIdentityModel.findOne({
where: { where: {
provider: getSocialProvider(provider), provider: getSocialProvider(provider),
externalId: profile.id externalId: profile.id
} }
}, function(err, identity) { })
if (err) { .then(identity => {
return cb(err);
}
if (identity) { if (identity) {
identity.credentials = credentials; identity.credentials = credentials;
return identity.updateAttributes({ return identity.updateAttributes({
profile: profile, profile: profile,
credentials: credentials, credentials: credentials,
modified: new Date() modified: new Date()
}, function(err) { })
if (err) { .then(function() {
return cb(err); // Find the user for the given identity
} return identity.user(function(err, user) {
// Find the user for the given identity // Create access token if the autoLogin flag is set to true
return identity.user(function(err, user) { if (!err && user && autoLogin) {
// Create access token if the autoLogin flag is set to true return (options.createAccessToken || createAccessToken)(
if (!err && user && autoLogin) { user,
return (options.createAccessToken || createAccessToken)( function(err, token) {
user, cb(err, user, identity, token);
function(err, token) { }
cb(err, user, identity, token); );
} }
); return cb(err, user, identity);
} });
return cb(err, user, identity);
});
}); });
} }
// Find the user model // Find the user model
var userModel = userIdentityModel.relations.user && const userModel = userIdentityModel.relations.user &&
userIdentityModel.relations.user.modelTo || userIdentityModel.relations.user.modelTo ||
loopback.getModelByType(loopback.User); loopback.getModelByType(loopback.User);
var userObj = options.profileToUser(provider, profile, options); const userObj = options.profileToUser(provider, profile, options);
if (!userObj.email && !options.emailOptional) { if (!userObj.email && !options.emailOptional) {
process.nextTick(function() { process.nextTick(function() {
@ -90,7 +86,7 @@ export default function(UserIdent) {
}); });
} }
var query; const query;
if (userObj.email) { if (userObj.email) {
query = { or: [ query = { or: [
{ username: userObj.username }, { username: userObj.username },
@ -103,7 +99,7 @@ export default function(UserIdent) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
var date = new Date(); const date = new Date();
return userIdentityModel.create({ return userIdentityModel.create({
provider: getSocialProvider(provider), provider: getSocialProvider(provider),
externalId: profile.id, externalId: profile.id,
@ -129,7 +125,7 @@ export default function(UserIdent) {
}; };
UserIdent.observe('before save', function(ctx, next) { UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance; const userIdent = ctx.currentInstance || ctx.instance;
if (!userIdent) { if (!userIdent) {
debug('no user identity instance found'); debug('no user identity instance found');
return next(); return next();