Skip to content

Instantly share code, notes, and snippets.

@AJ-Acevedo
Created July 25, 2013 06:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save AJ-Acevedo/6077336 to your computer and use it in GitHub Desktop.
Save AJ-Acevedo/6077336 to your computer and use it in GitHub Desktop.
# app / controllers / application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password, :remember_me) }
end
end
# app / models / user.rb
class User < ActiveRecord::Base
rolify
include Authority::UserAbilities
has_many :posts, foreign_key: :id
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:username]
after_save { self.add_role(:standard) unless self.has_any_role? }
validates_uniqueness_of :username
validates_presence_of :username
validates_presence_of :email
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:username)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end
# config / initializers / devise.rb
config.authentication_keys = [ :login ]
@morenoh149
Copy link

are you missing attr_accessor :login in the user model? If I omit it I get an error in the view.

@morenoh149
Copy link

oh nevermind, I'm mixing the documentation with that question. That question wanted to login with the username only, not username or email.

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