plataformatec/devise — Issue #1627: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address
Author: geekontheway
Created: 2012-02-08 11:25:30+00:00
State: open

I have Rails 3.0.10 running with activerecord and devise (2.0.0)

Following the wiki page

Allow users to sign in using username or email

when I use a correct username or email ,with correct password , I have the following error

`ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: login)`

>In Devise.rb

`config.authentication_keys = [ :login ]`

>The view : sessions/new.html.erb

``

>The model : admin.rb

```ruby
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :login, :password, :password_confirmation, :remember_me
attr_accessor :login

has_one :admin_profile
has_many :admins_group
has_many :groups, :through => :admins_group

has_many :admins_permission
has_many :permissions, :through => :admins_permission

validates :password, :confirmation => true
validates :password_confirmation, :presence => true

protected
def self.find_for_database_authentication(conditions)
login = conditions.delete(:login)
where(conditions).where({:username => login} | {:email => login}).first
end

def self.find_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }

attributes = attributes.slice(*required_attributes)
attributes.delete_if { |key, value| value.blank? }

if attributes.size == required_attributes.size
if attributes.has_key?(:login)
login = attributes.delete(:login)
record = find_record(login)
else
record = where(attributes).first
end
end

unless record
record = new

required_attributes.each do |key|
value = attributes[key]
record.send("#{key}=", value)
record.errors.add(key, value.present? ? error : :blank)
end
end
record
end

def self.find_record(login)
where({:username => login} | {:email => login}).first
end
end
```