【问题标题】:How to detect that user is going to be authenticated? (before authenticate)如何检测该用户将被认证? (认证前)
【发布时间】:2012-10-08 14:50:21
【问题描述】:

我需要在 Devise 将用户重定向到登录页面之前触发回调,可能是因为authenticate_user! 方法检测到他没有登录。类似:

before_filter :authenticate_user!, :only => :edit

def not_authenticated_callback
  # do something
end

如果没有调用authenticate_user!,则不应调用它。

【问题讨论】:

    标签: ruby-on-rails devise warden


    【解决方案1】:

    我找到了一个丑陋的解决方案:

    around_filter :intersect_warden
    
    def intersect_warden
      success = false
      result = catch(:warden) do
        result = yield
        success = true
        result
      end
    
      unless success
        not_authenticated_callback
        throw(:warden, result)
      end
    end
    

    【讨论】:

      【解决方案2】:

      使用 before_filter: authenticate_user! 时,如果用户未登录,该操作将不会进入您的控制器。

      如果你使用 before_filter, 当用户未登录时,您无法访问控制器内的任何功能。

      【讨论】:

      • before_filter 在控制器上下文中执行。
      • 我认为它调用了一个在 lib/devise/controllers/helpers.rb 中定义的辅助方法,这里是“authenticate#{resource_name}!”被评估为“authenticate_user!”
      • 不确定如何检查用户是否经过身份验证。你可以为authenticate_user添加一个after_filter!您可以在哪里检查用户是否已登录。
      • 如果用户未通过身份验证则不执行过滤后
      猜你喜欢
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多