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:
Scott Erickson 2014-02-24 11:12:52 -08:00
parent 4532de9980
commit 2be752ceed
4 changed files with 42 additions and 35 deletions

View file

@ -1,6 +1,7 @@
app = require 'application' app = require 'application'
auth = require 'lib/auth'
$ -> init = ->
app.initialize() app.initialize()
Backbone.history.start({ pushState: true }) Backbone.history.start({ pushState: true })
handleNormalUrls() handleNormalUrls()
@ -9,6 +10,15 @@ $ ->
treemaExt.setup() treemaExt.setup()
filepicker.setKey('AvlkNoldcTOU4PvKi2Xm7z') 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 = -> 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) ->

View file

@ -5,6 +5,7 @@ UserHandler = require('../users/user_handler')
config = require '../../server_config' config = require '../../server_config'
errors = require '../commons/errors' errors = require '../commons/errors'
mail = require '../commons/mail' mail = require '../commons/mail'
languages = require '../routes/languages'
module.exports.setup = (app) -> module.exports.setup = (app) ->
authentication.serializeUser((user, done) -> done(null, user._id)) authentication.serializeUser((user, done) -> done(null, user._id))
@ -43,10 +44,36 @@ module.exports.setup = (app) ->
) )
app.get('/auth/whoami', (req, res) -> 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.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()

View file

@ -3,7 +3,6 @@ crypto = require 'crypto'
request = require 'request' request = require 'request'
User = require './User' User = require './User'
Handler = require '../commons/Handler' Handler = require '../commons/Handler'
languages = require '../routes/languages'
mongoose = require 'mongoose' mongoose = require 'mongoose'
config = require '../../server_config' config = require '../../server_config'
errors = require '../commons/errors' errors = require '../commons/errors'
@ -172,28 +171,3 @@ UserHandler = class UserHandler extends Handler
res.end() res.end()
module.exports = new UserHandler() 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
)
)

View file

@ -40,9 +40,6 @@ setupOneSecondDelayMiddlware = (app) ->
if(config.slow_down) if(config.slow_down)
app.use((req, res, next) -> setTimeout((-> next()), 1000)) app.use((req, res, next) -> setTimeout((-> next()), 1000))
setupUserMiddleware = (app) ->
user.setupMiddleware(app)
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly = (app) -> setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly = (app) ->
isOldBrowser = (req) -> isOldBrowser = (req) ->
# https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js # https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js
@ -66,7 +63,6 @@ exports.setupMiddleware = (app) ->
setupExpressMiddleware app setupExpressMiddleware app
setupPassportMiddleware app setupPassportMiddleware app
setupOneSecondDelayMiddlware app setupOneSecondDelayMiddlware app
setupUserMiddleware app
###Routing function implementations### ###Routing function implementations###