简体   繁体   English

SocketStream:在/server/app.coffee之外访问@session

[英]SocketStream: Accessing @session outside of /server/app.coffee

I'm just getting started with SocketStream. 我刚刚开始使用SocketStream。 (v0.1.0) I created the file /app/server/auth.coffee with an exports.actions.login function. (v0.1.0)我使用exports.actions.login函数创建了文件/app/server/auth.coffee。 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 我想在这个文件中访问@ session.setUserId,但是我很难弄清楚@session在哪里以及如何在/app/server/app.coffee之外访问它

Here is my auth.coffee with comments where I'd like to access the session. 这是我的auth.coffee,其中包含我想要访问会话的评论。

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

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. 有趣的是你提出了一个关于会话的问题,因为我在过去的几天里重写了很多代码作为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. 好消息是@session变量将返回0.2,因为我找到了一种将会话数据传递到后端的有效方法,而不必使用丑陋的@getSession回调。

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. 要具体回答你的问题,@ session变量只是在处理请求之前注入到export.actions对象的另一个属性。 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). 因此,你不能有一个名为'session'的动作(虽然这个'魔术变量'的名称可以在0.2的下一个版本中配置)。

The exports.authenticate = true setting does not apply in your case. exports.authenticate = true设置不适用于您的情况。

I'm interested to know how/why you'd like to use the @session object outside of your /app/server code. 我很想知道如何/为什么要在/ app / server代码之外使用@session对象。

I will be committing all the latest session code to the 0.2 preview branch on github in a few days time. 我将在几天内将所有最新的会话代码提交到github上的0.2预览分支。

Hope that helps, 希望有所帮助,

Owen 欧文

You get the current session only within your server-side code ( app/server ) using the @getCurrentSession method. 您只能使用@getCurrentSession方法在服务器端代码( app/server )中获取当前会话。

Also you have to add: 你还必须添加:

exports.authenticate = true

to that file. 到那个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM