[英]Rails: Devise sign in error message behaviour
Can anyone give any pointers to how we can customise Devise so that if an unauthenticated user visits the root_url, they are redirected to /users/sign_in but not shown the "You need to sign in or sign up before continuing."任何人都可以指出我们如何自定义设计,以便如果未经身份验证的用户访问 root_url,他们将被重定向到 /users/sign_in 但不会显示“您需要登录或注册才能继续”。 error message?
错误信息?
To expand further: we want that message to be shown if the user tries to visit a 'deeper' URL (/controller/restricted_action) directly without first authenticating, but for someone with the app bookmarked as www.app.com we just want to redirect them straight to the login page without any error being shown.进一步扩展:如果用户尝试直接访问“更深的”URL(/controller/restricted_action)而无需首先进行身份验证,我们希望显示该消息,但对于将应用程序标记为 www.app.com 的人,我们只想将它们直接重定向到登录页面,而不会显示任何错误。
Had a look around through the SessionsController (to ascertain whether a custom controller could work) and various other places, but can't see a place or how we can override the behaviour in this way.环顾了 SessionsController(以确定自定义控制器是否可以工作)和其他各种地方,但看不到地方或我们如何以这种方式覆盖行为。
Any advice appreciated!任何建议表示赞赏!
This should remove the flash alert message if redirected from "/" to "/users/sign_in".如果从“/”重定向到“/users/sign_in”,这应该删除闪光警报消息。
class ApplicationController < ActionController::Base
before_action :keep_previous_url
before_action :no_unauthenticated_alert_if_redirected_from_root
def keep_previous_url
session[:previous_url] = request.fullpath
end
def no_unauthenticated_alert_if_redirected_from_root
if request.fullpath == "/users/sign_in" && session[:previous_url] == "/"
flash.now.alert = nil
end
end
end
@Anthony's suggestion above is good. @Anthony 上面的建议很好。 Although I think we can improve on it slightly.
虽然我认为我们可以稍微改进一下。 With this below solution, there is only 1
before_action
being called in the ApplicationController
(the less the better since it's used by all), and then one in your root or home controller itself.使用以下解决方案,在
ApplicationController
只调用了 1 个before_action
(越少越好,因为它已被所有人使用),然后在您的根控制器或主控制器本身中调用一个。 Also, it won't keep storing the previous_url
unnecessarily in the user session, saving memory.此外,它不会在用户会话中不必要地存储
previous_url
,从而节省内存。
Add this to your ApplicationController
:将此添加到您的
ApplicationController
:
class ApplicationController < ActionController::Base
before_action :_no_unauthenticated_alert_if_redirected_from_root
private
def _no_unauthenticated_alert_if_redirected_from_root
return if session[:requiring_sign_in_from_root] != true
session[:requiring_sign_in_from_root] = nil
flash.clear
end
end
and add this to the controller that serves your root "/" url:并将其添加到为您的根 "/" url 提供服务的控制器中:
class HomeController < ApplicationController
prepend_before_action :_check_if_requiring_sign_in_from_root
private
def _check_if_requiring_sign_in_from_root
session[:requiring_sign_in_from_root] = true unless user_signed_in?
end
end
The methods don't need to be private, but I think it's good practice.这些方法不需要是私有的,但我认为这是一种很好的做法。
I have fixed this by doing the following:我通过执行以下操作解决了这个问题:
authenticated :user do
root to: 'home#index', as: :home
end
root to: redirect('/users/sign_in')
Now I don't the error message when going to the root route.现在我在去根路由时没有错误信息。
Source: https://github.com/heartcombo/devise/wiki/How-To:-Require-authentication-for-all-pages来源: https : //github.com/heartcombo/devise/wiki/How-To : -Require-authentication-for-all-pages
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.