Devise authenticating with additional field company_id

446 views Asked by At

I'd like to authenticate user with company_id beside email, password (Devise's default).

I just searched Devise wiki sign-in-using-their-username-or-email, and Scope-login-to-subdomain

I just find out about Devise, and I am not good to understand clearly. So, someone guide me how to add new field for authenticating. How to change the Devise in initializers, User model, etc

One more thing: could you explain the difference between config.authentication_keys and config.request_keys in initializers\devise.erb. Thanks

2

There are 2 answers

0
user4557573 On BEST ANSWER

I solve my problem by just adding this in the initilizers/devise.rb

config.authentication_keys = [ :email , :company_id ]
0
Anjan On

Yes, need like two models:

do in your models

class User < ActiveRecord::Base
  devise :database_authenticatable, authentication_keys: [:email]
end

class Company < ActiveRecord::Base
  devise :database_authenticatable, authentication_keys: [:email, :company_id]
end

How To: Allow users to sign in using their username or email address