简体   繁体   English

根据用户是否已登录而更改root用户,而无需在Ruby on Rails上进行Devise

[英]Change root depending on whether a user is logged in or not without Devise on Ruby on Rails

I want to use another root for my application when a user is already logged in but I don't know how to do it without Devise, we implemented all of the authentication's logic from scratch. 当用户已经登录时,我想为应用程序使用另一个根目录,但是我不知道没有Devise怎么做,我们从头开始实现了所有身份验证逻辑。 We're using Rails 4. 我们正在使用Rails 4。

In Devise there is a authenticated routes helper which lets you create routes which are only accessible to an authenticated user: 在Devise中,有一个经过authenticated路由帮助程序 ,它可以让您创建只有经过身份验证的用户才能访问的路由:

unauthenticated do
  root 'pages#home'
end

authenticated do
  root 'dashboard#index'
  resources :things
end

This is possible due to the fact that Warden actually does most of the heavy lifting when it comes to authentication and Warden lives on the rack layer. 由于Warden在身份验证方面实际上承担了大部分繁重的工作,而Warden却生活在机架层上,因此这是可能的。

Lets say you have a bare bones system on top of Warden. 可以说您在Warden上有一个裸露的骨头系统。 This is how you would create something similiar: 这是您创建类似对象的方式:

module ActionDispatch::Routing
  class Mapper
    def authenticated(&block)
      block.call if env['warden'].authenticated?  
    end

    def unauthenticated(&block)
      block.call if env['warden'].unauthenticated?  
    end
  end
end

You can replace env['warden'].unauthenticated? 您可以替换env['warden'].unauthenticated? with whatever you have in your home rolled authentication system - but note that it has to work as Rack middleware. 与您的家用身份验证系统中的任何内容兼容-但请注意,它必须作为Rack中间件工作。 If your authentication system is some junk shoved into ApplicationController or a helper its not going work as that is far too late in the request cycle. 如果您的身份验证系统有些垃圾扔到ApplicationController或助手中,那么它将无法正常工作,因为在请求周期中为时已晚。

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

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