成员简单身份验证与设计会话在刷新时丢失



我在ember中使用了simplabs ember simple auth,并使用design设置了rails。当authenticate()被调用时,它会将电子邮件和密码发送到rails。其返回令牌。我可以在本地存储中看到令牌,如下所示,但只要我点击刷新,数据就会丢失,并返回登录页面。

{"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",
    }
}

在我的authenticate()函数中,我设置了design作为具有凭据的验证器。

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);
      });
    }
  }
});

在我的authenticators文件夹中,我定义了包含的devise.js

import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend();

在我的authorizers文件夹中,我定义了包含的devise.js

import DeviseAuthorizer from 'ember-simple-auth/authorizers/devise';
export default DeviseAuthorizer.extend();

我的配置/环境.js包含

ENV['simple-auth']={
    authorizer: 'simple-auth-authorizer:devise'
  };
  ENV['simple-auth-devise']={
    identificationAttributeName: 'email',
    resourceName:'user',
    tokenAttributeName: 'authentication_token'
  };

根据Ember Simple Auth:刷新时会话丢失指定标识AttributeName:"电子邮件"本应解决问题,但问题仍然存在。轨道侧

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

和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

路由被配置为使用会话控制器。我刚开始做emberjs,现在已经有好几天了。我不知道我在哪里错过了什么。

您不能在config/environment.js中配置设备验证器,而是在实际验证器类上配置属性,就像在Ember数据适配器上设置属性一样:

import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend({
  identificationAttributeName: 'email',
  resourceName:'user',
  tokenAttributeName: 'authentication_token'
});

这样,它将实际使用正确的属性,使restore方法在刷新时解析。

最新更新