ember-simple-auth,修改会话在刷新

时间:2015-12-22 10:48:38

标签: ruby-on-rails ember.js devise ember-cli ember-simple-auth

我在ember中使用了simplabs ember-simple-auth并使用devise设置了rails。当authenticate()被调用时,它会将电子邮件和密码发送给rails。返回令牌。我可以在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",
    }
}

在我的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);
      });
    }
  }
});

在我的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();

我的config / environment.js包含

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

根据Ember Simple Auth: Session lost on refresh指定identifyAttributeName:'email'应该已经解决了问题,但它仍然存在。 Rails方

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,现在我已经陷入困境了几天。我不知道我错过了什么。

1 个答案:

答案 0 :(得分:0)

您无法在config/environment.js中配置设计验证程序,而是在实际验证程序类上配置属性,就像设置属性一样。在Ember数据适配器上:

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

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