SocketStream:在/server/app.coffee之外访问@session
我刚刚开始使用 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的是,你现在提出了一个关于会话的问题,因为我在过去几天里作为 SocketStream 0.2 的一部分重写了很多这样的代码。
好消息是 @session 变量将在 0.2 中回归,因为我找到了一种将会话数据传递到后端的有效方法,而无需使用丑陋的 @getSession 回调。
为了具体回答您的问题,@session变量只是在处理请求之前注入export.actions对象的另一个属性。因此,你不能有一个名为“会话”的操作(尽管这个“魔法变量”的名称将在下一个版本的 0.2 中是可配置的)。
Exports.authenticate = true 设置不适用于您的情况。
我有兴趣知道您如何/为什么想在 /app/server 代码之外使用 @session 对象。
几天后我将把所有最新的会话代码提交到 github 上的 0.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
您只能使用
@getCurrentSession
方法在服务器端代码 (app/server
) 中获取当前会话。您还必须添加:
到该文件。
You get the current session only within your server-side code (
app/server
) using the@getCurrentSession
method.Also you have to add:
to that file.