Skip to content

Instantly share code, notes, and snippets.

@kdwinter
Last active September 5, 2015 05:45
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdwinter/444888 to your computer and use it in GitHub Desktop.
Save kdwinter/444888 to your computer and use it in GitHub Desktop.
Authlogic plugin for MongoMapper
module Authenticable
extend ActiveSupport::Concern
included do
key :username, String
key :email, String
key :crypted_password, String
key :password_salt, String
key :persistence_token, String
key :login_count, Integer, :default => 0
key :last_request_at, Time
key :last_login_at, Time
key :current_login_at, Time
key :last_login_ip, String
key :current_login_ip, String
class << self
alias named_scope scope
end
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
acts_as_authentic do |c|
c.login_field = 'username'
c.merge_validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true
end
end
module ClassMethods
def base_class
self
end
def primary_key
:_id
end
def default_timezone
:utc
end
def with_scope(qry)
qry = where(qry) if qry.is_a?(Hash)
yield qry
end
end
module InstanceMethods
def save(options = {})
options = {:validate => options} unless options.is_a?(Hash)
super
end
def readonly?
false
end
end
end
class User
include MongoMapper::Document
plugin Authenticable
timestamps!
end
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [self.send(self.class.primary_key)]
end
end
@kalbasit
Copy link

Hey Gigamo,

I have a newbie question, the files user.rb and user_session.rb goes into the app/models folder, but where does the other two files goes? Do I have to manually create a gem for each ?

Thank you

@kdwinter
Copy link
Author

You can put them anywhere in your load path. I personally made a new app/plugins directory in which I put authenticable.rb, and I put the validatable fix inside lib somewhere.

@kalbasit
Copy link

Got it!

Thank you

@alashbwm
Copy link

Is the validatable fix gone?

@kdwinter
Copy link
Author

That fix should no longer be required with the newer versions (that make use of ActiveModel) of MongoMapper.

@alashbwm
Copy link

Awesome, thanks! I am close, but I keep getting:

undefined method `login_field' for User:Class

But I see:

34     model.acts_as_authentic do |c|
35       c.login_field = 'username'
36       c.merge_validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true
37     end

Any suggestions?

@alashbwm
Copy link

FYI, I am using this guide for the rest of AuthLogic / Rails 3 : http://www.dixis.com/?p=352

@michaelegray
Copy link

I'm occasionally now getting this issue when I create a new user in the system:

NoMethodError (undefined method []=' for BSON::ObjectId('4e6e5c8414a6940001000006'):BSON::ObjectId): 2011-09-12T19:24:52+00:00 app[web.1]: app/models/authenticable.rb:58:insave'
2011-09-12T19:24:52+00:00 app[web.1]: app/controllers/users_controller.rb:4:in `create'

The error isn't consistant. I can get it to fail when I deploy on heroku+mongohq, but it doesn't fail when I run it locally (with a local mongodb).

Any ideas?

@spockz
Copy link

spockz commented Mar 5, 2012

I'm getting the following error:

undefined local variable or method `model' for User:Class

With Rails 3.1.3.

Copy link

ghost commented Mar 6, 2012

i getting the following error:
uninitialized constant ActiveSupport::Concern (NameError)
with rails 2.3.11 and mongomapper

@htaykhaing
Copy link

I'm getting same error with spockz...
How should i fix that error?
Thanks.

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