【问题标题】:OmniAuth and Devise, how to set optional passwordsOmniAuth 和 Devise,如何设置可选密码
【发布时间】:2011-06-30 17:55:21
【问题描述】:

我正在使用OmniAuthDevise 对用户进行身份验证。我希望使用 OmniAuth 提供程序注册的用户能够设置可选密码(API 身份验证所需),但我遇到了困难。

如果用户通过 OmniAuth 创建帐户并尝试设置密码,则会收到以下错误:

BCrypt::Errors::InvalidHash in RegistrationsController#update

我相信这是因为密码是空白的。有什么好办法解决这个问题?我考虑过生成一个随机密码,但这种方法的问题是用户需要知道当前密码才能编辑设置。

编辑: 我查看了allowing the user to change settings without requiring a current password,这就是我想做的只有如果用户最初没有密码。

【问题讨论】:

    标签: ruby-on-rails rubygems passwords devise omniauth


    【解决方案1】:

    我假设您不想要简单的方法,即如果他们想设置密码,只需重置密码?

    user.send_reset_password_instructions

    【讨论】:

    • 是的,我最初不想这样做,但我最终还是那样做。
    【解决方案2】:

    如果没有要验证的密码,另一种方法是将以下内容添加到您的“用户”模型类中以绕过密码验证,其中提供者是使用外部身份验证时设置的某个字段。

    def valid_password?(password)  
      !provider.nil? || super(password)  
    end
    

    【讨论】:

      【解决方案3】:

      另一种选择。您不必包含新字段。只需捕获引发的异常并返回 false。这是代码。

      def valid_password?(password)
         begin
            super(password)
         rescue BCrypt::Errors::InvalidHash
            return false
         end
      end
      

      这应该可以完成工作。

      【讨论】:

        【解决方案4】:

        这有点晚了,但它可能对其他人有所帮助,通过 Andrew 的回答,您可以创建一个密码并将其存储在数据库中,但您无法使用您的电子邮件和新密码登录,通过设置解决了这个问题:

          def valid_password
            !provider.nil? && !encrypted_password.present? || super
          end
        

        【讨论】:

          猜你喜欢
          • 2016-02-03
          • 1970-01-01
          • 2015-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-11
          • 2011-07-30
          相关资源
          最近更新 更多