繁体   English   中英

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

暂无
暂无

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

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