[英]How to store the user id in session with ember-simple-auth?
I want to store the user id in the session. 我想在会话中存储用户ID。 I have this authenticate method in a custom authenticator :
我在自定义身份验证器中有此身份验证方法:
authenticate: (credentials) ->
_this = this
new (Ember.RSVP.Promise) (resolve, reject) ->
Ember.$.ajax(
url: 'http://localhost:3000/api/v1/sessions'
type: 'POST'
data: session:
email: credentials.identification
password: credentials.password
).then ((response) ->
_this.set('user_id', response.user_id)
Ember.run ->
resolve token: response.token
), (xhr, status, error) ->
response = JSON.parse(xhr.responseText)
Ember.run ->
reject response.error
It's coffee script but it works like javascript. 它是咖啡脚本,但类似于javascript。 As you can this, I set 'user_id' in the session.
如您所愿,我在会话中设置了“ user_id”。
In app/initializers/custom-session
, I have this : 在
app/initializers/custom-session
,我有这个:
import Ember from "ember";
import Session from "simple-auth/session";
export default {
name: "current-user",
before: "simple-auth",
initialize: function(container) {
Session.reopen({
setCurrentUser: function() {
return this.get('user_id')
}.observes('secure.access_token')
});
}
};
user_id
always returns undefined
. user_id
始终返回undefined
。 I found many solutions on the Internet but nothing works. 我在Internet上找到了许多解决方案,但没有任何效果。
Is there a simple solution to do that? 有一个简单的解决方案吗?
You are setting the user_id
on the authenticator
( _this.set('user_id', response.user_id)
) instead of the session
. 您是在
authenticator
( _this.set('user_id', response.user_id)
)而不是session
上设置user_id
。 The user_id
should be passed to the resolve method in your authenticator
. 应该将
user_id
传递到authenticator
的resolve方法。 That way your user id is accessible by secure.user_id
in your session
. 这样,您的用户ID就可以在
session
通过secure.user_id
进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.