覆盖Devise SessionsController#Create和create视图-身份验证失败 [英] Overriding Devise SessionsController#Create and create view - and authentication fails

查看:75
本文介绍了覆盖Devise SessionsController#Create和create视图-身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从devise登录视图改编的用户登录页面,并且我要覆盖控制器,因为我需要在登录时执行多个任务,但是我无法跳过第1步(管理员身份验证)。

I have a user login page which was adapted from the devise login view and I am overriding the controller because I need to perform several tasks on login, but I can't get past step 1 (warden authentication.

内部用户:: SessionController<设计:: SessionsController,我只是拥有

Inside Users::SessionController < Devise::SessionsController, I simply have

def create
   super
   [...other post authentication code]
end

调试到super.create我可以看到它调用

Debugging into super.create I can see that it calls

 self.resource = warden.authenticate!(auth_options) 

但是从调用super到永远都不会恢复,因为warden.authenticate!调用似乎劫持了

but it never retu8rns from the call to super. Somehow the warden.authenticate! call appears to hijack the create method ends up re-rendering the login page.

我看到auth_options参数仅包含一个哈希:

I see that the auth_options parameters contains only a hash:

 => {:scope=>:user, :recall=>"users/sessions#new"}

也许有些问题是的,正如我以为需要以某种方式传递电子邮件和密码一样,但是Warden还是有点神秘,不确定那些身份验证选项中应该包含什么。因此,我不太了解正在验证的内容,但是无论如何,看守对我来说都是个谜。

Maybe something is wrong there, as I would have thought the email and password would need to be passed somehow, but Warden is a bit of a mystery anyway, not really sure what should be in those auth options. So I don't really understand what is being authenticated, but the warden is a mystery to me anyway.

这是创建方法所使用的params集合。

Here is the params collection that's coming through to the create method.

 <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"EJ[...]mA==", 
"email"=>"joe@gmail.com", "password"=>"Qrxt5d3", "commit"=>"Log in", "controller"=>"users/sessions", "action"=>"create"} 
permitted: false>

我可以使用
user.valid_password来确认密码正确(( Qrxt5d3 )

I can confirm that the password is correct using user.valid_password?("Qrxt5d3")

我检查了gem提供的原始视图代码上的参数名称,似乎感兴趣的参数称为电子邮件和密码,因此

I checked the parameter names on the original view code supplied by the gem, and it seems like the params of interest are called "email" and "password" so not obvious to me what is going wrong here.

感谢您的任何帮助。

推荐答案

显然问题出在验证失败时,warden.authenticate!(auth_options)进行了某种立即重定向到new。

Apparently the issue is that warden.authenticate!(auth_options) does some sort of immediate redirect to new when authentication fails.

我的身份验证失败的原因情况是因为我的模型中有:confirmable,而用户名尚未得到确认。从用户模型中删除它,然后身份验证成功。

The reason authentication was failing in my case was because I had :confirmable in my model and the user name hadn't been confirmed yet. Removing that from the user model, then authentication succeeded.

(我本以为authenticate!可能会引发错误或返回false或出现故障,但是它只会重定向到:new。

(I would have thought that authenticate! might raise an error or return false or something on failure but instead it just redirects to :new.

这篇关于覆盖Devise SessionsController#Create和create视图-身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