linking accounts now works.

bug, need to change how loopback generates provider string.
This commit is contained in:
Berkeley Martinez
2015-08-12 16:25:52 -07:00
parent a4e220e2bc
commit fc29c1fd9b
7 changed files with 80 additions and 8 deletions

View File

@ -24,7 +24,7 @@ export default function(UserIdent) {
return next();
}
const { profile } = userIdent;
const { profile, provider } = userIdent;
const picture = getFirstImageFromProfile(profile);
debug('picture', picture, user.picture);
@ -41,8 +41,12 @@ export default function(UserIdent) {
userChanged = true;
}
if (!(/github/).test(provider)) {
user[provider.split('-')[0]] = profile.username;
}
// 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");
setProfileFromGithub(user, profile, profile._json);
userChanged = true;

View File

@ -1,8 +1,10 @@
var Rx = require('rx');
var debug = require('debug')('freecc:user:remote');
import { Observable } from 'rx';
import debugFactory from 'debug';
const debug = debugFactory('freecc:user:remote');
function destroyAllRelated(id, Model) {
return Rx.Observable.fromNodeCallback(
return Observable.fromNodeCallback(
Model.destroyAll,
Model
)({ userId: id });
@ -19,7 +21,7 @@ module.exports = function(app) {
if (!id) {
return next();
}
Rx.Observable.combineLatest(
Observable.combineLatest(
destroyAllRelated(id, UserIdentity),
destroyAllRelated(id, UserCredential),
function(identData, credData) {

View 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);
}

View File

@ -92,6 +92,7 @@ module.exports = {
failureRedirect: failureRedirect,
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
link: true,
failureFlash: true
},
'linkedin-login': {
@ -126,6 +127,7 @@ module.exports = {
authOptions: {
state: process.env.LINKEDIN_STATE
},
link: true,
failureFlash: true
},
'github-login': {
@ -154,6 +156,7 @@ module.exports = {
clientID: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
scope: ['email'],
link: true,
failureFlash: true
}
};

View File

@ -66,6 +66,10 @@ var passportOptions = {
userObj.email = email;
}
if (!(/github/).test(provider)) {
userObj[provider.split('-')[0]] = profile.username;
}
if (/github/.test(provider)) {
setProfileFromGithub(userObj, profile, profile._json);
}

View File

@ -22,7 +22,7 @@ exports.saveInstance = function saveInstance(instance) {
// alias saveInstance
exports.saveUser = exports.saveInstance;
exports.observableQueryFromModel =
exports.observeQuery = exports.observableQueryFromModel =
function observableQueryFromModel(Model, method, query) {
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
};

View File

@ -8,7 +8,7 @@ block content
.row
.col-xs-12
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
| Link my GitHub to unlock this profile
else