登录后重定向用户,只有在root_path上 [英] Redirect user after log in only if it's on root_path
问题描述
root_path
,即它是一个简单的门户网站主页,具有登录表单。 用户登录后,我希望它进入 dashboard_path
。
我已经这样做了:
def signed_in_root_path(scope_or_resource)
dashboard_path
end
这显然应该在用户登录时使用,我不想让它去 root_path
,同时仍然保持用户回到上一页,如果它试图击中限制区域,它是超时或未登录。
ie:
restricted_page - > login - > restricted_page_but_logged_in
我不想改变这种行为,这就是为什么我没有使用 after_sign_in_path
,但是如果要重定向它,如果它在 root_path
或任何不需要用户的路由验证。
我的问题是这不工作。登录后,我被重定向到 root_path
,我相信是因为 after_sign_in_path
之前触发。
有没有办法做到这一点?谢谢!
编辑:这个工作是我第二次登录,即我去 root_path
登录,获取Flash消息,告诉我我已经登录,并在 root_path
上的表单上再次输入用户名和密码。我成功地被重定向到 dashboard_path
。仍然不是我想要的行为。
只是一个想法
您可以为签名的url定义两个root url,这将指向仪表板,第二个用于指向登录页面的用户的非签名页面
定义不同的根URL基于一些约束
在routes.rb
登录用户的根URL
约束(AuthenticatedUser)do
root:to => dashboard
end
未登录用户的根网址
root:to =>users / signin
然后在lib / authenticated_user.rb中创建类AuthenticatedUser
class AuthenticatedUser
def self .matches?(request)
user_signed_in?
end
end
现在如果用户登录root_url将指向仪表板否则它将指向登录页面
您还可以创建两个根(尚未测试)
root:to => dashboard,:constraints => {user_signed_in?}
root:to => users / signin
更多限制
http://edgeguides.rubyonrails.org/routing.html#request-based-constraints
注意
URL的优先级基于创建顺序,
首先创建 - >最高优先级资源
I have a root_path
on my Rails application that is not user-protected i.e. it's a simple portal homepage, with a login form.
After the users log in, I'd like it to go to dashboard_path
.
I've done this:
def signed_in_root_path(scope_or_resource)
dashboard_path
end
This apparently should be used when an user signs in, and I don't want it to go to the root_path
, while still keeping the user going back to a previous page if it tries to hit a restricted area and it's either timed out or not logged in.
i.e.:
restricted_page -> login -> restricted_page_but_logged_in
I don't want to change this behavior, and that's why I haven't used after_sign_in_path
, but want to redirect it if it's on root_path
, or any route that doesn't require user authentication.
My problem is that this is not working. After signing in, I'm getting redirected back to root_path
, which I believe is because of after_sign_in_path
getting triggered before.
Is there any way to do this? Thanks!
Edit: This works the second time I log in, i.e. I go to root_path
, log in, gets the flash message stating me that I'm logged in, and enter username and password again on the form on root_path
. I successfully get redirected to dashboard_path
. Still, not quite the behavior I want.
Just a thought
You can define two root url one for signed in url which will point to dashboard and second for non signed in users which will point to login page
define different root url based on some constraints in routes.rb
root url for signed in users
constraints(AuthenticatedUser) do
root :to => "dashboard"
end
root url for non signed in users
root :to=>"users/signin"
then create class AuthenticatedUser in lib/authenticated_user.rb
class AuthenticatedUser
def self.matches?(request)
user_signed_in?
end
end
now if user is signed in root_url will point to dashboard else it will point to signin page
Your can also create two roots using(did not tested it yet)
root :to => "dashboard", :constraints => {user_signed_in?}
root :to => "users/signin"
more on constrains http://edgeguides.rubyonrails.org/routing.html#request-based-constraints
Note
The priority of url is based upon order of creation, first created -> highest priority resources
这篇关于登录后重定向用户,只有在root_path上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!