簡體   English   中英

Ruby on rails - 設計注冊空白密碼

[英]Ruby on rails - Devise registrations blank password

我想基於 ruby​​ on rails 構建一個網絡應用程序。 對於身份驗證,我正在使用設計gem。 一切都很好:我可以創建帳戶、登錄、注銷等。

但在這里我有一個問題。 我希望能夠在不提供密碼的情況下進行注冊,但仍然可以使用其他帳戶的密碼進行注冊。

我在 config/initializers/devise.rb 上將密碼長度設置為 0 到 128

config.password_length = 0..128

但是接下來的步驟是什么來做我想做的事?

謝謝

好的,我在這里回復作為答案。

感謝 Ammar Shah,我想出了如何獲取密碼用戶和沒有密碼的用戶。

首先在lib/devise/strategies (創建文件夾)中創建一個名為database_authenticable.rb的文件,代碼如下:

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    # Default strategy for signing in a user, based on their email and password in the database.
    class DatabaseAuthenticatable < Authenticatable
      def authenticate!
        if password.blank?
          authentication_hash[:encrypted_password] = ''
        end
        resource  = mapping.to.find_for_database_authentication(authentication_hash)
        hashed = false

        if validate(resource){ hashed = true; resource.valid_password?(password) }
          remember_me(resource)
          resource.after_database_authentication
          success!(resource)
        end

        mapping.to.new.password = password if !hashed && Devise.paranoid
        fail(:not_found_in_database) unless resource
      end
    end
  end
end

Warden::Strategies.add(:database_authenticatable, Devise::Strategies::DatabaseAuthenticatable)

然后在devise_create_user.rb遷移中添加:

t.string   :remember_token

最后在user.rb模型中:

before_create :remember_value

def valid_password?(password)
    if password.blank?
        true
    else
        super
    end
end

def password_required?
    new_record? ? false : super
end

def remember_value
    self.remember_token ||= Devise.friendly_token
end

感謝 Ammar Shah 幫助我!

將 config/initializers/devise.rb 中的密碼長度重置為默認值,並使用此答案使密碼可選。

此外,這里是設計維基中逐步參與功能的完整實現。 這取決於您究竟想要達到什么目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM