简体   繁体   English

用户未经过身份验证时重新路由自定义路由(Devise,rails gem)

[英]Rerouting custom routes when user is not authenticated (Devise, rails gem)

I want to reroute a non-authenticated user to my login page, lets say my route is http://localhost:3000/users/sign_in aka devise/sessions#new right now by default devise will edit page when not logged in since this is built in. But any custom routes will just break the app if you're not logged in since it is not apart of the devise reroutes. 我想将一个未经过身份验证的用户重新路由到我的登录页面,假设我的路由是http://localhost:3000/users/sign_in aka http://localhost:3000/users/sign_in devise/sessions#new http://localhost:3000/users/sign_in现在默认设置将在未登录时编辑页面,因为这内置。但是,如果您没有登录,任何自定义路线都会破坏应用程序,因为它不是设计重新路由的一部分。 How do I set these routes to reroutes if not logged in? 如果未登录,如何将这些路由设置为重新路由? Quoting directly from devise below, i'm still at a loss on how to implement this with my routes, what should the code look like? 直接从下面的设计引用,我仍然不知道如何用我的路线实现这个,代码应该是什么样的?

goes inside lib 进入lib

  class CustomFailure < Devise::FailureApp
    def redirect_url
       new_user_session_url(:subdomain => 'secure')
    end

    # You need to override respond to eliminate recall
    def respond
      if http_auth?
        http_auth
      else
        redirect
      end
    end
  end
And add the following in config/initializers/devise.rb:

  config.warden do |manager|
    manager.failure_app = CustomFailure
  end

goes inside initializers 进入初始化器

 config.warden do |manager|
    manager.failure_app = CustomFailure
  end

You can try something like this In your routes.rb 你可以在你的routes.rb中尝试这样的事情

authenticated :user do
  devise_scope :user do
    root to: "dashboard#index"
  end
end

unauthenticated do
  devise_scope :user do
    root to: "homepage#index"
  end
end

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

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