Customizing User login with Authlogic: find_by_login_method

This post would be redundant especially when Devise is the “in” thing but I love the minimality of Authlogic and I’m sure over time I’ll grow to like Devise but for now Authlogic does everything I want.

In a scenario where you want Authlogic to find Users from a subset of users while adhering to a specific condition that you set (for instance: login users from a set of users who have active accounts), you would need to define some kind of condition or scope to find these users.

A simple configuration that Authlogic allows is

class UserSession < Authlogic::Session::Base
  find_by_login_method  :find_by_activated_user
end

The find_by_login_method identifies the class method that would be called upon the User model to find the user.

In user.rb

  def self.find_by_activated_user(email)
    self.find_by_email_and_activated(email, true)
  end

where activated is a boolean attribute that identifies if a user has been blocked from using the site.

Advertisements