リモートで働くプログラマーの検索結果

リモ太がググったことの覚書

複数モデルでdeviseを利用するときに特定のモデルだけauthentication_keysを変更したい

ログインに利用するキーを変更するには通常 config/initializers/devise.rbの中の

  # ==> Configuration for any authentication mechanism
  # Configure which keys are used when authenticating a user. The default is
  # just :email. You can configure it to use [:username, :subdomain], so for
  # authenticating a user, both parameters are required. Remember that those
  # parameters are used only when authenticating and not when retrieving from
  # session. If you need permissions, you should implement that in a before filter.
  # You can also supply a hash where the value is a boolean determining whether
  # or not authentication should be aborted when the value is not present.
  # config.authentication_keys = [:email]

ここの config.authentication_keys = [:email]を変更すればよいが、ここを変更すると全てのモデルに反映されてしまう

特定のモデルだけの場合は以下のようにdeviseのoptionとして指定すればよい

class Customer < ApplicationRecord
  devise :database_authenticatable, authentication_keys: [:organization_id, :email]

  # 企業IDとメールアドレスでログインをする
  def self.find_for_database_authentication(warden_conditions)
    email = warden_conditions[:email].to_s.downcase.strip
    find_by(email: email, organization_id: warden_conditions[:organization_id])
  end
end