简体   繁体   English

更新后的Ember Simple Auth(Devise),验证中断

[英]Ember Simple Auth (Devise) after update, authenticate break

I did an update from 0.6.4 to 0.7.2 with Ember Simple Auth (not Ember CLI version) for devise, now my authentification doesn't work at all :(, do you have an idea ? thank you very much for your help :) 我进行了Ember Simple Auth(不是Ember CLI版本)从0.6.4到0.7.2的更新,现在我的身份验证根本不起作用:(,您有一个主意吗?非常感谢您的帮助:)

PS : apparently, ApplicationController (application_controller.rb) don't continue after authenticate_with_http_token do |token, options| PS:显然,authenticate_with_http_token做| token,options |之后,ApplicationController(application_controller.rb)不会继续。 and authenticate_with_http_token is empty (tested with puts) 并且authenticate_with_http_token为空(通过puts测试)

login_controller.js login_controller.js

App.LoginController = Ember.Controller.extend(SimpleAuth.LoginControllerMixin, {
  authenticator: 'simple-auth-authenticator:devise'
  //authenticator: 'authenticator:custom'
});

application.js.coffee application.js.coffee

Ember.Application.initializer
  name: "authentication"
  after: "simple-auth"
  initialize: (container, application) ->
    applicationRoute = container.lookup("route:application")
    session = container.lookup("simple-auth-session:main")
    # handle the session events
    session.on "sessionAuthenticationSucceeded", ->

      applicationRoute.transitionTo "Myspace"
      return

    return

window.ENV = window.ENV || {}
window.ENV["simple-auth"] = { store: 'simple-auth-session-store:local-storage', authorizer: "simple-auth-authorizer:devise" };
window.ENV['simple-auth-devise'] = {
    crossOriginWhitelist: ['*'], 
    serverTokenEndpoint: 'users/sign_in',
  };

login.hbs login.hbs

<br />
<div class="row">
    <div class="large-12 columns">
        <form {{action 'authenticate' on='submit'}}>
          <label for="identification">Login</label>
          {{input id='identification' placeholder='Enter Login' value=identification}}
          <label for="password">Password</label>
          {{input id='password' placeholder='Enter Password' type='password' value=password}}
          <button type="submit">Login</button>
        </form>
    </div>
</div>

login_route.js.coffee login_route.js.coffee

App.LoginRoute = Ember.Route.extend(

  #model: (params) ->
    #return @store.find('user', @get('session.user_id'))

  setupController: (controller, model) ->
    #controller.set "content", model
    controller.set "errorMessage", null
    return

  actions:
    sessionAuthenticationFailed: (responseBody) ->
      message = responseBody.error
      @controller.set "errorMessage", message
      console.log "errorMessage : " + message
      return )

myspace_route.js.coffee myspace_route.js.coffee

App.MyspaceRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin,  ....)

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 = {
          user_token: self.resource.authentication_token,
          user_email: self.resource.email
        }
        render json: data, status: 201
      end
    end
  end
end

application_controller.rb application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session,
      if: Proc.new { |c| c.request.format =~ %r{application/json} }

  before_filter :skip_trackable, :authenticate_user_from_token!

  private

    def skip_trackable
      request.env['warden'].request.env['devise.skip_trackable'] = '1'
    end

    def authenticate_user_from_token!
      puts "authentification"
      puts authenticate_with_http_token
      authenticate_with_http_token do |token, options|
        user_email = options[:user_email].presence
        user       = user_email && User.find_by_email(user_email)
        puts "user.authentication_token"
        puts user.authentication_token
        puts token
        puts "token"
        if user && Devise.secure_compare(user.authentication_token, token)
          sign_in user, store: false
        end
      end
    end
end

You're setting up the window.ENV object in the initializer that runs after the 'simple-auth' initializer so Ember Simple Auth cannot actually see the values that you set when its initializer runs. 您正在初始化程序中设置window.ENV对象,该对象在'simple-auth'初始化程序之后运行,因此Ember Simple Auth实际上无法查看其初始化程序运行时设置的值。 Make sure the values are set before the 'simple-auth' initializer runs. 确保在'simple-auth'初始化程序运行之前设置值。

Also you should switch to Ember CLI of course ;) 当然你也应该切换到Ember CLI;)

After a run of debugger, it goes to : 运行调试器后,转到:

ember-simple-auth.js 烬-简单auth.js

