Node.js and Backbone.marionette Initial View

Currently, i have a Node.js app serving my static /login & /index html pages with my server side template language (jade in this case). I would like to after successful authentication, return data about the current logged in user so backbone can begin rendering the content.

For example, i have a UserGroup document in Mongo that stores information about the User and which group they belong to (authorization info).

With Passport, i can authenticate my user and redirect them to the main page.

app.post('/login',
  passport.authenticate('local', { successRedirect: '/',
                                   failureRedirect: '/login',
                                   failureFlash: true })
);

After a redirect, how can i send data about the user that backbone will pick up in the initial rendering of the '/' backbone route?

Will this be res.send() even though it wasn't backbone who initiated the first request or using sessions with meta data about the user.

You need to have an open request from the client. Try looking into socket.io to maintain a connection between the page and the server. If thats too much, try just making an AJAX request with backbone and have the server 'hold onto' the request until the login is successful and then send the response when it is ready. It'll be a challenge, though, to match ajax requests to login requests.

I think the authenticated user will have a cookie associated with him. You could parse the cookie and construct a global user object upon app initialization. Or, you could emit the user details in json format in a script tag on the page and use that to build the currentUser object. Im not sure if this is the best way to do this; I'm looking into it myself. Would love to hear thoughts/feedback on this.