add github check on login with github.

This commit is contained in:
Berkeley Martinez
2015-08-04 14:52:06 -07:00
parent df786eaf2b
commit 9196d1c48e

View File

@ -1,14 +1,16 @@
var debug = require('debug')('freecc:models:userIdent'); import debugFactory from 'debug';
var defaultProfileImage = const debug = debugFactory('freecc:models:userIdent');
require('../utils/constantStrings.json').defaultProfileImage;
const { defaultProfileImage } = require('../utils/constantStrings.json');
function getFirstImageFromProfile(profile) { function getFirstImageFromProfile(profile) {
return profile && profile.photos && profile.photos[0] ? return profile && profile.photos && profile.photos[0] ?
profile.photos[0].value : profile.photos[0].value :
null; null;
} }
module.exports = function(UserIdent) {
export default function(UserIdent) {
UserIdent.observe('before save', function(ctx, next) { UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance; var userIdent = ctx.currentInstance || ctx.instance;
if (!userIdent) { if (!userIdent) {
@ -16,13 +18,14 @@ module.exports = function(UserIdent) {
return next(); return next();
} }
userIdent.user(function(err, user) { userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); } if (err) { return next(err); }
if (!user) { if (!user) {
debug('no user attached to identity!'); debug('no user attached to identity!');
return next(); return next();
} }
var picture = getFirstImageFromProfile(userIdent.profile); const picture = getFirstImageFromProfile(userIdent.profile);
debug('picture', picture, user.picture); debug('picture', picture, user.picture);
// check if picture was found // check if picture was found
@ -34,15 +37,32 @@ module.exports = function(UserIdent) {
(!user.picture || user.picture === defaultProfileImage) (!user.picture || user.picture === defaultProfileImage)
) { ) {
debug('setting user picture'); debug('setting user picture');
user.picture = userIdent.profile.photos[0].value; user.picture = picture;
userChanged = true;
}
// if user is not github cool
// and user signed in with github
// then make them github cool
// and set their username from their github profile.
if (!user.isGithubCool && userIdent.provider === 'github-login') {
debug(`
user isn't github cool yet but signed in with github
lets make them cool!
`);
user.isGithubCool = true;
user.username = userIdent.profile.username.toLowerCase();
userChanged = true;
}
if (userChanged) {
return user.save(function(err) { return user.save(function(err) {
if (err) { return next(err); } if (err) { return next(err); }
next(); next();
}); });
} }
debug('exiting after user identity before save');
debug('exiting after user ident');
next(); next();
}); });
}); });
}; }