linking accounts now works.
bug, need to change how loopback generates provider string.
This commit is contained in:
@ -24,7 +24,7 @@ export default function(UserIdent) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { profile } = userIdent;
|
const { profile, provider } = userIdent;
|
||||||
const picture = getFirstImageFromProfile(profile);
|
const picture = getFirstImageFromProfile(profile);
|
||||||
|
|
||||||
debug('picture', picture, user.picture);
|
debug('picture', picture, user.picture);
|
||||||
@ -41,8 +41,12 @@ export default function(UserIdent) {
|
|||||||
userChanged = true;
|
userChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(/github/).test(provider)) {
|
||||||
|
user[provider.split('-')[0]] = profile.username;
|
||||||
|
}
|
||||||
|
|
||||||
// if user signed in with github refresh their info
|
// if user signed in with github refresh their info
|
||||||
if (/github/.test(userIdent.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;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
var Rx = require('rx');
|
import { Observable } from 'rx';
|
||||||
var debug = require('debug')('freecc:user:remote');
|
import debugFactory from 'debug';
|
||||||
|
|
||||||
|
const debug = debugFactory('freecc:user:remote');
|
||||||
|
|
||||||
function destroyAllRelated(id, Model) {
|
function destroyAllRelated(id, Model) {
|
||||||
return Rx.Observable.fromNodeCallback(
|
return Observable.fromNodeCallback(
|
||||||
Model.destroyAll,
|
Model.destroyAll,
|
||||||
Model
|
Model
|
||||||
)({ userId: id });
|
)({ userId: id });
|
||||||
@ -19,7 +21,7 @@ module.exports = function(app) {
|
|||||||
if (!id) {
|
if (!id) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
Rx.Observable.combineLatest(
|
Observable.combineLatest(
|
||||||
destroyAllRelated(id, UserIdentity),
|
destroyAllRelated(id, UserIdentity),
|
||||||
destroyAllRelated(id, UserCredential),
|
destroyAllRelated(id, UserCredential),
|
||||||
function(identData, credData) {
|
function(identData, credData) {
|
||||||
|
59
server/boot/a-extendUserIdent.js
Normal file
59
server/boot/a-extendUserIdent.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { observeMethod, observeQuery } from '../utils/rx';
|
||||||
|
|
||||||
|
export default function({ models }) {
|
||||||
|
const { User, UserIdentity, UserCredential } = models;
|
||||||
|
const findUserById = observeMethod(User, 'findById');
|
||||||
|
const findIdent = observeMethod(UserIdentity, 'findOne');
|
||||||
|
|
||||||
|
UserIdentity.link = function(
|
||||||
|
userId,
|
||||||
|
provider,
|
||||||
|
authScheme,
|
||||||
|
profile,
|
||||||
|
credentials,
|
||||||
|
options = {},
|
||||||
|
cb
|
||||||
|
) {
|
||||||
|
if (typeof options === 'function' && !cb) {
|
||||||
|
cb = options;
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
const user$ = findUserById(userId);
|
||||||
|
console.log('provider', provider);
|
||||||
|
console.log('id', profile.id);
|
||||||
|
findIdent({
|
||||||
|
provider: provider,
|
||||||
|
externalId: profile.id
|
||||||
|
})
|
||||||
|
.flatMap(identity => {
|
||||||
|
const modified = new Date();
|
||||||
|
if (!identity || identity.externalId !== profile.id) {
|
||||||
|
return observeQuery(UserIdentity, 'create', {
|
||||||
|
provider,
|
||||||
|
externalId: profile.id,
|
||||||
|
authScheme,
|
||||||
|
profile,
|
||||||
|
credentials,
|
||||||
|
userId,
|
||||||
|
created: modified,
|
||||||
|
modified
|
||||||
|
});
|
||||||
|
}
|
||||||
|
identity.credentials = credentials;
|
||||||
|
return observeQuery(identity, 'updateAttributes', {
|
||||||
|
profile,
|
||||||
|
credentials,
|
||||||
|
modified
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.withLatestFrom(user$, (identity, user) => ({ identity, user }))
|
||||||
|
.subscribe(
|
||||||
|
({ identity, user }) => {
|
||||||
|
cb(null, user, identity);
|
||||||
|
},
|
||||||
|
cb
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
UserCredential.link = UserIdentity.link.bind(UserIdentity);
|
||||||
|
}
|
@ -92,6 +92,7 @@ module.exports = {
|
|||||||
failureRedirect: failureRedirect,
|
failureRedirect: failureRedirect,
|
||||||
consumerKey: process.env.TWITTER_KEY,
|
consumerKey: process.env.TWITTER_KEY,
|
||||||
consumerSecret: process.env.TWITTER_SECRET,
|
consumerSecret: process.env.TWITTER_SECRET,
|
||||||
|
link: true,
|
||||||
failureFlash: true
|
failureFlash: true
|
||||||
},
|
},
|
||||||
'linkedin-login': {
|
'linkedin-login': {
|
||||||
@ -126,6 +127,7 @@ module.exports = {
|
|||||||
authOptions: {
|
authOptions: {
|
||||||
state: process.env.LINKEDIN_STATE
|
state: process.env.LINKEDIN_STATE
|
||||||
},
|
},
|
||||||
|
link: true,
|
||||||
failureFlash: true
|
failureFlash: true
|
||||||
},
|
},
|
||||||
'github-login': {
|
'github-login': {
|
||||||
@ -154,6 +156,7 @@ module.exports = {
|
|||||||
clientID: process.env.GITHUB_ID,
|
clientID: process.env.GITHUB_ID,
|
||||||
clientSecret: process.env.GITHUB_SECRET,
|
clientSecret: process.env.GITHUB_SECRET,
|
||||||
scope: ['email'],
|
scope: ['email'],
|
||||||
|
link: true,
|
||||||
failureFlash: true
|
failureFlash: true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -66,6 +66,10 @@ var passportOptions = {
|
|||||||
userObj.email = email;
|
userObj.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(/github/).test(provider)) {
|
||||||
|
userObj[provider.split('-')[0]] = profile.username;
|
||||||
|
}
|
||||||
|
|
||||||
if (/github/.test(provider)) {
|
if (/github/.test(provider)) {
|
||||||
setProfileFromGithub(userObj, profile, profile._json);
|
setProfileFromGithub(userObj, profile, profile._json);
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ exports.saveInstance = function saveInstance(instance) {
|
|||||||
// alias saveInstance
|
// alias saveInstance
|
||||||
exports.saveUser = exports.saveInstance;
|
exports.saveUser = exports.saveInstance;
|
||||||
|
|
||||||
exports.observableQueryFromModel =
|
exports.observeQuery = exports.observableQueryFromModel =
|
||||||
function observableQueryFromModel(Model, method, query) {
|
function observableQueryFromModel(Model, method, query) {
|
||||||
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
|
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
|
||||||
};
|
};
|
||||||
|
@ -8,7 +8,7 @@ block content
|
|||||||
.row
|
.row
|
||||||
.col-xs-12
|
.col-xs-12
|
||||||
if (!user.isGithubCool)
|
if (!user.isGithubCool)
|
||||||
a.btn.btn-lg.btn-block.btn-github.btn-link-social(href='/auth/github')
|
a.btn.btn-lg.btn-block.btn-github.btn-link-social(href='/link/github')
|
||||||
i.fa.fa-github
|
i.fa.fa-github
|
||||||
| Link my GitHub to unlock this profile
|
| Link my GitHub to unlock this profile
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user