add ability to unlink social accounts (twitter, linkedin)

This commit is contained in:
Ivan Sebastian
2016-09-28 21:26:17 +07:00
parent 9581beb05e
commit c9e2171c11
2 changed files with 109 additions and 27 deletions

View File

@@ -184,6 +184,12 @@ module.exports = function(app) {
getAccount
);
api.get(
'/account/unlink/:social',
sendNonUserToMap,
getUnlinkSocial
);
// Ensure these are the last routes!
api.get(
'/:username/front-end-certification',
@@ -266,6 +272,70 @@ module.exports = function(app) {
return res.redirect('/' + username);
}
function getUnlinkSocial(req, res, next) {
const { user } = req;
const { username } = user;
let social = req.params.social;
if (!social) {
req.flash('errors', {
msg: 'No social account found'
});
return res.redirect('/' + username);
}
social = social.toLowerCase();
const validSocialAccounts = ['twitter', 'linkedin'];
if (validSocialAccounts.indexOf(social) === -1) {
req.flash('errors', {
msg: 'Invalid social account'
});
return res.redirect('/' + username);
}
if (!user[social]) {
req.flash('errors', {
msg: `No ${social} account associated`
});
return res.redirect('/' + username);
}
const query = {
where: {
provider: social
}
};
return user.identities(query, function(err, identities) {
if (err) { return next(err); }
// assumed user identity is unique by provider
let identity = identities.shift();
if (!identity) {
req.flash('errors', {
msg: 'No social account found'
});
return res.redirect('/' + username);
}
return identity.destroy(function(err) {
if (err) { return next(err); }
const updateData = { [social]: null };
return user.update$(updateData)
.subscribe(() => {
debug(`${social} has been unlinked successfully`);
req.flash('info', {
msg: `You\'ve successfully unlinked your ${social}.`
});
return res.redirect('/' + username);
}, next);
});
});
}
function showUserProfile(req, res, next) {
const username = req.params.username.toLowerCase();
const { user } = req;