You can now login using either username or email

This commit is contained in:
Skud
2012-09-03 12:38:58 +01:00
parent b4ab40a2b3
commit dcd4f79bdc
8 changed files with 40 additions and 10 deletions

View File

@@ -6,6 +6,22 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :username, :email, :password, :password_confirmation,
:remember_me, :login
# attr_accessible :title, :body
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# allow login via either username or email address
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end

View File

@@ -3,8 +3,8 @@
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :login %><br />
<%= f.text_field :login %></div>
<div><%= f.submit "Send me reset password instructions" %></div>
<% end %>

View File

@@ -3,6 +3,9 @@
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>

View File

@@ -1,8 +1,8 @@
<h2>Sign in</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :login %><br />
<%= f.text_field :login %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>

View File

@@ -23,7 +23,7 @@ Devise.setup do |config|
# session. If you need permissions, you should implement that in a before filter.
# You can also supply a hash where the value is a boolean determining whether
# or not authentication should be aborted when the value is not present.
# config.authentication_keys = [ :email ]
config.authentication_keys = [ :login ]
# Configure parameters from the request object used for authentication. Each entry
# given should be a request method and it will automatically be passed to the
@@ -99,7 +99,7 @@ Devise.setup do |config|
config.reconfirmable = true
# Defines which key will be used when confirming an account
# config.confirmation_keys = [ :email ]
config.confirmation_keys = [ :login ]
# ==> Configuration for :rememberable
# The time the user will be remembered without asking for credentials again.
@@ -155,7 +155,7 @@ Devise.setup do |config|
# ==> Configuration for :recoverable
#
# Defines which key will be used when recovering the password for an account
# config.reset_password_keys = [ :email ]
config.reset_password_keys = [ :login ]
# Time interval you can reset your password with a reset password key.
# Don't put a too small interval or your users won't have the time to
@@ -229,4 +229,4 @@ Devise.setup do |config|
# When using omniauth, Devise cannot automatically set Omniauth path,
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = "/my_engine/users/auth"
end
end

View File

@@ -1,6 +1,11 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
en:
activerecord:
attributes:
user:
login: "Username or email"
errors:
messages:
expired: "has expired, please request a new one"

View File

@@ -0,0 +1,5 @@
class AddUsernameToUsers < ActiveRecord::Migration
def change
add_column :users, :username, :string
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120903092956) do
ActiveRecord::Schema.define(:version => 20120903112806) do
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
@@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 20120903092956) do
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true