简体   繁体   English

如果用户已登录,则将 root 路由到不同的 controller

[英]route root to different controller if user is logged in

We're using devise and are able to check if a logged in user is an admin in routes.rb so only admins can access example.com/sidekiq我们正在使用devise并且能够检查登录用户是否是routes.rb中的管理员,因此只有管理员才能访问example.com/sidekiq

authenticate :user, ->(user) { user.admin? } do
  mount Sidekiq::Web, at: '/sidekiq'
end

Is is possible to do something similar and point root to a different controller depending on whether a user is logged in or not like this pseudocode below?是否有可能做类似的事情并将 root 指向不同的 controller,具体取决于用户是否登录,如下面的伪代码?

if authenticate :user
  root "dashboard#main"
else
  root "pages#landing"
end
authenticated(:user) do
  root "dashboard#main"
end

unauthenticated(:user) do
  root "pages#landing"
end

Using unauthenticated here is really optional.在这里使用unauthenticated是可选的。 You could just define it as:您可以将其定义为:

authenticated(:user) do
  root "dashboard#main"
end
root "pages#landing"

And take advantage of the fact that routes have priority in the order they are defined.并利用路由按照它们定义的顺序具有优先级这一事实。 But it does let you swap the order if you want to.但如果你愿意,它确实可以让你交换订单。

The difference between authenticate and authenticated is that the former causes a authentication check and Warden will throw if the user is not authenticated so you get a redirect to the sign in page. authenticateauthenticated之间的区别在于,前者会导致身份验证检查,如果用户未通过身份验证,Warden 将抛出异常,以便您重定向到登录页面。 authenticated will just not run the block if the user is not signed in.如果用户未登录, authenticated将不会运行该块。

See:看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM