I have a multi-client CMS. Clients have many users and users belong to a client. Therefore the user model has username and client_id. The username is unique within the client_id scope.
I want to allow devise to sign the user in based on the username and the client_id, however, being that client_id is an incrementing integer, I would prefer the user to not need to know his client_id. Rather he should just be able to use the client name which is an attribute of the client model.
I know I can create a method client_handle like so:
class User
def client_handle
self.client.handle
end
end
is there a way I can configure devise to login using [:username, :client_handle] ?
Create a
client_namevirtual attribute in Users:Add
client_nameas an attr_accessor:attr_accessor :client_name
Add
client_nameto attr_accessible:attr_accessible :client_name
Tell Devise to use :client_name in the authentication_keys
Modify config/initializers/devise.rb to have:
Overwrite Devise’s find_for_database_authentication method in Users
For ActiveRecord:
And last step is add client_name field to views:
Make sure you have the Devise views in your project so that you can customize them
Rails 3:
Modify the views
sessions/new.html.erb:
*PS: Instead of your client_handle method use*