Skip to content

Instantly share code, notes, and snippets.

@kdwinter
Created June 14, 2010 21:52
Show Gist options
  • Save kdwinter/438370 to your computer and use it in GitHub Desktop.
Save kdwinter/438370 to your computer and use it in GitHub Desktop.
Authlogic with Mongoid (in spirit of http://pastie.org/503478)
module Authenticable
def self.included(model)
model.class_eval do
extend ClassMethods
include InstanceMethods
field :username
field :email
field :crypted_password
field :password_salt
field :persistence_token
field :login_count, :type => Integer, :default => 0
field :last_request_at, :type => DateTime
field :last_login_at, :type => DateTime
field :current_login_at, :type => DateTime
field :last_login_ip
field :current_login_ip
index :username
index :email
index :persistence_token
index :last_login_at
include Authlogic::ActsAsAuthentic::Base
include Authlogic::ActsAsAuthentic::Email
include Authlogic::ActsAsAuthentic::LoggedInStatus
include Authlogic::ActsAsAuthentic::Login
include Authlogic::ActsAsAuthentic::MagicColumns
include Authlogic::ActsAsAuthentic::Password
include Authlogic::ActsAsAuthentic::PerishableToken
include Authlogic::ActsAsAuthentic::PersistenceToken
include Authlogic::ActsAsAuthentic::RestfulAuthentication
include Authlogic::ActsAsAuthentic::SessionMaintenance
include Authlogic::ActsAsAuthentic::SingleAccessToken
include Authlogic::ActsAsAuthentic::ValidationsScope
end
end
module ClassMethods
def <(klass)
return true if klass == ::ActiveRecord::Base
super(klass)
end
def column_names
fields.map &:first
end
def quoted_table_name
'users'
end
def primary_key
# FIXME: Is this check good enough?
if caller.first.to_s =~ /(persist|session)/
:_id
else
@@primary_key
end
end
def default_timezone
:utc
end
def find_by__id(*args)
find *args
end
# Change this to your preferred login field
def find_by_username(username)
where(:username => username).first
end
def with_scope(query)
query = where(query) if query.is_a?(Hash)
yield query
end
end
module InstanceMethods
def readonly?
false
end
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
include Authenticable
acts_as_authentic do |config|
# Change this to your preferred login field
config.login_field = 'username'
config.merge_validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true
end
end
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [self.send(self.class.primary_key)]
end
end
@jurikern
Copy link

jurikern commented May 4, 2013

index({:username => 1}, {:unique => true})
index({:email => 1}, {:unique => true})
index({:persistence_token => 1}, {:unique => true})

It's new syntax for creating indexes

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