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
@mcansky
Copy link

mcansky commented Aug 12, 2011

interesting, I'm trying to make the original pastie work with sinatra, but so far no luck, thinking it's almost 2 years old I believe some stuff have evolved
yet something stroke me : your code looks much more modular and doesn't have the part where some methods (callbacks) are generated) is it because of mongoid or just the way you wrote the thing ?

@jimsynz
Copy link

jimsynz commented Aug 13, 2011

I gave up and switched to Devise :)

@rfc2822
Copy link

rfc2822 commented Aug 14, 2011

Having the same problem like cmelbye, I found out that it is enough to comment out the line
include Authlogic::ActsAsAuthentic::LoggedInStatus
as this sub-module causes the where problem.

@jpascal
Copy link

jpascal commented Nov 7, 2011

Understand where a problem :(

Started POST "/sessions" for 127.0.0.1 at 2011-11-07 16:09:18 +0300
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bB2XWOa8vsAW0Zd2DK0GeTAaOgpU5cbUK7JPnFsaH2s=", "session"=>{"username"=>"jpascal", "password"=>"[FILTERED]"}, "commit"=>"Login"}
MONGODB grabs['system.namespaces'].find({})
MONGODB grabs['users'].find({:username=>"jpascal"}).limit(-1).sort([[:_id, :asc]])
Completed 500 Internal Server Error in 185ms

TypeError (nil is not a symbol):
app/controllers/sessions_controller.rb:10:in `create'

Rendered /usr/local/rvm/gems/ruby-1.9.2-p290@hotelgrab/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
Rendered /usr/local/rvm/gems/ruby-1.9.2-p290@hotelgrab/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.1ms)
Rendered /usr/local/rvm/gems/ruby-1.9.2-p290@hotelgrab/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.5ms)

class SessionsController < ApplicationController
...
def create
@user_session = UserSession.new(params[:session])
if @user_session.save
redirect_to root_url
else
render :action => 'new'
end
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