![](/img/trans.png)
[英]Ruby on Rails / Devise: Determining in model if user is logged in
[英]Change root depending on whether a user is logged in or not without Devise on Ruby on Rails
当用户已经登录时,我想为应用程序使用另一个根目录,但是我不知道没有Devise怎么做,我们从头开始实现了所有身份验证逻辑。 我们正在使用Rails 4。
在Devise中,有一个经过authenticated
路由帮助程序 ,它可以让您创建只有经过身份验证的用户才能访问的路由:
unauthenticated do
root 'pages#home'
end
authenticated do
root 'dashboard#index'
resources :things
end
由于Warden在身份验证方面实际上承担了大部分繁重的工作,而Warden却生活在机架层上,因此这是可能的。
可以说您在Warden上有一个裸露的骨头系统。 这是您创建类似对象的方式:
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
您可以替换env['warden'].unauthenticated?
与您的家用身份验证系统中的任何内容兼容-但请注意,它必须作为Rack中间件工作。 如果您的身份验证系统有些垃圾扔到ApplicationController
或助手中,那么它将无法正常工作,因为在请求周期中为时已晚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.