codecombat/app/lib/auth.coffee

83 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
{backboneFailure, genericFailure} = require 'lib/errors'
User = require 'models/User'
2014-01-26 17:44:08 -05:00
storage = require 'lib/storage'
2014-01-03 13:32:13 -05:00
module.exports.CURRENT_USER_KEY = CURRENT_USER_KEY = 'whoami'
BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
module.exports.createUser = (userObject, failure=backboneFailure) ->
user = new User(userObject)
user.save({}, {
error: failure,
success: (model) ->
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, model)
2014-01-03 13:32:13 -05:00
window.location.reload()
})
module.exports.loginUser = (userObject, failure=genericFailure) ->
jqxhr = $.post('/auth/login',
{
username:userObject.email,
password:userObject.password
},
(model) ->
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, model)
2014-01-03 13:32:13 -05:00
window.location.reload()
)
jqxhr.fail(failure)
module.exports.logoutUser = ->
FB?.logout?()
2014-01-03 13:32:13 -05:00
res = $.post('/auth/logout', {}, ->
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, null)
2014-01-03 13:32:13 -05:00
window.location.reload()
)
res.fail(genericFailure)
init = ->
2014-01-06 20:45:35 -05:00
# Load the user from local storage, and refresh it from the server.
2014-01-03 13:32:13 -05:00
# Also refresh and cache the gravatar info.
2014-01-26 17:44:08 -05:00
storedUser = storage.load(CURRENT_USER_KEY)
2014-01-06 20:45:35 -05:00
firstTime = not storedUser
module.exports.me = window.me = new User(storedUser)
me.url = -> '/auth/whoami'
me.fetch()
retry = -> me.fetch() # blindly try again
error = -> setTimeout(retry, 1000) # blindly try again
me.on 'error', error, @
2014-01-06 20:45:35 -05:00
me.on 'sync', ->
me.off 'error', error, @ if firstTime
2014-01-06 20:56:34 -05:00
me.url = -> "/db/user/#{me.id}"
2014-01-06 20:45:35 -05:00
trackFirstArrival() if firstTime
2014-01-03 13:32:13 -05:00
if me and not me.get('testGroupNumber')?
# Assign testGroupNumber to returning visitors; new ones in server/handlers/user
me.set 'testGroupNumber', Math.floor(Math.random() * 256)
me.save()
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, me.attributes)
2014-01-06 20:45:35 -05:00
me.loadGravatarProfile() if me.get('email')
Backbone.listenTo(me, 'sync', userSynced)
2014-01-03 13:32:13 -05:00
userSynced = (user) ->
Backbone.Mediator.publish('me:synced', {me:user})
2014-01-26 17:44:08 -05:00
storage.save(CURRENT_USER_KEY, user)
2014-01-03 13:32:13 -05:00
init()
onSetVolume = (e) ->
return if e.volume is me.get('volume')
me.set('volume', e.volume)
me.save()
Backbone.Mediator.subscribe('level-set-volume', onSetVolume, module.exports)
trackFirstArrival = ->
# will have to filter out users who log in with existing accounts separately
# but can at least not track logouts as first arrivals using local storage
2014-01-26 17:44:08 -05:00
beenHereBefore = storage.load(BEEN_HERE_BEFORE_KEY)
2014-01-03 13:32:13 -05:00
return if beenHereBefore
2014-01-06 20:45:35 -05:00
window.tracker?.trackEvent 'First Arrived'
2014-01-26 17:44:08 -05:00
storage.save(BEEN_HERE_BEFORE_KEY, true)