Rails 设计不带过滤器的验证用户检查

Rails devise check for authenticate user without filter

controller 中不使用 before filter 我想检查 user 是否已通过身份验证,因为它强制对 user 进行身份验证。 我正在寻找一种方法如何在控制器中询问 user 是否已通过身份验证,并基于此决定从 controller.

发回的内容

当前代码:

class Api::TestsController < Api::ApiController
  before_filter :authenticate_user!
// the filter is force me to be an authenticated user to get access to this controller
end

我想要的:

class Api::TestsController < Api::ApiController
  def index
    if current_user
      // do something if the user is authenticated 
    else
      // do something if the user not authenticated
    end
  end
end

我该如何实现? 我想要的:

class Api::TestsController < Api::ApiController
  def index
    if current_user then
      // do something if the user is authenticated 
    else
      // do something if the user not authenticated
    end
  end
end

我该如何实现?

好吧,就用方法user_signed_in?吧。假设您的设计模型是 User.

class Api::TestsController < Api::ApiController
  def index
    if user_signed_in?
      # do something if the user is authenticated 
    else
      # do something if the user not authenticated
    end
  end
end