Devise - 如果帐户未确认,则重定向到页面 [英] Devise - Redirected to page if account isn't confirmed
问题描述
如果用户的帐户尚未确认,我正在尝试重定向该用户。所以这涉及代码的两个部分:
I'm trying to redirect the user if their account hasn't been confirmed. So this involves two parts of the code:
- 在用户首次创建帐户后重定向用户
- 如果他们尝试在确认帐户之前尝试登录,请重定向
我需要第二个帮助。
第一个我可以通过在我的自定义注册控制器中放入 after_inactive_sign_up_path_for(资源)
来实现。我试图为SessionsController做同样的事情,但它没有工作。如果他们还没有确认帐户,我需要重写什么才能正确重定向用户?
The first I was able to do by putting in after_inactive_sign_up_path_for(resource)
in my custom RegistrationsController. I've tried to do the same for a SessionsController, but it didn't work. What do I need to over write in order to properly redirect the user if they haven't confirmed the account yet?
推荐答案
你可能需要创建一个自定义守门员策略,并检查该帐户是否需要确认。这些东西分类:
You may have to create a custom warden strategy and check if the account needs confirmation. Something of this sorts:
# config/initializers/my_strategy.rb
Warden::Strategies.add(:my_strategy) do
def valid?
true
end
def authenticate!
u = User.find_for_authentication(:email => params[:email])
if u.nil? || !u.valid_password?(params[:password])
fail(:invalid)
elsif !u.confirmed?
fail!("Account needs confirmation.")
redirect!("your_root_url")
end
else
success!(u)
end
end
#config/initializers/devise.rb
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :my_strategy
end
假设用户名和密码在请求中作为参数传递。您可以查看数据库可靠策略默认情况下,Devise如何处理登录身份验证的示例。
This assumes the username and password are passed in the request as params. You can look at the database_authenticable strategy for an example of how Devise deals with sign-in authentication by default.
这篇关于Devise - 如果帐户未确认,则重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!