Altered the G+ login button to only login when you click it, it gives feedback on login progress, and fixed a bug where sometimes there would be a cross-domain error because we were trying to serialize a window.

This commit is contained in:
Scott Erickson 2014-11-14 11:04:17 -08:00
parent 635c4e18df
commit 36305cde97
4 changed files with 64 additions and 9 deletions

View file

@ -28,7 +28,7 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
onGPlusLoaded: ->
session_state = null
if @accessToken
if @accessToken and me.get('gplusID')
# We need to check the current state, given our access token
gapi.auth.setToken 'token', @accessToken
session_state = @accessToken.session_state
@ -50,19 +50,22 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
onGPlusLogin: (e) =>
@loggedIn = true
storage.save(GPLUS_TOKEN_KEY, e)
try
# Without removing this, we sometimes get a cross-domain error
d = JSON.stringify(_.omit(e, 'g-oauth-window'))
storage.save(GPLUS_TOKEN_KEY, d)
catch e
console.error 'Unable to save G+ token key', e
@accessToken = e
@trigger 'logged-in'
return if (not me) or me.get 'gplusID' # so only get more data
loginCodeCombat: ->
# email and profile data loaded separately
@responsesComplete = 0
gapi.client.request(path: plusURL, callback: @onPersonEntityReceived)
gapi.client.load('oauth2', 'v2', =>
gapi.client.oauth2.userinfo.get().execute(@onEmailReceived))
shouldSave: false
responsesComplete: 0
onPersonEntityReceived: (r) =>
for gpProp, userProp of userPropsToSave
@ -74,18 +77,21 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
me.set(userProp, value)
@responsesComplete += 1
@personLoaded = true
@trigger 'person-loaded'
@saveIfAllDone()
onEmailReceived: (r) =>
newEmail = r.email and r.email isnt me.get('email')
return unless newEmail or me.get('anonymous')
return unless newEmail or me.get('anonymous', true)
me.set('email', r.email)
@shouldSave = true
@responsesComplete += 1
@emailLoaded = true
@trigger 'email-loaded'
@saveIfAllDone()
saveIfAllDone: =>
return unless @responsesComplete is 2
return unless @personLoaded and @emailLoaded
return unless me.get('email') and me.get('gplusID')
Backbone.Mediator.publish 'auth:logging-in-with-gplus', {}
@ -97,6 +103,7 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
patch._id = me.id
patch.email = me.get('email')
wasAnonymous = me.get('anonymous')
@trigger 'logging-into-codecombat'
me.save(patch, {
patch: true
type: 'PUT'

View file

@ -95,6 +95,10 @@
logging_in: "Logging In"
log_out: "Log Out"
recover: "recover account"
authenticate_gplus: 'Authenticate G+'
load_profile: 'Load G+ Profile'
load_email: 'Load G+ Email'
finishing: 'Finishing'
signup:
create_account_title: "Create Account to Save Progress"

View file

@ -0,0 +1,7 @@
ul.list-group
for step in steps
li.list-group-item(class=step.done ? 'list-group-item-success' : 'list-group-item-warning')
span(data-i18n=step.i18n)
if step.done
span.glyphicon.glyphicon-ok.pull-right

View file

@ -18,6 +18,7 @@ module.exports = class AuthModal extends ModalView
'click #github-login-button': 'onGitHubLoginClicked'
'submit': 'onSubmitForm' # handles both submit buttons
'keyup #name': 'onNameChange'
'click #gplus-login-button': 'onClickGPlusLogin'
subscriptions:
'errors:server-error': 'onServerError'
@ -113,3 +114,39 @@ module.exports = class AuthModal extends ModalView
onGitHubLoginClicked: ->
Backbone.Mediator.publish 'auth:log-in-with-github', {}
gplusAuthSteps: [
{ i18n: 'login.authenticate_gplus', done: false }
{ i18n: 'login.load_profile', done: false }
{ i18n: 'login.load_email', done: false }
{ i18n: 'login.finishing', done: false }
]
onClickGPlusLogin: ->
step.done = false for step in @gplusAuthSteps
handler = application.gplusHandler
@renderGPlusAuthChecklist()
@listenToOnce handler, 'logged-in', ->
@gplusAuthSteps[0].done = true
@renderGPlusAuthChecklist()
handler.loginCodeCombat()
@listenToOnce handler, 'person-loaded', ->
@gplusAuthSteps[1].done = true
@renderGPlusAuthChecklist()
@listenToOnce handler, 'email-loaded', ->
@gplusAuthSteps[2].done = true
@renderGPlusAuthChecklist()
@listenToOnce handler, 'logging-into-codecombat', ->
@gplusAuthSteps[3].done = true
@renderGPlusAuthChecklist()
renderGPlusAuthChecklist: ->
template = require 'templates/modal/auth-modal-gplus-checklist'
el = $(template({steps: @gplusAuthSteps}))
el.i18n()
@$el.find('.modal-body:visible').empty().append(el)
@$el.find('.modal-footer').remove()