繁体   English   中英

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

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

我刚刚开始使用SocketStream。 (v0.1.0)我使用exports.actions.login函数创建了文件/app/server/auth.coffee。 我想在这个文件中访问@ session.setUserId,但是我很难弄清楚@session在哪里以及如何在/app/server/app.coffee之外访问它

这是我的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

有趣的是你提出了一个关于会话的问题,因为我在过去的几天里重写了很多代码作为SocketStream 0.2的一部分。

好消息是@session变量将返回0.2,因为我找到了一种将会话数据传递到后端的有效方法,而不必使用丑陋的@getSession回调。

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

exports.authenticate = true设置不适用于您的情况。

我很想知道如何/为什么要在/ app / server代码之外使用@session对象。

我将在几天内将所有最新的会话代码提交到github上的0.2预览分支。

希望有所帮助,

欧文

您只能使用@getCurrentSession方法在服务器端代码( app/server )中获取当前会话。

你还必须添加:

exports.authenticate = true

到那个文件。

暂无
暂无

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

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