Rails 使用 ember-cli-simple-auth-devise 时总是返回 401

Rails always returning 401 when using ember-cli-simple-auth-devise

总体上对编程非常陌生,对 Rails 的经验很少,对 ember 的经验很少甚至没有。我现在可能对 embers "build ambitious web apps" 抱有太大的野心。我无法通过 Devise 设置正确获得身份验证。它总是在尝试通过 ember 进行身份验证时返回 401,并且出于某种原因,当我对 ember-simple-auth 进行一些必要的更改以通过 github 页面工作时,我的 rails "flash messages" 开始报错了。就像我通过浏览器访问 /users/sign_in 以获得 html 一样。在我展示了我认为相关的代码之后,更多的细节。如果我遗漏了什么,请告诉我 post 我还需要什么。

我正在使用以下设置:

我需要简单验证和简单验证设计,还是 ember-cli-simple-auth-devise encompass/contain ember- cli-simple-auth?

Ember代码

config/environment.js



    ENV['simple-auth'] = {
      authorizer: 'simple-auth-authorizer:devise',
    };

    ENV['simple-auth-devise'] = {
      crossOriginWhitelist: ['*'],
      //serverTokenEndpoint: 'http://localhost:3000/users/sign_in'
      //I have tried it both with and with out serverTokenEndpoint
    };

app/controllers/login.js



       import Ember from 'ember';
       import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';

        export default Ember.Controller.extend(LoginControllerMixin, {
          authenticator: 'simple-auth-authenticator:devise'
        });


app/routes/application.js

    import Ember from 'ember';
    import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

    export default Ember.Route.extend(ApplicationRouteMixin);

app/routes/protected.js



    import Ember from 'ember';
    import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';

    export default Ember.Route.extend(AuthenticatedRouteMixin);


app/templates/login.hbs



    
        Login
        {{input value=identification placeholder='Enter Login'}}
        Password
        {{input value=password placeholder='Enter Password' type='password'}}
        Login
    

Rails代码

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  #commented the following out to rule it out.
  #protect_from_forgery with: :null_session

  before_filter :authenticate_user_from_token!

  before_filter :authenticate_user!

  def authenticate_user_from_token!
    authenticate_with_http_token do |token, options|
      user_email = options[:user_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

app/controllers/sessions_controller.rb

class SessionsController < Devise::SessionsController
  respond_to :html, :json

  def create
    super do |user|
      if request.format.json?
        data = {
          token:      user.authentication_token,
          user_email: user.email
        }
        render json: data, status: 201 and return
      end
    end
  end
end

app/models/user.rb

class User < ActiveRecord::Base

  before_save :ensure_authentication_token
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def ensure_authentication_token
     if authentication_token.blank?
       self.authentication_token = generate_authentication_token
     end
   end

   private

     def generate_authentication_token
       loop do
         token = Devise.friendly_token
         break token unless User.where(authentication_token: token).first
       end
     end

end

config/routes.rb

  resources :specials

  #devise_for :users
  devise_for :users, controllers: { sessions: 'sessions' }

  root to: 'specials#index'

当我提交 ember 登录表单时,无论我提交什么,我都会收到回复:

Started POST "/users/sign_in" for 127.0.0.1 at 2015-03-04 19:50:57 -0500
Processing by SessionsController#create as JSON
  Parameters: {"user"=>{"password"=>"[FILTERED]", "user_email"=>"foo@bar.com"}}
Completed 401 Unauthorized in 7ms

如果我访问 http://localhost:3000/users/sign_in,我会收到以下信息:

/app/views/layouts/application.html.erb where line #11 raised:

undefined method `flash' for #<ActionDispatch::Request:0x007fec37dfe708>

app/views/layouts/application.html.erb

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>

在我根据 https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise

进行建议更改后出现此错误

这个问题已被 https://github.com/simplabs/ember-simple-auth/pull/456. Just update your app/controllers/application_controller.rb and your app/controllers/sessions_controller.rb according to the revised https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth-devise/README.md 修复。 还要加上

identificationAttributeName: 'email'

ENV['simple-auth-devise']config/environment.js。一旦拉取请求的更改被发布,最后添加的内容可以再次删除。