Working with Authlogic in script/console
When probing / debugging issues with Authlogic on rails console, you want to be able to create UserSessions and authenticate just like a live app does. However, you may run into issues if you plainly try to instantiate and save UserSession objects in console.
>> UserSession.new(:email => "validemail@domain.com", :password => "password") Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects from /var/lib/gems/1.8/gems/authlogic-2.1.1/lib/authlogic/session/activation.rb:47:in `initialize' from /var/lib/gems/1.8/gems/authlogic-2.1.1/lib/authlogic/session/klass.rb:61:in `initialize' from /var/lib/gems/1.8/gems/authlogic-2.1.1/lib/authlogic/session/scopes.rb:79:in `initialize' from (irb):2:in `new' from (irb):2
Authlogic uses framework adapters (RailsAdapter or MerbAdapter) to extract information such as session/cookies when working on authentication stuff. In order to enable this same mechanism for rails console, you need to do the following inside console:
>> Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
After this, you should be able to create UserSession objects like you normally would:
>> us = UserSession.new(:email => "validemail@domain.com", :password => "password") => #<UserSession: {:password="<protected", :email="validemail@domain.com"}> >> us.save! User Load (0.6ms) SELECT * FROM `users` WHERE (LOWER(`users`.email) = 'validemail@domain.com') LIMIT 1 SQL (0.2ms) BEGIN User Update (0.4ms) UPDATE `users` SET `failed_login_count` = 2, `updated_at` = '2009-09-25 02:31:57', `perishable_token` = 'bP_s1_z9gwCFmrIX9cdV' WHERE `id` = 1 SQL (1706.2ms) COMMIT Authlogic::Session::Existence::SessionInvalidError: Your session is invalid and has the following errors: Password is not valid from /var/lib/gems/1.8/gems/authlogic-2.1.1/lib/authlogic/session/existence.rb:85:in `save!' from (irb):11
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.






[...] console since authlogic will fail if you try to instantiate User object in console. You have to jump thru hoops (include Authlogic, set proper controller reference) to get it to work. Problem with Perishable [...]
Authlogic: after the initial hype | Pathfinder Development | Software Developers | Blogs
30 Sep 09 at 8:41 pm
Great fix! It’s great to be able to hop over to script/console to to some quick exploratory tests.
Question: Can is it possible to search for *all* logged_in users from script console without touching the User table? Does the UserSession model support this?
Joe
21 Oct 09 at 1:46 pm
Great fix! It’s great to be able to hop over to script/console to to some quick exploratory tests.
Question: Is it possible to search for *all* logged_in users from script console without touching the User table? Does the UserSession model support this?
Joe
21 Oct 09 at 2:05 pm
Good question. UserSession provides a ActiveRecord like interface to cookies/session. However, I don’t believe there is any static find* methods that would give us the aggregate information, atleast not directly.
You might want to crack open the gem files and see if there is any such thing built in. Or else the other option is to use :last_accessed_at and other magic columns on User model to figure out currently logged in users…
Sharad
25 Oct 09 at 2:53 am
Hi,
By any chance, do you have the same fix, but for backgroundrb ? Because I’m getting the exact same error, (You must activate the Authlogic::Session::Base.controller with a controller object before creating objects), but within the context of backgroundrb (see http://osdir.com/ml/authlogic/2009-12/msg00014.html)
Thank you.
Guillaume
8 Jan 10 at 3:25 pm
[...] Following the advice given here. [...]
Authlogic and backgroundrb… – PersoNEt(h)ic
8 Jan 10 at 7:13 pm
[...] console since authlogic will fail if you try to instantiate User object in console. You have to jump thru hoops (include Authlogic, set proper controller reference) to get it to work. Problem with Perishable [...]
Authlogic: after the initial hype | Pathfinder Software
8 Apr 11 at 4:55 pm
Cheers! Thanks for this
ArngrĂm
3 Jun 11 at 12:31 pm
I am using rails 2 with authlogic 2.1.9. When I us.save! I got this error
NoMethodError: undefined method `request’ for #
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/authlogic-2.1.9/lib/authlogic/controller_adapters/abstract_adapter.rb:34:in `request’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/authlogic-2.1.9/lib/authlogic/session/magic_columns.rb:61:in `update_info’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:178:in `send’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:178:in `evaluate_method’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:166:in `call’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:93:in `run’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `each’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `send’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `run’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:276:in `run_callbacks’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/authlogic-2.1.9/lib/authlogic/session/callbacks.rb:83:in `before_save’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/authlogic-2.1.9/lib/authlogic/session/existence.rb:68:in `save’
from /Users/omargarcia/.rvm/gems/ruby-1.8.7-p357/gems/authlogic-2.1.9/lib/authlogic/session/existence.rb:86:in `save!’
from (irb):6
Any help will be much appreciated
omar
25 Oct 12 at 3:47 pm