Tatva-Artha

meaning of "it"

Working with Authlogic in script/console

with 6 comments

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
http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

Related posts:

  1. Ruby Authlogic: lazy initialization based on defined?() call What is the difference between following two versions of code?...
  2. Authlogic: after the initial hype On my rails projects, I’ve used restful_authentication before and I...
  3. Authlogic: customizing default validations On new Ruby/Rails projects, we are going with authlogic for...
  4. Getting session_id in rails If you are using database sessions, rails sessions table stores...
  5. Cookie Testing Quirks in Rails I documented about a gotcha with cookie testing in rails...

Related posts brought to you by Yet Another Related Posts Plugin.

Written by Sharad

September 25th, 2009 at 2:38 am

Posted in All

6 Responses to 'Working with Authlogic in script/console'

Subscribe to comments with RSS or TrackBack to 'Working with Authlogic in script/console'.

  1. [...] 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 [...]

  2. 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

  3. 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

  4. 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

  5. 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

  6. [...] Following the advice given here. [...]

Leave a Reply