mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-24 08:08:15 -05:00
Merge branch 'master' of https://github.com/codecombat/codecombat
This commit is contained in:
commit
6a51bd6837
8 changed files with 63 additions and 83 deletions
|
@ -35,6 +35,11 @@
|
||||||
<!--[if IE 9]> <script src="/javascripts/vendor_with_box2d.js"></script> <![endif]-->
|
<!--[if IE 9]> <script src="/javascripts/vendor_with_box2d.js"></script> <![endif]-->
|
||||||
<!--[if !IE]><!--> <script src="/javascripts/vendor.js"></script> <!--<![endif]-->
|
<!--[if !IE]><!--> <script src="/javascripts/vendor.js"></script> <!--<![endif]-->
|
||||||
<script src="/javascripts/app.js"></script> <!-- it's all Backbone! -->
|
<script src="/javascripts/app.js"></script> <!-- it's all Backbone! -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
window.userObject = "userObjectTag";
|
||||||
|
</script>
|
||||||
|
|
||||||
<script>require('initialize');</script>
|
<script>require('initialize');</script>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
app = require 'application'
|
app = require 'application'
|
||||||
auth = require 'lib/auth'
|
|
||||||
|
|
||||||
init = ->
|
init = ->
|
||||||
app.initialize()
|
app.initialize()
|
||||||
|
@ -10,15 +9,8 @@ init = ->
|
||||||
treemaExt.setup()
|
treemaExt.setup()
|
||||||
filepicker.setKey('AvlkNoldcTOU4PvKi2Xm7z')
|
filepicker.setKey('AvlkNoldcTOU4PvKi2Xm7z')
|
||||||
|
|
||||||
$ ->
|
$ -> init()
|
||||||
# Make sure we're "logged in" first.
|
|
||||||
if auth.me.id
|
|
||||||
init()
|
|
||||||
else
|
|
||||||
Backbone.Mediator.subscribeOnce 'me:synced', init
|
|
||||||
|
|
||||||
window.init = init
|
|
||||||
|
|
||||||
handleNormalUrls = ->
|
handleNormalUrls = ->
|
||||||
# http://artsy.github.com/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/
|
# http://artsy.github.com/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/
|
||||||
$(document).on "click", "a[href^='/']", (event) ->
|
$(document).on "click", "a[href^='/']", (event) ->
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
CocoClass = require 'lib/CocoClass'
|
CocoClass = require 'lib/CocoClass'
|
||||||
{me, CURRENT_USER_KEY} = require 'lib/auth'
|
{me} = require 'lib/auth'
|
||||||
{backboneFailure} = require 'lib/errors'
|
{backboneFailure} = require 'lib/errors'
|
||||||
storage = require 'lib/storage'
|
storage = require 'lib/storage'
|
||||||
|
|
||||||
|
@ -59,7 +59,6 @@ module.exports = FacebookHandler = class FacebookHandler extends CocoClass
|
||||||
error: backboneFailure,
|
error: backboneFailure,
|
||||||
url: "/db/user?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
|
url: "/db/user?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
|
||||||
success: (model) ->
|
success: (model) ->
|
||||||
storage.save(CURRENT_USER_KEY, model.attributes)
|
|
||||||
window.location.reload() if model.get('email') isnt oldEmail
|
window.location.reload() if model.get('email') isnt oldEmail
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
CocoClass = require 'lib/CocoClass'
|
CocoClass = require 'lib/CocoClass'
|
||||||
{me, CURRENT_USER_KEY} = require 'lib/auth'
|
{me} = require 'lib/auth'
|
||||||
{backboneFailure} = require 'lib/errors'
|
{backboneFailure} = require 'lib/errors'
|
||||||
storage = require 'lib/storage'
|
storage = require 'lib/storage'
|
||||||
GPLUS_TOKEN_KEY = 'gplusToken'
|
GPLUS_TOKEN_KEY = 'gplusToken'
|
||||||
|
@ -102,7 +102,6 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
|
||||||
error: backboneFailure,
|
error: backboneFailure,
|
||||||
url: "/db/user?gplusID=#{gplusID}&gplusAccessToken=#{@accessToken.access_token}"
|
url: "/db/user?gplusID=#{gplusID}&gplusAccessToken=#{@accessToken.access_token}"
|
||||||
success: (model) ->
|
success: (model) ->
|
||||||
storage.save(CURRENT_USER_KEY, model.attributes)
|
|
||||||
window.location.reload() if wasAnonymous and not model.get('anonymous')
|
window.location.reload() if wasAnonymous and not model.get('anonymous')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
{me} = require 'lib/auth'
|
|
||||||
|
|
||||||
gplusClientID = "800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com"
|
gplusClientID = "800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com"
|
||||||
|
|
||||||
go = (path) -> -> @routeDirectly path, arguments
|
go = (path) -> -> @routeDirectly path, arguments
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
{backboneFailure, genericFailure} = require 'lib/errors'
|
{backboneFailure, genericFailure} = require 'lib/errors'
|
||||||
User = require 'models/User'
|
User = require 'models/User'
|
||||||
storage = require 'lib/storage'
|
storage = require 'lib/storage'
|
||||||
|
|
||||||
module.exports.CURRENT_USER_KEY = CURRENT_USER_KEY = 'whoami'
|
|
||||||
BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
|
BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
|
||||||
|
|
||||||
|
init = ->
|
||||||
|
module.exports.me = window.me = new User(window.userObject) # inserted into main.html
|
||||||
|
trackFirstArrival()
|
||||||
|
if me and not me.get('testGroupNumber')?
|
||||||
|
# Assign testGroupNumber to returning visitors; new ones in server/routes/auth
|
||||||
|
me.set 'testGroupNumber', Math.floor(Math.random() * 256)
|
||||||
|
me.save()
|
||||||
|
|
||||||
|
me.loadGravatarProfile() if me.get('email')
|
||||||
|
Backbone.listenTo(me, 'sync', Backbone.Mediator.publish('me:synced', {me:me}))
|
||||||
|
|
||||||
module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) ->
|
module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) ->
|
||||||
user = new User(userObject)
|
user = new User(userObject)
|
||||||
user.save({}, {
|
user.save({}, {
|
||||||
error: failure,
|
error: failure,
|
||||||
success: (model) ->
|
success: -> if nextURL then window.location.href = nextURL else window.location.reload()
|
||||||
storage.save(CURRENT_USER_KEY, model)
|
|
||||||
if nextURL
|
|
||||||
window.location.href = nextURL
|
|
||||||
else
|
|
||||||
window.location.reload()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports.loginUser = (userObject, failure=genericFailure) ->
|
module.exports.loginUser = (userObject, failure=genericFailure) ->
|
||||||
|
@ -23,52 +27,15 @@ module.exports.loginUser = (userObject, failure=genericFailure) ->
|
||||||
username:userObject.email,
|
username:userObject.email,
|
||||||
password:userObject.password
|
password:userObject.password
|
||||||
},
|
},
|
||||||
(model) ->
|
(model) -> window.location.reload()
|
||||||
storage.save(CURRENT_USER_KEY, model)
|
|
||||||
window.location.reload()
|
|
||||||
)
|
)
|
||||||
jqxhr.fail(failure)
|
jqxhr.fail(failure)
|
||||||
|
|
||||||
module.exports.logoutUser = ->
|
module.exports.logoutUser = ->
|
||||||
FB?.logout?()
|
FB?.logout?()
|
||||||
res = $.post('/auth/logout', {}, ->
|
res = $.post('/auth/logout', {}, -> window.location.reload())
|
||||||
storage.save(CURRENT_USER_KEY, null)
|
|
||||||
window.location.reload()
|
|
||||||
)
|
|
||||||
res.fail(genericFailure)
|
res.fail(genericFailure)
|
||||||
|
|
||||||
init = ->
|
|
||||||
# Load the user from local storage, and refresh it from the server.
|
|
||||||
# Also refresh and cache the gravatar info.
|
|
||||||
|
|
||||||
storedUser = storage.load(CURRENT_USER_KEY)
|
|
||||||
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, @
|
|
||||||
me.on 'sync', ->
|
|
||||||
me.off 'error', error, @ if firstTime
|
|
||||||
me.url = -> "/db/user/#{me.id}"
|
|
||||||
trackFirstArrival() if firstTime
|
|
||||||
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()
|
|
||||||
storage.save(CURRENT_USER_KEY, me.attributes)
|
|
||||||
|
|
||||||
me.loadGravatarProfile() if me.get('email')
|
|
||||||
Backbone.listenTo(me, 'sync', userSynced)
|
|
||||||
|
|
||||||
userSynced = (user) ->
|
|
||||||
Backbone.Mediator.publish('me:synced', {me:user})
|
|
||||||
storage.save(CURRENT_USER_KEY, user)
|
|
||||||
|
|
||||||
init()
|
|
||||||
|
|
||||||
onSetVolume = (e) ->
|
onSetVolume = (e) ->
|
||||||
return if e.volume is me.get('volume')
|
return if e.volume is me.get('volume')
|
||||||
me.set('volume', e.volume)
|
me.set('volume', e.volume)
|
||||||
|
@ -83,3 +50,6 @@ trackFirstArrival = ->
|
||||||
return if beenHereBefore
|
return if beenHereBefore
|
||||||
window.tracker?.trackEvent 'First Arrived'
|
window.tracker?.trackEvent 'First Arrived'
|
||||||
storage.save(BEEN_HERE_BEFORE_KEY, true)
|
storage.save(BEEN_HERE_BEFORE_KEY, true)
|
||||||
|
|
||||||
|
init()
|
||||||
|
|
||||||
|
|
|
@ -71,9 +71,7 @@ module.exports.setup = (app) ->
|
||||||
if req.user
|
if req.user
|
||||||
sendSelf(req, res)
|
sendSelf(req, res)
|
||||||
else
|
else
|
||||||
user = new User({anonymous:true})
|
user = makeNewUser(req)
|
||||||
user.set 'testGroupNumber', Math.floor(Math.random() * 256) # also in app/lib/auth
|
|
||||||
user.set 'preferredLanguage', languages.languageCodeFromAcceptedLanguages req.acceptedLanguages
|
|
||||||
makeNext = (req, res) -> -> sendSelf(req, res)
|
makeNext = (req, res) -> -> sendSelf(req, res)
|
||||||
next = makeNext(req, res)
|
next = makeNext(req, res)
|
||||||
loginUser(req, res, user, false, next)
|
loginUser(req, res, user, false, next)
|
||||||
|
@ -84,21 +82,6 @@ module.exports.setup = (app) ->
|
||||||
res.send(UserHandler.formatEntity(req, req.user))
|
res.send(UserHandler.formatEntity(req, req.user))
|
||||||
res.end()
|
res.end()
|
||||||
|
|
||||||
loginUser = (req, res, user, send=true, next=null) ->
|
|
||||||
user.save((err) ->
|
|
||||||
if err
|
|
||||||
return @sendDatabaseError(res, err)
|
|
||||||
|
|
||||||
req.logIn(user, (err) ->
|
|
||||||
if err
|
|
||||||
return @sendDatabaseError(res, err)
|
|
||||||
|
|
||||||
if send
|
|
||||||
return @sendSuccess(res, user)
|
|
||||||
next() if next
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
app.post('/auth/logout', (req, res) ->
|
app.post('/auth/logout', (req, res) ->
|
||||||
req.logout()
|
req.logout()
|
||||||
res.end()
|
res.end()
|
||||||
|
@ -155,6 +138,26 @@ module.exports.setup = (app) ->
|
||||||
res.send "Unsubscribed #{req.query.email} from all CodeCombat emails. Sorry to see you go! <p><a href='/account/settings'>Account settings</a></p>"
|
res.send "Unsubscribed #{req.query.email} from all CodeCombat emails. Sorry to see you go! <p><a href='/account/settings'>Account settings</a></p>"
|
||||||
res.end()
|
res.end()
|
||||||
|
|
||||||
|
module.exports.loginUser = loginUser = (req, res, user, send=true, next=null) ->
|
||||||
|
user.save((err) ->
|
||||||
|
if err
|
||||||
|
return @sendDatabaseError(res, err)
|
||||||
|
|
||||||
|
req.logIn(user, (err) ->
|
||||||
|
if err
|
||||||
|
return @sendDatabaseError(res, err)
|
||||||
|
|
||||||
|
if send
|
||||||
|
return @sendSuccess(res, user)
|
||||||
|
next() if next
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
module.exports.makeNewUser = makeNewUser = (req) ->
|
||||||
|
user = new User({anonymous:true})
|
||||||
|
user.set 'testGroupNumber', Math.floor(Math.random() * 256) # also in app/lib/auth
|
||||||
|
user.set 'preferredLanguage', languages.languageCodeFromAcceptedLanguages req.acceptedLanguages
|
||||||
|
|
||||||
createMailOptions = (receiver, password) ->
|
createMailOptions = (receiver, password) ->
|
||||||
# TODO: use email templates here
|
# TODO: use email templates here
|
||||||
options =
|
options =
|
||||||
|
@ -163,4 +166,4 @@ createMailOptions = (receiver, password) ->
|
||||||
replyTo: config.mail.username
|
replyTo: config.mail.username
|
||||||
subject: "[CodeCombat] Password Reset"
|
subject: "[CodeCombat] Password Reset"
|
||||||
text: "You can log into your account with: #{password}"
|
text: "You can log into your account with: #{password}"
|
||||||
#
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ baseRoute = require './server/routes/base'
|
||||||
user = require './server/users/user_handler'
|
user = require './server/users/user_handler'
|
||||||
logging = require './server/commons/logging'
|
logging = require './server/commons/logging'
|
||||||
config = require './server_config'
|
config = require './server_config'
|
||||||
|
auth = require './server/routes/auth'
|
||||||
|
UserHandler = require('./server/users/user_handler')
|
||||||
|
|
||||||
###Middleware setup functions implementation###
|
###Middleware setup functions implementation###
|
||||||
# 2014-03-03: Try not using this and see if it's still a problem
|
# 2014-03-03: Try not using this and see if it's still a problem
|
||||||
|
@ -85,7 +87,19 @@ exports.setupMiddleware = (app) ->
|
||||||
|
|
||||||
setupFallbackRouteToIndex = (app) ->
|
setupFallbackRouteToIndex = (app) ->
|
||||||
app.all '*', (req, res) ->
|
app.all '*', (req, res) ->
|
||||||
res.sendfile path.join(__dirname, 'public', 'index.html')
|
if req.user
|
||||||
|
sendMain(req, res)
|
||||||
|
else
|
||||||
|
user = auth.makeNewUser(req)
|
||||||
|
makeNext = (req, res) -> -> sendMain(req, res)
|
||||||
|
next = makeNext(req, res)
|
||||||
|
auth.loginUser(req, res, user, false, next)
|
||||||
|
|
||||||
|
sendMain = (req, res) ->
|
||||||
|
fs.readFile path.join(__dirname, 'public', 'main.html'), 'utf8', (err,data) ->
|
||||||
|
# insert the user object directly into the html so the application can have it immediately
|
||||||
|
data = data.replace('"userObjectTag"', JSON.stringify(UserHandler.formatEntity(req, req.user)))
|
||||||
|
res.send data
|
||||||
|
|
||||||
setupFacebookCrossDomainCommunicationRoute = (app) ->
|
setupFacebookCrossDomainCommunicationRoute = (app) ->
|
||||||
app.get '/channel.html', (req, res) ->
|
app.get '/channel.html', (req, res) ->
|
||||||
|
|
Loading…
Reference in a new issue