【问题标题】:Rails Warden before_logout is called twiceRails Warden before_logout 被调用两次
【发布时间】:2013-07-15 17:05:25
【问题描述】:

当我尝试调用“user.my_method”时,我正在努力弄清楚为什么我对 Warden::Manager.before_logout 的调用会为 NilClass 抛出 NoMethodError。然后我在 before_logout 块中添加了一个 debug puts 调用,并发现它在每次注销时都被调用了两次——第一次是用户为 nil,然后紧接着是提供了用户对象。因此,我可以通过将调用更改为“user.my_method if user”来巧妙地绕过异常,但我仍然对不知道为什么 before_logout 被调用两次感到不舒服。有没有其他人看过这个?这可能是那些仅发生在开发中的环境异常之一吗?

Devise.setup do |config|
  Warden::Manager.after_authentication do |user,auth,opts|
    user.update_attributes(is_active: true)
  end
  Warden::Manager.before_logout do |user,auth,opts|
    user.update_attributes(is_active: false) if user
  end

【问题讨论】:

    标签: ruby-on-rails-3 devise warden


    【解决方案1】:

    这是旧的,但您可能有两个模型(在 Warden 的案例中是范围):一个用于 User,另一个用于 AdminUser。您可以只对所需的模型/范围使用 if 语句:

    Warden::Manager.before_logout do |user, auth, opts|
        if opts[:scope] == :user
           user.current_sign_in_at = nil
           user.save!
        end
    end
    

    【讨论】:

      【解决方案2】:

      通过在 config/initializers/devise.rb 中添加它来解决它。为您想在注销时执行的操作添加代码。

      Warden::Manager.before_logout do |user,auth,opts|
        # store what ever you want on logout
        # If not in initializer it will generate two records (strange)
      end
      

      【讨论】:

      • 但这就是我拥有它的地方,这就是它如此令人困惑的原因。这是配置文件的前几行:Devise.setup do |config| Warden::Manager.after_authentication do |user,auth,opts| user.update_attributes(is_active: true) end Warden::Manager.before_logout do |user,auth,opts| user.update_attributes(is_active: false) if user end
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2012-08-02
      • 2016-04-01
      • 2012-05-05
      相关资源
      最近更新 更多