LoginSignup
27
29

More than 5 years have passed since last update.

Deviseでメールアドレス以外のカラムをログイン認証に利用する

Last updated at Posted at 2013-04-28

例えばアカウント名(accountカラム)で認証を行いたい場合はuserモデルに以下を追加する。

models/user.rb
devise :authentication_keys => [ :account ]

さらにアカウント名、もしくはメールアドレスのどちらかで認証したい場合は入力値を受け取るための適当なattributeを追加し、self.find_first_by_auth_conditionsをオーバーライドすることで実現できる。

models/user.rb
devise :authentication_keys => [ :login ]

attr_accessor :login

def self.find_first_by_auth_conditions(warden_conditions)
  conditions = warden_conditions.dup
  if login = conditions.delete(:login)
    where(conditions).where(["account = :value OR email = :value", { :value => login }]).first
  else
    where(conditions).first
  end
end
27
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
29