Authenticate with username

Tim Petricola edited this page Jul 31, 2014 · 3 revisions

The following instructions apply for Clearance >= 0.11.0.

In your model:

class User < ActiveRecord::Base
  include Clearance::User

  def email_optional?
    true
  end

  def self.authenticate(username, password)
    return nil  unless user = find_by_username(username)
    return user if     user.authenticated?(password)
  end
end

In app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base
  include Clearance::Authentication

  def authenticate(params)
    User.authenticate(params[:session][:username],
                      params[:session][:password])
  end
end