fix account link failure now flashes

This commit is contained in:
Berkeley Martinez
2015-08-18 14:48:22 -07:00
parent d9d44ae509
commit b2f4e520cb
3 changed files with 23 additions and 11 deletions

View File

@@ -1,5 +1,9 @@
import{ Observable } from 'rx';
import { observeMethod, observeQuery } from '../utils/rx';
import { getSocialProvider } from '../utils/auth';
import debugFactory from 'debug';
const debug = debugFactory('fcc:userIdent');
export default function({ models }) {
const { User, UserIdentity, UserCredential } = models;
@@ -20,15 +24,18 @@ export default function({ models }) {
options = {};
}
const user$ = findUserById(userId);
console.log('provider', provider);
console.log('id', profile.id);
findIdent({
provider: getSocialProvider(provider),
externalId: profile.id
})
const query = {
where: {
provider: getSocialProvider(provider),
externalId: profile.id
}
};
debug('link identity query', query);
findIdent(query)
.flatMap(identity => {
const modified = new Date();
if (!identity || identity.externalId !== profile.id) {
if (!identity) {
return observeQuery(UserIdentity, 'create', {
provider: getSocialProvider(provider),
externalId: profile.id,
@@ -40,6 +47,11 @@ export default function({ models }) {
modified
});
}
if (identity.userId !== userId) {
return Observable.throw(
new Error('An account is already linked to that profile')
);
}
identity.credentials = credentials;
return observeQuery(identity, 'updateAttributes', {
profile: getSocialProvider(provider),