Tatva-Artha

meaning of "it"

Working with Authlogic in script/console

with 9 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

No related posts.

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

Written by Sharad

September 25th, 2009 at 2:38 am

Posted in All

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

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

  8. Cheers! Thanks for this

    ArngrĂ­m

    3 Jun 11 at 12:31 pm

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

Leave a Reply