2013-07-19 81 views
0

自定義身份驗證我有一個典型的用戶模型(以用戶名,電子郵件地址和密碼字段)HAS_ONE客戶模型有許多手機型號。與軌道4國外模型設計

我想要做的是允許用戶使用電子郵件+密碼,用戶名+密碼或其中一個電話號碼+密碼的組合登錄。

如何使用Devise實現此目標?

回答

0

爲了實現這一點,我不得不添加下面我用戶型號:

attr_accessor :login 

    private 

    def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     match = where(conditions).where(
     ["lower(email) = :login OR lower(username) = :login", 
     { login: login.downcase }]).first 

     unless match 
     match = joins(client: :phones).where(conditions).where(
      ["lower(client_phones.number) = :phone", 
      { phone: PhoneFormatter.parse(login) }]).first 
     end 

     find(match.id) if match 
    else 
     where(conditions).first 
    end 
    end