Ruby-on-rails 如果用户未通过 Devise 进行身份验证,则重定向到登录页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23555618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:37:31  来源:igfitidea点击:

Redirect to log in page if user is not authenticated with Devise

ruby-on-railsruby-on-rails-4devisewarden

提问by Gdeglin

I'm using Devise with Ruby on Rails.

我正在将 Devise 与 Ruby on Rails 一起使用。

What is the recommended way to redirect unauthenticated users to the sessions#new page if they attempt to access a page that requires authentication?

如果未经身份验证的用户尝试访问需要身份验证的页面,推荐将其重定向到 session#new 页面的方法是什么?

Right now I get an error that says no route matches the one they attempt to access (leading to a 404 error in production).

现在我收到一个错误,说没有路由与他们尝试访问的路由匹配(导致生产中出现 404 错误)。

回答by GeekToL

Just simple add this method to application_controller.rb

只需简单地将此方法添加到 application_controller.rb

  protected
  def authenticate_user!
    if user_signed_in?
      super
    else
      redirect_to login_path, :notice => 'if you want to add a notice'
      ## if you want render 404 page
      ## render :file => File.join(Rails.root, 'public/404'), :formats => [:html], :status => 404, :layout => false
    end
  end

And you can call this method on before_filteranother controllers you want.

你可以在before_filter你想要的另一个控制器上调用这个方法。

e.g :

例如:

class HomesController < ApplicationController
  before_filter :authenticate_user!
  ## if you want spesific action for require authentication
  ## before_filter :authenticate_user!, :only => [:action1, :action2]
end

Don't forget add login_pathinto routes.rb

不要忘记添加login_pathroutes.rb

devise_scope :user do
  match '/sign-in' => "devise/sessions#new", :as => :login
end

note : I always use this way when play with devise for my apps authentication.. (rails 3.2 and rails 4.0.1)

注意:我在使用设计进行应用程序身份验证时总是使用这种方式..(rails 3.2 和 rails 4.0.1)

回答by leandrotk

You can do just like GeekTol wrote, or just put

你可以像 GeekTol 写的那样做,或者只是把

before_action :authenticate_user!

in your controller.

在您的控制器中。

In this case, devise uses the default authenticate_user! method, that will redirect to the "user_session_path" and use the default flash message.

在这种情况下,设计使用默认的authenticate_user!方法,这将重定向到“user_session_path”并使用默认的 flash 消息。

It's not necessary to rewrite authenticate_user! method, unless you want to customize it.

没有必要重写authenticate_user!方法,除非您想自定义它。

回答by comphelp

I thought you could just add: before_action :authenticate_user! to each controller that required the user to be logged in.

我以为你可以添加:before_action :authenticate_user!到每个需要用户登录的控制器。

I'm a Rails beginner but I found this in my own searches and it works well in my application.

我是 Rails 初学者,但我在自己的搜索中发现了这一点,并且在我的应用程序中运行良好。

回答by Lucas Polonio

You should refer to Devise's own How To: How To: Redirect to a specific page when the user can not be authenticated.

您应该参考 Devise 自己的 How To: How To: Redirect to a specific page when the user can't beauthenticated

Another alternative I can think of is creating a routing Constraint wrapping your protected routes. You'd better stick to Devise's way, but here is an example:

我能想到的另一种选择是创建一个路由约束来包装受保护的路由。你最好坚持 Devise 的方式,但这里有一个例子:

#On your routes.rb
constraints(Constraints::LoginRequired) do
  get '/example' 
end

#Somewhere like lib/constraints/login_required.rb
module Constraints
  class LoginRequired
    def self.matches?(request)
      #some devise code that checks if the user is logged in
    end 
  end
end

回答by Antony Mithun

Add this code in your config/routes.rb devise_for :usersand resources :usersand you can generate devise in views.

在你的config / routes.rb中添加该代码devise_for :users,并resources :users与您可以生成视图色器件。