authenticate: function() {
    var args          = Array.prototype.slice.call(arguments);
    var authenticator = args.shift();
    Ember.assert('Session#authenticate requires the authenticator factory to be specified, was ' + authenticator, !Ember.isEmpty(authenticator));
    var _this            = this;
    var theAuthenticator = this.container.lookup(authenticator);
    Ember.assert('No authenticator for factory "' + authenticator + '" could be found', !Ember.isNone(theAuthenticator));
    return new Ember.RSVP.Promise(function(resolve, reject) {
      theAuthenticator.authenticate.apply(theAuthenticator, args).then(function(content) {
        _this.setup(authenticator, content, true);
        resolve(); // <- it goes to here
      }, function(error) {
        _this.clear();
        _this.trigger('sessionAuthenticationFailed', error);
        reject(error);
      });
    });
  },

The json response with token seems to be ok, and authenticator config seems to be ok also ... 带有令牌的json响应似乎还可以,并且Authenticator config似乎还可以...

Also i have a "Rejected" in this promise 我也有这个承诺的“拒绝”

ember-simple-auth.js 烬-简单auth.js

 restore: function() {
        var _this = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
          var restoredContent = _this.store.restore();
          var authenticator   = restoredContent.authenticator;
          if (!!authenticator) {
            delete restoredContent.authenticator;
            _this.container.lookup(authenticator).restore(restoredContent).then(function(content) {
              _this.setup(authenticator, content);
              resolve();
            }, function() {
              _this.store.clear();
              reject();
            });
          } else {
            _this.store.clear();
            reject();
          }
        });
      },

Trace of the Rejected promise : 拒绝承诺的痕迹:

VM7522:164 Ember Inspector (Promise Trace): 
    at new Promise (http://localhost:3000/assets/ember.js?body=1:10174:9)
    at __exports__.default.Ember.ObjectProxy.extend.restore (http://localhost:3000/assets/ember-simple-auth.js?body=1:1116:16)
    at __exports__.default (http://localhost:3000/assets/ember-simple-auth.js?body=1:1337:15)
    at __exports__.default.initialize (http://localhost:3000/assets/ember-simple-auth.js?body=1:447:9)
    at http://localhost:3000/assets/ember.js?body=1:43164:11
    at visit (http://localhost:3000/assets/ember.js?body=1:43556:7)
    at DAG.topsort (http://localhost:3000/assets/ember.js?body=1:43610:11)
    at Namespace.extend.runInitializers (http://localhost:3000/assets/ember.js?body=1:43161:15)
    at Namespace.extend._initialize (http://localhost:3000/assets/ember.js?body=1:43046:14)

Edit 1: and also this one : 编辑1:还有这个:

ember-simple-auth-devise.js 烬,简单的认证- devise.js

  restore: function(properties) {
    var _this            = this;
    var propertiesObject = Ember.Object.create(properties);
    return new Ember.RSVP.Promise(function(resolve, reject) {
      if (!Ember.isEmpty(propertiesObject.get(_this.tokenAttributeName)) && !Ember.isEmpty(propertiesObject.get(_this.identificationAttributeName))) {
        resolve(properties);
      } else {
        reject();
      }
    });
  },

with trace : 带有痕迹:

 Ember Inspector (Promise Trace): 
    at new Promise (http://localhost:3000/assets/ember.js?body=1:10174:9)
    at __exports__.default.Base.extend.restore (http://localhost:3000/assets/ember-simple-auth-devise.js?body=1:156:16)
    at apply (http://localhost:3000/assets/ember.js?body=1:7993:27)
    at superWrapper [as restore] (http://localhost:3000/assets/ember.js?body=1:7571:15)
    at http://localhost:3000/assets/ember-simple-auth.js?body=1:1121:51
    at invokeResolver (http://localhost:3000/assets/ember.js?body=1:10192:9)
    at new Promise (http://localhost:3000/assets/ember.js?body=1:10178:9)
    at __exports__.default.Ember.ObjectProxy.extend.restore (http://localhost:3000/assets/ember-simple-auth.js?body=1:1116:16)
    at __exports__.default (http://localhost:3000/assets/ember-simple-auth.js?body=1:1337:15)

With the help of marcoow, just modified https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise#server-side-setup SessionsController like this : 在marcoow的帮助下,像这样修改了https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise#server-side-setup SessionsController:

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 = {
          user_token: self.resource.authentication_token,
          user_email: self.resource.email
        }
        render json: @data.to_json, status: 201
      end
    end
  end
end

Now it's working 现在可以了

Edit : to_json explanation : http://apidock.com/rails/ActiveRecord/Serialization/to_json 编辑: to_json说明: http //apidock.com/rails/ActiveRecord/Serialization/to_json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM