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'
|
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-11-02 21:36:43 -05:00
|
|
|
mode: 'signup' # or 'login'
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
events:
|
|
|
|
# login buttons
|
2014-06-30 22:16:26 -04:00
|
|
|
'click #switch-to-signup-button': 'onSignupInstead'
|
2014-11-02 21:36:43 -05:00
|
|
|
'click #switch-to-login-button': 'onLoginInstead'
|
2014-08-15 10:20:45 -04:00
|
|
|
'click #github-login-button': 'onGitHubLoginClicked'
|
2014-05-22 14:24:35 -04:00
|
|
|
'submit': 'onSubmitForm' # handles both submit buttons
|
2014-07-10 14:50:16 -04:00
|
|
|
'keyup #name': 'onNameChange'
|
2014-11-14 14:04:17 -05:00
|
|
|
'click #gplus-login-button': 'onClickGPlusLogin'
|
2014-12-06 20:27:02 -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'
|
|
|
|
'auth:logging-in-with-facebook': 'onLoggingInWithFacebook'
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-07-10 14:50:16 -04:00
|
|
|
constructor: (options) ->
|
2014-11-25 13:19:38 -05:00
|
|
|
options ?= {}
|
2014-07-10 14:50:16 -04:00
|
|
|
@onNameChange = _.debounce @checkNameExists, 500
|
|
|
|
super options
|
2014-11-22 20:38:01 -05:00
|
|
|
@mode = options.mode if options.mode
|
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()
|
|
|
|
@$el.toggleClass('signup', @mode is 'signup').toggleClass('login', @mode is 'login')
|
|
|
|
|
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
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
onSignupInstead: (e) ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-05-22 14:24:35 -04:00
|
|
|
@mode = 'signup'
|
|
|
|
@previousFormInputs = forms.formToObject @$el
|
|
|
|
@render()
|
|
|
|
_.delay application.router.renderLoginButtons, 500
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-11-02 21:36:43 -05:00
|
|
|
onLoginInstead: (e) ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-11-02 21:36:43 -05:00
|
|
|
@mode = 'login'
|
|
|
|
@previousFormInputs = forms.formToObject @$el
|
|
|
|
@render()
|
|
|
|
_.delay application.router.renderLoginButtons, 500
|
|
|
|
|
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()
|
|
|
|
if @mode is 'login' then @loginAccount() else @createAccount()
|
|
|
|
false
|
|
|
|
|
|
|
|
loginAccount: ->
|
|
|
|
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
|
|
|
|
2015-06-30 12:35:45 -04:00
|
|
|
emailCheck = ->
|
|
|
|
email = $('#email', @$el).val()
|
|
|
|
filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
|
|
|
|
unless filter.test(email)
|
|
|
|
forms.setErrorToProperty @$el, 'email', "Please enter a valid email address", true
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2014-05-22 14:24:35 -04:00
|
|
|
createAccount: ->
|
2015-06-30 12:35:45 -04:00
|
|
|
unless emailCheck() is true
|
|
|
|
return
|
2014-05-22 14:24:35 -04:00
|
|
|
forms.clearFormAlerts(@$el)
|
|
|
|
userObject = forms.formToObject @$el
|
|
|
|
delete userObject.subscribe
|
2014-07-10 14:50:16 -04:00
|
|
|
delete userObject.name if userObject.name is ''
|
|
|
|
userObject.name = @suggestedName if @suggestedName
|
2014-06-30 22:16:26 -04:00
|
|
|
for key, val of me.attributes when key in ['preferredLanguage', 'testGroupNumber', 'dateCreated', 'wizardColor1', 'name', 'music', 'volume', 'emails']
|
2014-05-22 14:24:35 -04:00
|
|
|
userObject[key] ?= val
|
2014-09-01 12:11:10 -04:00
|
|
|
subscribe = @$el.find('#subscribe').prop('checked')
|
2014-05-22 14:24:35 -04:00
|
|
|
userObject.emails ?= {}
|
|
|
|
userObject.emails.generalNews ?= {}
|
|
|
|
userObject.emails.generalNews.enabled = subscribe
|
|
|
|
res = tv4.validateMultiple userObject, User.schema
|
|
|
|
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
|
2014-11-21 19:23:26 -05:00
|
|
|
Backbone.Mediator.publish "auth:signed-up", {}
|
2014-12-08 18:00:49 -05:00
|
|
|
window.tracker?.trackEvent 'Finished Signup', label: 'CodeCombat'
|
2014-05-22 14:24:35 -04:00
|
|
|
@enableModalInProgress(@$el)
|
2014-12-28 16:25:20 -05:00
|
|
|
createUser userObject, null, window.nextURL
|
2014-05-22 14:24:35 -04:00
|
|
|
|
|
|
|
onLoggingInWithFacebook: (e) ->
|
|
|
|
modal = $('.modal:visible', @$el)
|
|
|
|
@enableModalInProgress(modal) # TODO: part of forms
|
|
|
|
|
|
|
|
onServerError: (e) -> # TODO: work error handling into a separate forms system
|
2014-06-30 22:16:26 -04:00
|
|
|
@disableModalInProgress(@$el)
|
2014-07-10 14:50:16 -04:00
|
|
|
|
|
|
|
checkNameExists: =>
|
|
|
|
name = $('#name', @$el).val()
|
|
|
|
return forms.clearFormAlerts(@$el) if name is ''
|
|
|
|
User.getUnconflictedName name, (newName) =>
|
|
|
|
forms.clearFormAlerts(@$el)
|
|
|
|
if name is newName
|
|
|
|
@suggestedName = undefined
|
|
|
|
else
|
|
|
|
@suggestedName = newName
|
|
|
|
forms.setErrorToProperty @$el, 'name', "That name is taken! How about #{newName}?", true
|
2014-08-15 10:20:45 -04:00
|
|
|
|
|
|
|
onGitHubLoginClicked: ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'auth:log-in-with-github', {}
|
2014-11-14 14:04:17 -05:00
|
|
|
|
|
|
|
gplusAuthSteps: [
|
|
|
|
{ i18n: 'login.authenticate_gplus', done: false }
|
|
|
|
{ i18n: 'login.load_profile', done: false }
|
|
|
|
{ i18n: 'login.finishing', done: false }
|
|
|
|
]
|
|
|
|
|
|
|
|
onClickGPlusLogin: ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-11-14 14:04:17 -05:00
|
|
|
step.done = false for step in @gplusAuthSteps
|
|
|
|
handler = application.gplusHandler
|
2014-11-21 19:23:26 -05:00
|
|
|
|
2014-11-14 14:04:17 -05:00
|
|
|
@listenToOnce handler, 'logged-in', ->
|
|
|
|
@gplusAuthSteps[0].done = true
|
|
|
|
@renderGPlusAuthChecklist()
|
|
|
|
handler.loginCodeCombat()
|
|
|
|
@listenToOnce handler, 'person-loaded', ->
|
|
|
|
@gplusAuthSteps[1].done = true
|
|
|
|
@renderGPlusAuthChecklist()
|
|
|
|
|
|
|
|
@listenToOnce handler, 'logging-into-codecombat', ->
|
2015-02-13 14:18:00 -05:00
|
|
|
@gplusAuthSteps[2].done = true
|
2014-11-14 14:04:17 -05:00
|
|
|
@renderGPlusAuthChecklist()
|
2014-11-21 19:23:26 -05:00
|
|
|
|
2014-11-14 14:04:17 -05:00
|
|
|
renderGPlusAuthChecklist: ->
|
2014-12-03 19:40:28 -05:00
|
|
|
template = require 'templates/core/auth-modal-gplus-checklist'
|
2014-11-14 14:04:17 -05:00
|
|
|
el = $(template({steps: @gplusAuthSteps}))
|
|
|
|
el.i18n()
|
|
|
|
@$el.find('.modal-body:visible').empty().append(el)
|
|
|
|
@$el.find('.modal-footer').remove()
|