diff --git a/app/initialize.coffee b/app/initialize.coffee index e076f2ec3..7670088db 100644 --- a/app/initialize.coffee +++ b/app/initialize.coffee @@ -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) -> diff --git a/server/routes/auth.coffee b/server/routes/auth.coffee index 5f050dd06..e5d3f9367 100644 --- a/server/routes/auth.coffee +++ b/server/routes/auth.coffee @@ -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() diff --git a/server/users/user_handler.coffee b/server/users/user_handler.coffee index af806a968..c755a7c33 100644 --- a/server/users/user_handler.coffee +++ b/server/users/user_handler.coffee @@ -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() \ No newline at end of file diff --git a/server_setup.coffee b/server_setup.coffee index d0afd259d..9f94da179 100644 --- a/server_setup.coffee +++ b/server_setup.coffee @@ -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###