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:
|
|
|
|
# login buttons
|
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-02-25 18:24:16 -05:00
|
|
|
'submit form': 'onSubmitForm' # handles both submit buttons
|
2014-07-10 14:50:16 -04:00
|
|
|
'keyup #name': 'onNameChange'
|
2016-02-25 18:24:16 -05:00
|
|
|
'click #gplus-login-btn': 'onClickGPlusLogin'
|
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'
|
2014-05-22 14:24:35 -04:00
|
|
|
|
|
|
|
subscriptions:
|
2014-08-27 15:24:03 -04:00
|
|
|
'errors:server-error': 'onServerError'
|
2016-02-25 18:24:16 -05:00
|
|
|
'auth:facebook-api-loaded': 'onFacebookAPILoaded'
|
|
|
|
|
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 {}
|
2016-02-25 18:24:16 -05:00
|
|
|
@listenTo application.gplusHandler, 'logged-into-google', @onGPlusHandlerLoggedIntoGoogle
|
|
|
|
@listenTo application.gplusHandler, 'person-loaded', @onGPlusPersonLoaded
|
|
|
|
@listenTo application.gplusHandler, 'render-login-buttons', @onGPlusRenderLoginButtons
|
|
|
|
@listenTo application.facebookHandler, 'logged-into-facebook', @onFacebookHandlerLoggedIntoFacebook
|
|
|
|
@listenTo application.facebookHandler, 'person-loaded', @onFacebookPersonLoaded
|
2014-07-10 14:50:16 -04:00
|
|
|
|
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'
|
2016-02-25 18:24:16 -05:00
|
|
|
@$('#facebook-login-btn').attr('disabled', true) if not window.FB?
|
2014-12-06 20:27:02 -05:00
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
afterInsert: ->
|
|
|
|
super()
|
2014-10-15 16:43:26 -04:00
|
|
|
_.delay (=> application.router.renderLoginButtons()), 500
|
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
|
|
|
onGPlusRenderLoginButtons: ->
|
|
|
|
@$('#gplus-login-btn').attr('disabled', false)
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2016-02-25 18:24:16 -05:00
|
|
|
onFacebookAPILoaded: ->
|
|
|
|
@$('#facebook-login-btn').attr('disabled', false)
|
|
|
|
|
|
|
|
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
|
|
|
|
res = tv4.validateMultiple userObject, User.schema
|
|
|
|
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
|
|
|
|
|
|
|
onClickGPlusLogin: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
@clickedGPlusLogin = true
|
|
|
|
|
|
|
|
onGPlusHandlerLoggedIntoGoogle: ->
|
|
|
|
return unless @clickedGPlusLogin
|
|
|
|
application.gplusHandler.loadPerson()
|
|
|
|
btn = @$('#gplus-login-btn')
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
|
|
|
|
onGPlusPersonLoaded: (gplusAttrs) ->
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchGPlusUser(gplusAttrs.gplusID, {
|
|
|
|
success: =>
|
|
|
|
me.loginGPlusUser(gplusAttrs.gplusID, {
|
|
|
|
success: -> window.location.reload()
|
|
|
|
error: @onGPlusLoginError
|
|
|
|
})
|
|
|
|
error: @onGPlusLoginError
|
|
|
|
})
|
|
|
|
|
|
|
|
onGPlusLoginError: =>
|
|
|
|
btn = @$('#gplus-login-btn')
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.sign_in_with_gplus'))
|
|
|
|
btn.attr('disabled', false)
|
|
|
|
errors.showNotyNetworkError(arguments...)
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
@clickedFacebookLogin = true
|
|
|
|
if application.facebookHandler.loggedIn
|
|
|
|
@onFacebookHandlerLoggedIntoFacebook()
|
|
|
|
else
|
|
|
|
application.facebookHandler.loginThroughFacebook()
|
|
|
|
|
|
|
|
onFacebookHandlerLoggedIntoFacebook: ->
|
|
|
|
return unless @clickedFacebookLogin
|
|
|
|
application.facebookHandler.loadPerson()
|
|
|
|
btn = @$('#facebook-login-btn')
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
|
|
|
|
onFacebookPersonLoaded: (facebookAttrs) ->
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchFacebookUser(facebookAttrs.facebookID, {
|
|
|
|
success: =>
|
|
|
|
me.loginFacebookUser(facebookAttrs.facebookID, {
|
|
|
|
success: -> window.location.reload()
|
|
|
|
error: @onFacebookLoginError
|
|
|
|
})
|
|
|
|
error: @onFacebookLoginError
|
|
|
|
})
|
|
|
|
|
|
|
|
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'
|