freeCodeCamp/server/utils/auth.js

62 lines
1.2 KiB
JavaScript
Raw Normal View History

import assign from 'object.assign';
2015-08-21 23:08:49 +00:00
const providerHash = {
facebook: ({ id }) => id,
twitter: ({ username }) => username,
linkedin({ _json }) {
return _json && _json.publicProfileUrl || null;
},
google: ({ id }) => id
};
export function getUsernameFromProvider(provider, profile) {
return typeof providerHash[provider] === 'function' ?
providerHash[provider](profile) :
null;
}
// using es6 argument destructing
export function setProfileFromGithub(
user,
{
profileUrl: githubURL,
username
},
{
id: githubId,
'avatar_url': picture,
email: githubEmail,
'created_at': joinedGithubOn,
blog: website,
location,
name
}
) {
return assign(
user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{
name,
username: username.toLowerCase(),
location,
joinedGithubOn,
website,
picture,
githubId,
githubURL,
githubEmail,
githubProfile: githubURL
}
);
}
export function getFirstImageFromProfile(profile) {
return profile && profile.photos && profile.photos[0] ?
profile.photos[0].value :
null;
}
export function getSocialProvider(provider) {
return provider.split('-')[0];
}