jct*_*ank 5 ruby json ruby-on-rails devise ruby-on-rails-4
我正在尝试使用设计进行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
Run Code Online (Sandbox Code Playgroud)
devise.rb设置如下,
config.http_authenticatable_on_xhr = false
config.navigational_formats = ['*/*', :'*/*', :html, :json]
Run Code Online (Sandbox Code Playgroud)
routes.rb具有以下内容,
devise_for :users, controllers: {registrations: "registrations", sessions: "sessions"}
Run Code Online (Sandbox Code Playgroud)
登录表格,
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
由于failure未调用new操作,因此使用响应头呈现默认操作,如下所示,
Content-Type text/html; charset=utf-8
Location http://localhost:3000/users/sign_in.js
Run Code Online (Sandbox Code Playgroud)
我也很好奇它为什么渲染.js而不是.json.
有人能指出我错过了什么吗?谢谢
花了几个小时调试应用程序后,我发现设计的activatable.rb文件中有一个错误。当我按照选项进行操作时,发现设备的注册钩子after_set_user不会随recall选项一起抛出。
这似乎只发生在尝试使用现有电子邮件/用户名登录的非活动帐户(已启用可确认策略,但帐户尚未确认)。但是,如果尝试使用不存在的电子邮件/用户名登录,则上述操作与authenticate!throws :wardenrecall选项一样有效。
我很高兴有人也发现了相同的错误并提供了补丁,但我很好奇他们为什么不与它合并。解决方案可以在这里找到 !