2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-11-29 11:54:08 -05:00
|
|
|
template = require 'templates/core/auth'
|
2014-11-28 20:49:41 -05:00
|
|
|
{loginUser, createUser, me} = require 'core/auth'
|
|
|
|
forms = require 'core/forms'
|
2014-05-22 14:24:35 -04:00
|
|
|
User = require 'models/User'
|
2014-11-28 20:49:41 -05:00
|
|
|
application = require 'core/application'
|
2016-02-25 18:24:16 -05:00
|
|
|
errors = require 'core/errors'
|
2014-05-22 14:24:35 -04:00
|
|
|
|
2014-07-23 10:02:45 -04:00
|
|
|
module.exports = class AuthModal extends ModalView
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'auth-modal'
|
2014-05-22 14:24:35 -04:00
|
|
|
template: template
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
events:
|
2016-02-25 18:24:16 -05:00
|
|
|
'click #switch-to-signup-btn': 'onSignupInstead'
|
2014-08-15 10:20:45 -04:00
|
|
|
'click #github-login-button': 'onGitHubLoginClicked'
|
2016-03-15 16:50:33 -04:00
|
|
|
'submit form': 'onSubmitForm'
|
2014-07-10 14:50:16 -04:00
|
|
|
'keyup #name': 'onNameChange'
|
2016-03-15 16:50:33 -04:00
|
|
|
'click #gplus-login-btn': 'onClickGPlusLoginButton'
|
2015-12-11 17:47:42 -05:00
|
|
|
'click #facebook-login-btn': 'onClickFacebookLoginButton'
|
2016-02-25 18:24:16 -05:00
|
|
|
'click #close-modal': 'hide'
|
2016-04-17 20:48:35 -04:00
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'errors:server-error': 'onServerError'
|
2014-05-22 14:24:35 -04:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
# Initialization
|
|
|
|
|
|
|
|
initialize: (options={}) ->
|
2016-01-21 17:41:57 -05:00
|
|
|
@previousFormInputs = options.initialValues or {}
|
2014-07-10 14:50:16 -04:00
|
|
|
|
2016-05-31 15:39:30 -04:00
|
|
|
# TODO: Switch to promises and state, rather than using defer to hackily enable buttons after render
|
|
|
|
application.gplusHandler.loadAPI({ success: => _.defer => @$('#gplus-login-btn').attr('disabled', false) })
|
|
|
|
application.facebookHandler.loadAPI({ success: => _.defer => @$('#facebook-login-btn').attr('disabled', false) })
|
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
c.showRequiredError = @options.showRequiredError
|
2015-04-08 21:31:13 -04:00
|
|
|
c.showSignupRationale = @options.showSignupRationale
|
2014-05-22 14:24:35 -04:00
|
|
|
c.mode = @mode
|
|
|
|
c.formValues = @previousFormInputs or {}
|
2014-07-10 14:50:16 -04:00
|
|
|
c.me = me
|
2014-05-22 14:24:35 -04:00
|
|
|
c
|
2014-08-27 15:24:03 -04:00
|
|
|
|
2014-12-06 20:27:02 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
2015-11-19 12:05:00 -05:00
|
|
|
@playSound 'game-menu-open'
|
2014-12-06 20:27:02 -05:00
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
afterInsert: ->
|
|
|
|
super()
|
2014-09-25 18:32:33 -04:00
|
|
|
_.delay (=> $('input:visible:first', @$el).focus()), 500
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
onSignupInstead: (e) ->
|
|
|
|
CreateAccountModal = require('./CreateAccountModal')
|
|
|
|
modal = new CreateAccountModal({initialValues: forms.formToObject @$el})
|
|
|
|
currentView.openModalView(modal)
|
2014-11-02 21:36:43 -05:00
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
onSubmitForm: (e) ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-05-22 14:24:35 -04:00
|
|
|
e.preventDefault()
|
|
|
|
forms.clearFormAlerts(@$el)
|
|
|
|
userObject = forms.formToObject @$el
|
2016-05-31 18:54:28 -04:00
|
|
|
res = tv4.validateMultiple userObject, formSchema
|
2014-05-22 14:24:35 -04:00
|
|
|
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
|
|
|
|
@enableModalInProgress(@$el) # TODO: part of forms
|
2015-02-11 19:12:26 -05:00
|
|
|
loginUser userObject, null, window.nextURL
|
2014-05-22 14:24:35 -04:00
|
|
|
|
|
|
|
onServerError: (e) -> # TODO: work error handling into a separate forms system
|
2014-06-30 22:16:26 -04:00
|
|
|
@disableModalInProgress(@$el)
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Google Plus
|
2014-11-14 14:04:17 -05:00
|
|
|
|
2016-03-15 16:50:33 -04:00
|
|
|
onClickGPlusLoginButton: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
btn = @$('#gplus-login-btn')
|
2016-05-31 15:39:30 -04:00
|
|
|
application.gplusHandler.connect({
|
2016-03-15 16:50:33 -04:00
|
|
|
context: @
|
|
|
|
success: ->
|
2016-05-31 15:39:30 -04:00
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.gplusHandler.loadPerson({
|
2016-03-15 16:50:33 -04:00
|
|
|
context: @
|
2016-05-31 15:39:30 -04:00
|
|
|
success: (gplusAttrs) ->
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchGPlusUser(gplusAttrs.gplusID, {
|
|
|
|
success: =>
|
|
|
|
me.loginGPlusUser(gplusAttrs.gplusID, {
|
|
|
|
success: -> window.location.reload()
|
2016-03-15 16:50:33 -04:00
|
|
|
error: @onGPlusLoginError
|
|
|
|
})
|
2016-05-31 15:39:30 -04:00
|
|
|
error: @onGPlusLoginError
|
2016-03-15 16:50:33 -04:00
|
|
|
})
|
2016-02-25 18:24:16 -05:00
|
|
|
})
|
|
|
|
})
|
2016-03-15 16:50:33 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
onGPlusLoginError: =>
|
|
|
|
btn = @$('#gplus-login-btn')
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.sign_in_with_gplus'))
|
|
|
|
btn.attr('disabled', false)
|
2016-03-15 16:50:33 -04:00
|
|
|
errors.showNotyNetworkError(arguments...)
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Facebook
|
2015-11-19 12:05:00 -05:00
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
onClickFacebookLoginButton: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
btn = @$('#facebook-login-btn')
|
2016-05-31 15:39:30 -04:00
|
|
|
application.facebookHandler.connect({
|
2016-03-15 16:50:33 -04:00
|
|
|
context: @
|
|
|
|
success: ->
|
2016-05-31 15:39:30 -04:00
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.facebookHandler.loadPerson({
|
2016-03-15 16:50:33 -04:00
|
|
|
context: @
|
2016-05-31 15:39:30 -04:00
|
|
|
success: (facebookAttrs) ->
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchFacebookUser(facebookAttrs.facebookID, {
|
|
|
|
success: =>
|
|
|
|
me.loginFacebookUser(facebookAttrs.facebookID, {
|
|
|
|
success: -> window.location.reload()
|
2016-03-15 16:50:33 -04:00
|
|
|
error: @onFacebookLoginError
|
|
|
|
})
|
2016-05-31 15:39:30 -04:00
|
|
|
error: @onFacebookLoginError
|
2016-03-15 16:50:33 -04:00
|
|
|
})
|
2016-02-25 18:24:16 -05:00
|
|
|
})
|
|
|
|
})
|
2016-03-15 16:50:33 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
onFacebookLoginError: =>
|
|
|
|
btn = @$('#facebook-login-btn')
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.sign_in_with_facebook'))
|
|
|
|
btn.attr('disabled', false)
|
|
|
|
errors.showNotyNetworkError(arguments...)
|
|
|
|
|
2015-12-11 17:47:42 -05:00
|
|
|
|
2015-11-19 12:05:00 -05:00
|
|
|
onHidden: ->
|
|
|
|
super()
|
|
|
|
@playSound 'game-menu-close'
|
2016-05-31 18:54:28 -04:00
|
|
|
|
|
|
|
formSchema = {
|
|
|
|
type: 'object'
|
|
|
|
properties: _.pick(User.schema.properties, 'email', 'password')
|
|
|
|
required: ['email', 'password']
|
|
|
|
}
|