0

I'm just getting started with SocketStream. (v0.1.0) I created the file /app/server/auth.coffee with an exports.actions.login function. I'd like to access @session.setUserId in this file, but I'm have a hard time figuring out where @session lives and how to access it outside of /app/server/app.coffee

Here is my auth.coffee with comments where I'd like to access the session.

users = [
  username: 'craig'
  password: 'craig',
  username: 'joe'
  password: 'joe',
]

authenticate = (credentials, cb) ->
  user = _.detect users, (user) ->
    user.username == credentials.username and user.password == credentials.password
  authenticated = true if user?
  callback cb, authenticated

exports.actions = 
  login: (credentials, cb) ->
    authenticate credentials, (user) ->
      # here is where i'd like to set the userId like so:
      # @session.setUserId credentials.username
      callback cb user
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
Craig M
  • 5,598
  • 4
  • 32
  • 43
  • Leaky abstractions? Please elaborate...I'm still getting my brain wrapped around async programming, node, socketstream, coffeescript, etc.. I'm eager to pick up as many best practices along the way as I can. (This isn't a future production app, btw. It's just a sandbox for me to play around with SocketStream and get a feel for developing with the framework.) – Craig M Aug 18 '11 at 21:06
  • I'm implying `@session` magically lives somewhere. Some part of SocketStream is calling your functions with some `this` value that contains `@session`. God knows where though. – Raynos Aug 18 '11 at 21:11
  • Yeah, I've been going through the SocketStream source trying to figure out where it lives, but I'm coming up empty so far. I'll figure it out eventually if no one answers before then. – Craig M Aug 18 '11 at 21:20

2 Answers2

2

Interesting you bring a question about sessions up at the moment as I've been re-writing a lot of this code over the last few days as part of SocketStream 0.2.

The good news is the @session variable will be back in 0.2 as I have found an efficient way to pass the session data through to the back end without having to use the ugly @getSession callback.

To answer your question specifically, the @session variable is simply another property which is injected into the export.actions object before the request is processed. Hence you cannot have an action called 'session' (though the name of this 'magic variable' will be configurable in the next release of 0.2).

The exports.authenticate = true setting does not apply in your case.

I'm interested to know how/why you'd like to use the @session object outside of your /app/server code.

I will be committing all the latest session code to the 0.2 preview branch on github in a few days time.

Hope that helps,

Owen

  • Thanks, Owen. I was actually trying to use it in /app/server/auth.coffee. It was coming up as undefined. I reread the readme and followed the custom_auth example. This seems to be working (though my cookie is disappearing on page refresh. I'm still looking into why that is). Any idea when 0.2.0 is going to be released to npm? We're getting ready to spin up a node based project in the next week or so, and SS is our first choice for framework at this point. Also, I can't officially speak for the company, but if we do go with SS, we may be able to contribute to the project. – Craig M Aug 18 '11 at 23:34
  • The @session var should be present in any /app/server file. Just watch out if you're calling it inside another callback (as the this variable changes). In which case use => for your callback instead of ->. – socketstream Aug 18 '11 at 23:40
  • The next commit I make to 0.2 (in a few days) will be the last major change before 0.2 is released to npm, so I encourage you to use the 0.2 then. I want to spend another week or so afterwards doing some tidying up and looking into Node 0.5 / Connect Auth compatibility, then we're good to go. Any help is always appreciated :) – socketstream Aug 18 '11 at 23:46
  • Using => for the callback was the fix. Thanks again, Owen. IMO, SocketStream is following the right paradigm in terms of how to approach building (most) web apps with node. I'm impressed with the codebase already, and the corporate backing gives me confidence that development will continue. You'll definitely be seeing more of me even if my company doesn't choose the framework. SocketStream is perfect for a personal project that's been siting on the back burner for years. When I'm comfortable using the framework, I'll get back in touch to see how I can contribute to the project. – Craig M Aug 19 '11 at 06:01
0

You get the current session only within your server-side code (app/server) using the @getCurrentSession method.

Also you have to add:

exports.authenticate = true

to that file.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123