Merge branch 'staging' of github.com:FreeCodeCamp/freecodecamp i:wq

nto staging
This commit is contained in:
Quincy Larson
2015-08-04 15:10:15 -07:00
2 changed files with 34 additions and 10 deletions

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();
}); });
}); });
}; }

View File

@ -32,7 +32,7 @@ block content
.form-group .form-group
label.col-sm-3.col-sm-offset-2.control-label(for='username') Username (path to public profile) * label.col-sm-3.col-sm-offset-2.control-label(for='username') Username (path to public profile) *
.col-sm-4 .col-sm-4
input.form-control(type='text', placeholder='username' name='username', autocomplete="off", id='username', ng-model='user.username', required='required', ng-minlength='5', ng-maxlength='20', ng-keypress='', unique-username='', ng-pattern="/^[A-z0-9_]+$/") input.form-control(type='text', placeholder='username' name='username', autocomplete="off", id='username', ng-model='user.username', required='required', ng-minlength='5', ng-maxlength='20', ng-keypress='', unique-username='', ng-pattern="/^[A-z0-9_]+$/", ng-disabled='user.isGithubCool')
.col-sm-4.col-sm-offset-5(ng-cloak, ng-show="profileForm.username.$error.pattern") .col-sm-4.col-sm-offset-5(ng-cloak, ng-show="profileForm.username.$error.pattern")
alert(type='danger') alert(type='danger')
span.ion-close-circled span.ion-close-circled
@ -53,6 +53,10 @@ block content
alert(type='danger') alert(type='danger')
span.ion-close-circled span.ion-close-circled
| That username is already in use. | That username is already in use.
.col-sm-4.col-sm-offset-5(ng-cloak, ng-show="user.isGithubCool")
alert(type='info')
span.ion-close-circled
| You are GithubCool! Your username cannot be changed.
.form-group .form-group
label.col-sm-3.col-sm-offset-2.control-label(for='email') Email * label.col-sm-3.col-sm-offset-2.control-label(for='email') Email *