add github check on login with github.

pull/1541/head
Berkeley Martinez 2015-08-04 14:52:06 -07:00
parent df786eaf2b
commit 9196d1c48e
1 changed files with 29 additions and 9 deletions

View File

@ -1,14 +1,16 @@
var debug = require('debug')('freecc:models:userIdent');
import debugFactory from 'debug';
var defaultProfileImage =
require('../utils/constantStrings.json').defaultProfileImage;
const debug = debugFactory('freecc:models:userIdent');
const { defaultProfileImage } = require('../utils/constantStrings.json');
function getFirstImageFromProfile(profile) {
return profile && profile.photos && profile.photos[0] ?
profile.photos[0].value :
null;
}
module.exports = function(UserIdent) {
export default function(UserIdent) {
UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance;
if (!userIdent) {
@ -16,13 +18,14 @@ module.exports = function(UserIdent) {
return next();
}
userIdent.user(function(err, user) {
let userChanged = false;
if (err) { return next(err); }
if (!user) {
debug('no user attached to identity!');
return next();
}
var picture = getFirstImageFromProfile(userIdent.profile);
const picture = getFirstImageFromProfile(userIdent.profile);
debug('picture', picture, user.picture);
// check if picture was found
@ -34,15 +37,32 @@ module.exports = function(UserIdent) {
(!user.picture || user.picture === defaultProfileImage)
) {
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) {
if (err) { return next(err); }
next();
});
}
debug('exiting after user ident');
debug('exiting after user identity before save');
next();
});
});
};
}