From 9196d1c48ececf281e767cfbcb566571e9b4653a Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Tue, 4 Aug 2015 14:52:06 -0700 Subject: [PATCH 1/2] add github check on login with github. --- common/models/User-Identity.js | 38 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/common/models/User-Identity.js b/common/models/User-Identity.js index 720e98b44a..781ea7d0ac 100644 --- a/common/models/User-Identity.js +++ b/common/models/User-Identity.js @@ -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(); }); }); -}; +} From e01876f3ab435d99b48f6ad78e6574a2c8fbb23b Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Tue, 4 Aug 2015 15:03:33 -0700 Subject: [PATCH 2/2] disable username updating when camper is githubCool --- server/views/account/account.jade | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/views/account/account.jade b/server/views/account/account.jade index 436651c1e0..81798a9511 100644 --- a/server/views/account/account.jade +++ b/server/views/account/account.jade @@ -32,7 +32,7 @@ block content .form-group label.col-sm-3.col-sm-offset-2.control-label(for='username') Username (path to public profile) * .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") alert(type='danger') span.ion-close-circled @@ -53,6 +53,10 @@ block content alert(type='danger') span.ion-close-circled | 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 label.col-sm-3.col-sm-offset-2.control-label(for='email') Email *