fix override user identity login to reformat provider
This commit is contained in:
@ -1,15 +1,132 @@
|
|||||||
|
import loopback from 'loopback';
|
||||||
import debugFactory from 'debug';
|
import debugFactory from 'debug';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
setProfileFromGithub,
|
setProfileFromGithub,
|
||||||
getFirstImageFromProfile
|
getFirstImageFromProfile,
|
||||||
|
getSocialProvider
|
||||||
} from '../../server/utils/auth';
|
} from '../../server/utils/auth';
|
||||||
|
|
||||||
const debug = debugFactory('freecc:models:userIdent');
|
const debug = debugFactory('freecc:models:userIdent');
|
||||||
|
|
||||||
const { defaultProfileImage } = require('../utils/constantStrings.json');
|
const { defaultProfileImage } = require('../utils/constantStrings.json');
|
||||||
|
|
||||||
|
function createAccessToken(user, ttl, cb) {
|
||||||
|
if (arguments.length === 2 && typeof ttl === 'function') {
|
||||||
|
cb = ttl;
|
||||||
|
ttl = 0;
|
||||||
|
}
|
||||||
|
user.accessTokens.create({
|
||||||
|
created: new Date(),
|
||||||
|
ttl: Math.min(ttl || user.constructor.settings.ttl,
|
||||||
|
user.constructor.settings.maxTTL)
|
||||||
|
}, cb);
|
||||||
|
}
|
||||||
|
|
||||||
export default function(UserIdent) {
|
export default function(UserIdent) {
|
||||||
|
// original source
|
||||||
|
// github.com/strongloop/loopback-component-passport
|
||||||
|
UserIdent.login = function(
|
||||||
|
provider,
|
||||||
|
authScheme,
|
||||||
|
profile,
|
||||||
|
credentials,
|
||||||
|
options,
|
||||||
|
cb
|
||||||
|
) {
|
||||||
|
options = options || {};
|
||||||
|
if (typeof options === 'function' && !cb) {
|
||||||
|
cb = options;
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
var autoLogin = options.autoLogin || !options.autoLogin;
|
||||||
|
var userIdentityModel = UserIdent;
|
||||||
|
profile.id = profile.id || profile.openid;
|
||||||
|
userIdentityModel.findOne({
|
||||||
|
where: {
|
||||||
|
provider: getSocialProvider(provider),
|
||||||
|
externalId: profile.id
|
||||||
|
}
|
||||||
|
}, function(err, identity) {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
if (identity) {
|
||||||
|
identity.credentials = credentials;
|
||||||
|
return identity.updateAttributes({
|
||||||
|
profile: profile,
|
||||||
|
credentials: credentials,
|
||||||
|
modified: new Date()
|
||||||
|
}, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
// Find the user for the given identity
|
||||||
|
return identity.user(function(err, user) {
|
||||||
|
// Create access token if the autoLogin flag is set to true
|
||||||
|
if (!err && user && autoLogin) {
|
||||||
|
return (options.createAccessToken || createAccessToken)(
|
||||||
|
user,
|
||||||
|
function(err, token) {
|
||||||
|
cb(err, user, identity, token);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
cb(err, user, identity);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Find the user model
|
||||||
|
var userModel = userIdentityModel.relations.user &&
|
||||||
|
userIdentityModel.relations.user.modelTo ||
|
||||||
|
loopback.getModelByType(loopback.User);
|
||||||
|
|
||||||
|
var userObj = options.profileToUser(provider, profile, options);
|
||||||
|
|
||||||
|
if (!userObj.email && !options.emailOptional) {
|
||||||
|
process.nextTick(function() {
|
||||||
|
return cb('email is missing from the user profile');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var query;
|
||||||
|
if (userObj.email) {
|
||||||
|
query = { or: [
|
||||||
|
{ username: userObj.username },
|
||||||
|
{ email: userObj.email }
|
||||||
|
]};
|
||||||
|
} else {
|
||||||
|
query = { username: userObj.username };
|
||||||
|
}
|
||||||
|
userModel.findOrCreate({ where: query }, userObj, function(err, user) {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
var date = new Date();
|
||||||
|
userIdentityModel.create({
|
||||||
|
provider: getSocialProvider(provider),
|
||||||
|
externalId: profile.id,
|
||||||
|
authScheme: authScheme,
|
||||||
|
profile: profile,
|
||||||
|
credentials: credentials,
|
||||||
|
userId: user.id,
|
||||||
|
created: date,
|
||||||
|
modified: date
|
||||||
|
}, function(err, identity) {
|
||||||
|
if (!err && user && autoLogin) {
|
||||||
|
return (options.createAccessToken || createAccessToken)(
|
||||||
|
user,
|
||||||
|
function(err, token) {
|
||||||
|
cb(err, user, identity, token);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
cb(err, user, identity);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
UserIdent.observe('before save', function(ctx, next) {
|
UserIdent.observe('before save', function(ctx, next) {
|
||||||
var userIdent = ctx.currentInstance || ctx.instance;
|
var userIdent = ctx.currentInstance || ctx.instance;
|
||||||
if (!userIdent) {
|
if (!userIdent) {
|
||||||
@ -42,7 +159,7 @@ export default function(UserIdent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(/github/).test(provider)) {
|
if (!(/github/).test(provider)) {
|
||||||
user[provider.split('-')[0]] = profile.username;
|
user[getSocialProvider(provider)] = profile.username;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if user signed in with github refresh their info
|
// if user signed in with github refresh their info
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { observeMethod, observeQuery } from '../utils/rx';
|
import { observeMethod, observeQuery } from '../utils/rx';
|
||||||
|
import { getSocialProvider } from '../utils/auth';
|
||||||
|
|
||||||
export default function({ models }) {
|
export default function({ models }) {
|
||||||
const { User, UserIdentity, UserCredential } = models;
|
const { User, UserIdentity, UserCredential } = models;
|
||||||
@ -22,14 +23,14 @@ export default function({ models }) {
|
|||||||
console.log('provider', provider);
|
console.log('provider', provider);
|
||||||
console.log('id', profile.id);
|
console.log('id', profile.id);
|
||||||
findIdent({
|
findIdent({
|
||||||
provider: provider,
|
provider: getSocialProvider(provider),
|
||||||
externalId: profile.id
|
externalId: profile.id
|
||||||
})
|
})
|
||||||
.flatMap(identity => {
|
.flatMap(identity => {
|
||||||
const modified = new Date();
|
const modified = new Date();
|
||||||
if (!identity || identity.externalId !== profile.id) {
|
if (!identity || identity.externalId !== profile.id) {
|
||||||
return observeQuery(UserIdentity, 'create', {
|
return observeQuery(UserIdentity, 'create', {
|
||||||
provider,
|
provider: getSocialProvider(provider),
|
||||||
externalId: profile.id,
|
externalId: profile.id,
|
||||||
authScheme,
|
authScheme,
|
||||||
profile,
|
profile,
|
||||||
@ -41,7 +42,7 @@ export default function({ models }) {
|
|||||||
}
|
}
|
||||||
identity.credentials = credentials;
|
identity.credentials = credentials;
|
||||||
return observeQuery(identity, 'updateAttributes', {
|
return observeQuery(identity, 'updateAttributes', {
|
||||||
profile,
|
profile: getSocialProvider(provider),
|
||||||
credentials,
|
credentials,
|
||||||
modified
|
modified
|
||||||
});
|
});
|
||||||
|
@ -40,3 +40,7 @@ export function getFirstImageFromProfile(profile) {
|
|||||||
profile.photos[0].value :
|
profile.photos[0].value :
|
||||||
null;
|
null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSocialProvider(provider) {
|
||||||
|
return provider.split('-')[0];
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user