fix social auth scheme

closes #1734
This commit is contained in:
Berkeley Martinez
2015-08-12 20:09:15 -07:00
parent eb07cbfea6
commit 4b0a9bef0a
3 changed files with 53 additions and 55 deletions

View File

@ -132,52 +132,54 @@ export default function(UserIdent) {
if (!userIdent) { if (!userIdent) {
debug('no user identity instance found'); debug('no user identity instance found');
return next(); return next();
}
userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); }
if (!user) {
debug('no user attached to identity!');
return next();
} }
userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); }
if (!user) {
debug('no user attached to identity!');
return next();
}
const { profile, provider } = userIdent; const { profile, provider } = userIdent;
const picture = getFirstImageFromProfile(profile); const picture = getFirstImageFromProfile(profile);
debug('picture', picture, user.picture); debug('picture', picture, user.picture);
// check if picture was found // check if picture was found
// check if user has no picture // check if user has no picture
// check if user has default picture // check if user has default picture
// set user.picture from oauth provider // set user.picture from oauth provider
if ( if (
picture && picture &&
(!user.picture || user.picture === defaultProfileImage) (!user.picture || user.picture === defaultProfileImage)
) { ) {
debug('setting user picture'); debug('setting user picture');
user.picture = picture; user.picture = picture;
userChanged = true; userChanged = true;
} }
if (!(/github/).test(provider)) { if (!(/github/).test(provider)) {
user[getSocialProvider(provider)] = profile.username; debug('setting social', provider, (/github/g).test(provider));
} debug('profile username', profile.username);
user[provider] = profile.username;
}
// if user signed in with github refresh their info // if user signed in with github refresh their info
if (/github/.test(provider)) { if (/github/.test(provider)) {
debug("user isn't github cool or username from github is different"); debug("user isn't github cool or username from github is different");
setProfileFromGithub(user, profile, profile._json); setProfileFromGithub(user, profile, profile._json);
userChanged = true; userChanged = true;
} }
if (userChanged) { if (userChanged) {
return user.save(function(err) { return user.save(function(err) {
if (err) { return next(err); } if (err) { return next(err); }
next(); next();
}); });
} }
debug('exiting after user identity before save'); debug('exiting after user identity before save');
next(); next();
}); });
}); });
} }

View File

@ -72,25 +72,20 @@
"type": "string", "type": "string",
"default": "" "default": ""
}, },
"linkedinProfile": { "linkedin": {
"type": "string", "type": "string"
"default": ""
}, },
"githubProfile": { "codepen": {
"type": "string", "type": "string"
"default": ""
}, },
"codepenProfile": { "twitter": {
"type": "string", "type": "string"
"default": ""
}, },
"twitterHandle": { "facebook": {
"type": "string", "type": "string"
"default": ""
}, },
"facebookProfile": { "google": {
"type": "string", "type": "string"
"default": ""
}, },
"completedBonfires": { "completedBonfires": {
"type": [ "type": [

View File

@ -11,6 +11,7 @@ var uuid = require('node-uuid'),
passportProviders = require('./passport-providers'); passportProviders = require('./passport-providers');
var setProfileFromGithub = require('./utils/auth').setProfileFromGithub; var setProfileFromGithub = require('./utils/auth').setProfileFromGithub;
var getSocialProvider = require('./utils/auth').getSocialProvider;
var generateKey = var generateKey =
require('loopback-component-passport/lib/models/utils').generateKey; require('loopback-component-passport/lib/models/utils').generateKey;
@ -67,7 +68,7 @@ var passportOptions = {
} }
if (!(/github/).test(provider)) { if (!(/github/).test(provider)) {
userObj[provider.split('-')[0]] = profile.username; userObj[getSocialProvider(provider)] = profile.username;
} }
if (/github/.test(provider)) { if (/github/.test(provider)) {