Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Devise configuration to allow username or email address for sign_in, confirmation, unlock, forgot password instructions.
################################################################
# For views/migrations etc. check http://tinyurl.com/3xfx3zm #
################################################################
# File : RAILS_APP/config/initializers/devise.rb
# Change the following only. Rest can stay same
# NOTE : You must use devise master or any version released after Mar 13, 2011 to get everything mentioned here working.
config.authentication_keys = [ :login ]
config.confirmation_keys = [ :login ]
config.unlock_keys = [ :login ]
###############################################################
# IMPORTANT: You need meta_where gem for where clause to work #
# Tinker accordingly if you don't want to use meta_where #
###############################################################
# File : RAILS_APP/app/models/user.rb
# Your user model. Select appropriate model depending on your devise config
# To facilitate username or email login
attr_accessor :login
# Setup accessible (or protected) attributes for your model
# NOTE the :login here.
attr_accessible :login, :username, :email, :password, :password_confirmation, :remember_me
# Essential functions
protected
def self.find_for_database_authentication(conditions)
login = conditions.delete(:login)
u = self.arel_table
where(conditions).where(u[:username].eq(login).or(u[:email].eq(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)
u = self.arel_table
where(u[:username].eq(login).or(u[:email].eq(login))).first
end
@geekontheway

This comment has been minimized.

Show comment Hide comment
@geekontheway

geekontheway Feb 8, 2012

Started POST "/admins/sign_in" for 127.0.0.1 at 2012-02-08 19:06:43 +0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"djp14XxKzmx/nNMMRsd/NAYJCdu+dAlyjJgNTfCeDAQ=", "admin"=>{"login"=>"geekontheway", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"djp14XxKzmx/nNMMRsd/NAYJCdu+dAlyjJgNTfCeDAQ=", "admin"=>{"login"=>"geekontheway", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 121ms

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

Started POST "/admins/sign_in" for 127.0.0.1 at 2012-02-08 19:06:43 +0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"djp14XxKzmx/nNMMRsd/NAYJCdu+dAlyjJgNTfCeDAQ=", "admin"=>{"login"=>"geekontheway", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"djp14XxKzmx/nNMMRsd/NAYJCdu+dAlyjJgNTfCeDAQ=", "admin"=>{"login"=>"geekontheway", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 121ms

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

@geekontheway

This comment has been minimized.

Show comment Hide comment
@geekontheway

geekontheway Feb 8, 2012

what to config In the view? thanks

what to config In the view? thanks

@kulbirsaini

This comment has been minimized.

Show comment Hide comment
@kulbirsaini

kulbirsaini Feb 10, 2012

In your User (or corresponding) model, check that database field login is in the list of accessible attributes.
Example:

In file app/model/user.rb
attr_accessible :login, :password, :blah, :foo, :bar

Owner

kulbirsaini commented Feb 10, 2012

In your User (or corresponding) model, check that database field login is in the list of accessible attributes.
Example:

In file app/model/user.rb
attr_accessible :login, :password, :blah, :foo, :bar

@geekontheway

This comment has been minimized.

Show comment Hide comment
@geekontheway

geekontheway Feb 10, 2012

Thanks!

Thanks!

@paatsinsuwan

This comment has been minimized.

Show comment Hide comment
@paatsinsuwan

paatsinsuwan Jul 22, 2012

line 22

where(conditions).where({:username => login} | { :email => login}).first

should be

where(conditions).where({:username => login} || { :email => login}).first

and you're the best

line 22

where(conditions).where({:username => login} | { :email => login}).first

should be

where(conditions).where({:username => login} || { :email => login}).first

and you're the best

@kulbirsaini

This comment has been minimized.

Show comment Hide comment
@kulbirsaini

kulbirsaini Jul 24, 2012

@paatsinsuwan I was actually using meta_where ruby gem and that syntax worked. The syntax you suggested will not work. Just check the generated sql for your query

User.where(conditions).where({:username => login} || { :email => login}).to_sql

I have made the modification so that things work properly without meta_where.

Owner

kulbirsaini commented Jul 24, 2012

@paatsinsuwan I was actually using meta_where ruby gem and that syntax worked. The syntax you suggested will not work. Just check the generated sql for your query

User.where(conditions).where({:username => login} || { :email => login}).to_sql

I have made the modification so that things work properly without meta_where.

@babyhai

This comment has been minimized.

Show comment Hide comment
@babyhai

babyhai Jun 3, 2017

按着你的这么写的 登录提示密码错误 登陆会报错 不过我的注册需要邮箱才能注册 注册成功是直接进入登录的状态的 以此证明用户名 密码没错 即使邮箱+密码也不能登陆了 我需要怎么才能找到错误的地方

还有一点 rails5 使用attr_accessible 会报错 我是这么写的
def user_params
params.require(:user).permit(:login, :username, :email,
:password, :password_confirmation, :remember_me)
end

babyhai commented Jun 3, 2017

按着你的这么写的 登录提示密码错误 登陆会报错 不过我的注册需要邮箱才能注册 注册成功是直接进入登录的状态的 以此证明用户名 密码没错 即使邮箱+密码也不能登陆了 我需要怎么才能找到错误的地方

还有一点 rails5 使用attr_accessible 会报错 我是这么写的
def user_params
params.require(:user).permit(:login, :username, :email,
:password, :password_confirmation, :remember_me)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment