0

多くの電話モデルを持つhas_one クライアントモデルという典型的なユーザーモデル (ユーザー名、電子メール、パスワード フィールドを含む) があります。

私がやりたいことは、ユーザーが電子メールとパスワード、ユーザー名とパスワード、または電話番号とパスワードのいずれかの組み合わせでサインインできるようにすることです。

Deviseを使用してこれを達成するにはどうすればよいですか?

4

1 に答える 1

0

これを実現するために、Userモデルに以下を追加する必要がありました。

  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
于 2013-07-25T00:42:40.260 に答える