【问题标题】:warden authenticate recall not getting called典狱长验证召回没有被调用
【发布时间】:2013-12-21 20:09:15
【问题描述】:

我正在尝试使用 devise 进行 ajax 登录,但我似乎无法正常工作。问题是我在设计上启用了可确认的策略,warden.authenticate!应该在非活动用户上失败并呈现failure 操作,而不是呈现默认操作。我注意到许多教程都有相同的设置。我正在使用 rails 4.0.0 并设计 3.2.2。

class SessionsController < Devise::SessionsController
   def create
     #problem: failure not getting called
     resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure") 
     sign_in(resource_name, resource)
     render json: {success: true}
   end 

  def failure
     render json: {success: false, errors: ["Login failed!"]}
  end
end

devise.rb 设置如下,

config.http_authenticatable_on_xhr = false
config.navigational_formats = ['*/*', :'*/*', :html, :json]

routes.rb 有以下内容,

devise_for :users, controllers: {registrations: "registrations", sessions: "sessions"}

登录表单,

 <%= form_for(resource, as: resource_name, 
    url: session_path(resource_name), 
    format: :json,
    html:{id:"signin_form"},
    remote: true) do |f| %>
<%= f.label(:email, "Email") %> 
<%= f.text_field(:email, class: "field text") %> 
<%= f.label(:password, "Password") %> 
<%= f.text_field(:password, class: "field text", id: "user_signin_password") %>  

<%= f.submit "Sign in", class: "theme_color btn key" %>
<% end %>

由于failure 动作没有被调用,默认的new 动作呈现如下响应头,

Content-Type   text/html; charset=utf-8
Location       http://localhost:3000/users/sign_in.js

我也很好奇为什么它呈现.js而不是.json

谁能指出我错过了什么?谢谢

【问题讨论】:

    标签: ruby-on-rails ruby json devise ruby-on-rails-4


    【解决方案1】:

    在花了几个小时调试应用程序后,我发现 devise 的 activatable.rb 文件中有一个错误。当我按照选项进行操作时,发现设计的注册钩子after_set_user 不会与recall 选项一起抛出。

    这似乎只发生在尝试使用现有电子邮件/用户名登录的非活动帐户(启用了可确认策略,但帐户尚未确认)。但是,如果尝试使用不存在的电子邮件/用户名登录,则上述操作会作为 authenticate! throws :warden 与 recall 选项一起使用。

    我很高兴有人也发现了相同的错误并提供了补丁,但我很好奇他们为什么不与它合并。 可以找到解决办法 here!

    【讨论】:

    • 天哪!我将你的补丁添加到了我的 applications.rb 文件中,它就像一个魅力。
    • @SteveZelaznik 你能分享一下代码吗,你是如何将补丁添加到 application.rb 文件中的?面临类似问题
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    相关资源
    最近更新 更多