[英]ember-simple-auth with devise session lost on refresh
I used simplabs ember-simple-auth in ember and have rails setup using devise. 我在ember中使用了simplabs ember-simple-auth,并使用devise进行了rails设置。 when authenticate() gets called it sends the email and password to rails.
调用authenticate()时,它将电子邮件和密码发送到rails。 which returns the token.
返回令牌。 I can see token in localstorage as follows but as soon as I hit refresh the data is lost taking me back to login page.
我可以按如下所示在localstorage中看到令牌,但是一旦单击刷新 ,数据就会丢失,将我带回到登录页面。
{"secure":{
"authenticator":"authenticator:devise",
"id":1,"email":"abc@abc.com",
"created_at":"2015-12-21T06:25:31.000Z",
"updated_at":"2015-12-22T10:11:56.728Z",
"authentication_token":"UoUTuVmUfwsVUnHVkE4U",
}
}
in my authenticate() function I have setup devise as authenticator with credentials. 在我的authenticate()函数中,我设置了具有凭据的身份验证器。
export default Ember.Controller.extend({
_email: Ember.inject.controller('email'),
session: Ember.inject.service('session'),
actions: {
authenticate(){
let identification = this.get('_email.email');
let password = this.get('password');
console.log(identification, password);
this.get('session').authenticate("authenticator:devise",identification,password).catch((reason)=>{
this.set('errorMessage',reason.error|| reason);
});
}
}
});
inside my authenticators folder I have defined devise.js which contains 在我的Authenticators文件夹中,我定义了devise.js,其中包含
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend();
inside my authorizers folder I have defined devise.js which contains 在我的authorizers文件夹中,我定义了devise.js,其中包含
import DeviseAuthorizer from 'ember-simple-auth/authorizers/devise';
export default DeviseAuthorizer.extend();
my config/environment.js contains 我的config / environment.js包含
ENV['simple-auth']={
authorizer: 'simple-auth-authorizer:devise'
};
ENV['simple-auth-devise']={
identificationAttributeName: 'email',
resourceName:'user',
tokenAttributeName: 'authentication_token'
};
according to Ember Simple Auth: Session lost on refresh specifying identificationAttributeName: 'email' should have solved the problem but it still persists. 根据Ember Simple Auth:指定了identificationAttributeName:“ email”的会话在刷新时丢失,应该已经解决了该问题,但仍然存在。 Rails side
导轨侧
application_controller.rb application_controller.rb
class ApplicationController < ActionController::Base
respond_to :json
protect_from_forgery with: :null_session
before_filter :authenticate_user_from_token!
private
def authenticate_user_from_token!
authenticate_with_http_token do |token, options|
user_email = options[:email].presence
user = user_email && User.find_by_email(user_email)
if user && Devise.secure_compare(user.authentication_token, token)
sign_in user, store: false
end
end
end
end
and session_controller.rb : 和session_controller.rb:
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html { super }
format.json do
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
data = {
token: self.resource.authentication_token,
email: self.resource.email
}
render json: data, status: 201
end
end
end
end
routes are configured to use the session controller. 路由配置为使用会话控制器。 I just started emberjs and I am getting stuck on this for some days now.I dont know where I have missed somthing.
我刚开始使用emberjs,现在已经被卡住了好几天。我不知道我错过了什么。
You cannot configure the devise authenticator in config/environment.js
but instead configure properties on the actual authenticator class as you would set properties eg on an Ember Data adapter: 您不能在
config/environment.js
中config/environment.js
验证器,而是在实际的验证器类上配置属性,就像在Ember Data适配器上设置属性一样:
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend({
identificationAttributeName: 'email',
resourceName:'user',
tokenAttributeName: 'authentication_token'
});
That way it will actually use the correct properties making the restore
method resolve on refresh. 这样,它将实际使用正确的属性,使
restore
方法在刷新时解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.