Skip to content

Commit

Permalink
add ability to unlink social accounts (twitter, linkedin)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantedja committed Oct 27, 2016
1 parent 9581beb commit c9e2171
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 27 deletions.
66 changes: 39 additions & 27 deletions common/app/routes/settings/components/Social-Settings.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { PropTypes } from 'react';
import { Button } from 'react-bootstrap';
import FA from 'react-fontawesome';
import classnames from 'classnames';

export default function SocialSettings({
isGithubCool,
Expand All @@ -22,33 +23,44 @@ export default function SocialSettings({
{ githubCopy }
</Button>
];
if (isGithubCool && !isTwitter) {
buttons.push((
<Button
block={ true }
bsSize='lg'
className='btn-link-social btn-twitter'
href='/link/twitter'
key='twitter'
>
<FA name='twitter' />
Add my Twitter to my portfolio
</Button>
));
}
if (isGithubCool && !isLinkedIn) {
buttons.push((
<Button
block={ true }
bsSize='lg'
className='btn-link-social btn-linkedin'
href='/link/linkedin'
key='linkedin'
>
<FA name='linkedin' />
Add my LinkedIn to my portfolio
</Button>
));
const socials = [
{
isActive: isTwitter,
identifier: 'twitter',
text: 'Twitter'
},
{
isActive: isLinkedIn,
identifier: 'linkedin',
text: 'LinkedIn'
}
];
if (isGithubCool) {
socials.forEach(({ isActive, identifier, text }) => {
const socialClass = classnames(
'btn-link-social',
`btn-${identifier}`,
{ active: isActive }
);
const socialLink = isActive ?
`/account/unlink/${identifier}` :
`/link/${identifier}`;
const socialText = isTwitter ?
`Remove my ${text} from my portfolio` :
`Add my ${text} to my portfolio`;
buttons.push((
<Button
block={ true }
bsSize='lg'
className={ socialClass }
href={ socialLink }
key={ identifier }
>
<FA name={ identifier } />
{ socialText }
</Button>
));
});
}
return (<div>{ buttons }</div>);
}
Expand Down
70 changes: 70 additions & 0 deletions server/boot/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c9e2171

Please sign in to comment.