allows login using username or email
This commit is contained in:
@@ -3,7 +3,8 @@ class User < ActiveRecord::Base
|
||||
include Verification
|
||||
|
||||
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable,
|
||||
:trackable, :validatable, :omniauthable, :async, :password_expirable, :secure_validatable
|
||||
:trackable, :validatable, :omniauthable, :async, :password_expirable, :secure_validatable,
|
||||
authentication_keys: [:login]
|
||||
|
||||
acts_as_voter
|
||||
acts_as_paranoid column: :hidden_at
|
||||
@@ -48,6 +49,7 @@ class User < ActiveRecord::Base
|
||||
|
||||
attr_accessor :skip_password_validation
|
||||
attr_accessor :use_redeemable_code
|
||||
attr_accessor :login
|
||||
|
||||
scope :administrators, -> { joins(:administrators) }
|
||||
scope :moderators, -> { joins(:moderator) }
|
||||
@@ -286,6 +288,16 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
delegate :can?, :cannot?, to: :ability
|
||||
|
||||
# overwritting of Devise method to allow login using email OR username
|
||||
def self.find_for_database_authentication(warden_conditions)
|
||||
conditions = warden_conditions.dup
|
||||
if login = conditions.delete(:login)
|
||||
where(conditions.to_hash).where(["lower(email) = ? OR username = ?", login.downcase, login]).first
|
||||
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
|
||||
where(conditions.to_hash).first
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_document_number
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<%= f.email_field :email, autofocus: true, placeholder: t("devise_views.sessions.new.email_label") %>
|
||||
<%= f.text_field :login, autofocus: true, placeholder: t("devise_views.sessions.new.login_label") %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 column">
|
||||
|
||||
@@ -113,6 +113,7 @@ en:
|
||||
description: "Description"
|
||||
terms_of_service: "Terms of service"
|
||||
user:
|
||||
login: "Email or username"
|
||||
email: "Email"
|
||||
username: "Username"
|
||||
password_confirmation: "Password confirmation"
|
||||
|
||||
@@ -108,6 +108,7 @@ es:
|
||||
description: "Descripción"
|
||||
terms_of_service: "Términos de servicio"
|
||||
user:
|
||||
login: Email o nombre de usuario
|
||||
email: "Correo electrónico"
|
||||
username: "Nombre de usuario"
|
||||
password_confirmation: "Confirmación de contraseña"
|
||||
|
||||
@@ -68,7 +68,7 @@ en:
|
||||
title: Forgotten password?
|
||||
sessions:
|
||||
new:
|
||||
email_label: Email
|
||||
login_label: Email or username
|
||||
password_label: Password
|
||||
remember_me: Remember me
|
||||
submit: Enter
|
||||
|
||||
@@ -68,7 +68,7 @@ es:
|
||||
title: "¿Has olvidado tu contraseña?"
|
||||
sessions:
|
||||
new:
|
||||
email_label: Email
|
||||
login_label: Email o nombre de usuario
|
||||
password_label: Contraseña
|
||||
remember_me: Recordarme
|
||||
submit: Entrar
|
||||
|
||||
@@ -35,7 +35,19 @@ feature 'Users' do
|
||||
|
||||
visit '/'
|
||||
click_link 'Sign in'
|
||||
fill_in 'user_email', with: 'manuela@consul.dev'
|
||||
fill_in 'user_login', with: 'manuela@consul.dev'
|
||||
fill_in 'user_password', with: 'judgementday'
|
||||
click_button 'Enter'
|
||||
|
||||
expect(page).to have_content 'You have been signed in successfully.'
|
||||
end
|
||||
|
||||
scenario 'Sign in with username' do
|
||||
create(:user, username: 'larry', email: 'manuela@consul.dev', password: 'judgementday')
|
||||
|
||||
visit '/'
|
||||
click_link 'Sign in'
|
||||
fill_in 'user_login', with: 'larry'
|
||||
fill_in 'user_password', with: 'judgementday'
|
||||
click_button 'Enter'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user