mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 15:48:11 -05:00
Refactored user creation to /auth/whoami, and made the app call that first, so only one user is created. Fixed #318. Think this also fixes #406.
This commit is contained in:
parent
4532de9980
commit
2be752ceed
4 changed files with 42 additions and 35 deletions
|
@ -1,14 +1,24 @@
|
|||
app = require 'application'
|
||||
auth = require 'lib/auth'
|
||||
|
||||
$ ->
|
||||
init = ->
|
||||
app.initialize()
|
||||
Backbone.history.start({ pushState: true })
|
||||
handleNormalUrls()
|
||||
|
||||
|
||||
treemaExt = require 'treema-ext'
|
||||
treemaExt.setup()
|
||||
filepicker.setKey('AvlkNoldcTOU4PvKi2Xm7z')
|
||||
|
||||
$ ->
|
||||
# Make sure we're "logged in" first.
|
||||
if auth.me.id
|
||||
init()
|
||||
else
|
||||
Backbone.Mediator.subscribeOnce 'me:synced', init
|
||||
|
||||
window.init = init
|
||||
|
||||
handleNormalUrls = ->
|
||||
# http://artsy.github.com/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/
|
||||
$(document).on "click", "a[href^='/']", (event) ->
|
||||
|
|
|
@ -5,6 +5,7 @@ UserHandler = require('../users/user_handler')
|
|||
config = require '../../server_config'
|
||||
errors = require '../commons/errors'
|
||||
mail = require '../commons/mail'
|
||||
languages = require '../routes/languages'
|
||||
|
||||
module.exports.setup = (app) ->
|
||||
authentication.serializeUser((user, done) -> done(null, user._id))
|
||||
|
@ -43,10 +44,36 @@ module.exports.setup = (app) ->
|
|||
)
|
||||
|
||||
app.get('/auth/whoami', (req, res) ->
|
||||
res.setHeader('Content-Type', 'text/json');
|
||||
if req.user
|
||||
sendSelf(req, res)
|
||||
else
|
||||
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
|
||||
makeNext = (req, res) -> -> sendSelf(req, res)
|
||||
next = makeNext(req, res)
|
||||
loginUser(req, res, user, false, next)
|
||||
)
|
||||
|
||||
sendSelf = (req, res) ->
|
||||
res.setHeader('Content-Type', 'text/json')
|
||||
res.send(UserHandler.formatEntity(req, req.user))
|
||||
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) ->
|
||||
req.logout()
|
||||
|
|
|
@ -3,7 +3,6 @@ crypto = require 'crypto'
|
|||
request = require 'request'
|
||||
User = require './User'
|
||||
Handler = require '../commons/Handler'
|
||||
languages = require '../routes/languages'
|
||||
mongoose = require 'mongoose'
|
||||
config = require '../../server_config'
|
||||
errors = require '../commons/errors'
|
||||
|
@ -171,29 +170,4 @@ UserHandler = class UserHandler extends Handler
|
|||
res.redirect(document?.get('photoURL') or '/images/generic-wizard-icon.png')
|
||||
res.end()
|
||||
|
||||
module.exports = new UserHandler()
|
||||
|
||||
module.exports.setupMiddleware = (app) ->
|
||||
app.use (req, res, next) ->
|
||||
if req.user
|
||||
next()
|
||||
else
|
||||
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
|
||||
loginUser(req, res, user, false, next)
|
||||
|
||||
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 = new UserHandler()
|
|
@ -40,9 +40,6 @@ setupOneSecondDelayMiddlware = (app) ->
|
|||
if(config.slow_down)
|
||||
app.use((req, res, next) -> setTimeout((-> next()), 1000))
|
||||
|
||||
setupUserMiddleware = (app) ->
|
||||
user.setupMiddleware(app)
|
||||
|
||||
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly = (app) ->
|
||||
isOldBrowser = (req) ->
|
||||
# https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js
|
||||
|
@ -66,7 +63,6 @@ exports.setupMiddleware = (app) ->
|
|||
setupExpressMiddleware app
|
||||
setupPassportMiddleware app
|
||||
setupOneSecondDelayMiddlware app
|
||||
setupUserMiddleware app
|
||||
|
||||
###Routing function implementations###
|
||||
|
||||
|
|
Loading…
Reference in a new issue