Merge branch 'adeeb1-master'

This commit is contained in:
Scott Erickson 2016-02-19 10:07:52 -08:00
commit cea1a70ab4
3 changed files with 31 additions and 22 deletions

View file

@ -19,4 +19,4 @@ module.exports = class GitHubHandler extends CocoClass
client_id: @clientID client_id: @clientID
redirect_uri: @redirectURI redirect_uri: @redirectURI
location.href = "https://github.com/login/oauth/authorize?" + $.param(request) location.href = "https://github.com/login/oauth/authorize?" + $.param(request)

View file

@ -83,7 +83,7 @@
h3(data-i18n="signup.creating") Creating Account... h3(data-i18n="signup.creating") Creating Account...
.auth-network-logins .auth-network-logins
// GitHub login too buggy to survive // GitHub login complete, but the button does not fit in with the design yet. Hidden for now
//div.network-login //div.network-login
// btn.btn.btn-sm.github-login-button#github-login-button // btn.btn.btn-sm.github-login-button#github-login-button
// img(src="/images/pages/modal/auth/github_icon.png") // img(src="/images/pages/modal/auth/github_icon.png")

View file

@ -28,26 +28,35 @@ module.exports.setup = (app) ->
return log.error err if err? return log.error err if err?
githubUser = JSON.parse response githubUser = JSON.parse response
log.info 'Got GitHub auth callback response', githubUser log.info 'Got GitHub auth callback response', githubUser
emailLower = githubUser.email?.toLowerCase()
if not emailLower request.get {uri: 'https://api.github.com/user/emails', headers: headers}, (err, r, response) ->
return errors.serverError res, "Problem finding GitHub user with that identity." return log.error err if err?
githubUserEmails = JSON.parse response
log.info 'Got GitHub user emails', githubUserEmails
emailLower = _.find githubUserEmails, (email) -> email.primary is true
emailLower = emailLower.email.toLowerCase()
log.info 'Got primary Github email', emailLower
if not emailLower
return errors.serverError res, "Problem finding GitHub user with that identity."
# GitHub users can change emails # GitHub users can change emails
User.findOne {$or: [{emailLower: emailLower}, {githubID: githubUser.id}]}, (err, user) -> User.findOne {$or: [{emailLower: emailLower}, {githubID: githubUser.id}]}, (err, user) ->
return errors.serverError res, err if err?
wrapup = (err, user) ->
return errors.serverError res, err if err? return errors.serverError res, err if err?
req.login (user), (err) -> wrapup = (err, user) ->
return errors.serverError res, err if err? return errors.serverError res, err if err?
res.redirect '/' req.login (user), (err) ->
unless user return errors.serverError res, err if err?
req.user.set 'email', githubUser.email res.redirect '/'
req.user.set 'githubID', githubUser.id unless user
req.user.save wrapup req.user.set 'email', emailLower
else if user.get('githubID') isnt githubUser.id # Add or replace githubID to/with existing user req.user.set 'githubID', githubUser.id
user.set 'githubID', githubUser.id req.user.save wrapup
user.save wrapup else if user.get('githubID') isnt githubUser.id # Add or replace githubID to/with existing user
else if user.get('emailLower') isnt emailLower # Existing GitHub user with us changed email user.set 'githubID', githubUser.id
user.update {email: githubUser.email}, (err) -> wrapup err, user user.save wrapup
else # All good you've been here before else if user.get('emailLower') isnt emailLower # Existing GitHub user with us changed email
wrapup null, user user.update {email: emailLower}, (err) -> wrapup err, user
else # All good you've been here before
wrapup null, user