Search code examples
ruby-on-railsconsoledevise

How to sign in a user using Devise from a Rails console?


After loading the Rails console, how should I sign in a user?

Devise provides a test helper which can be used in tests and I've tried to use in console:

>> include Devise::TestHelpers
>> helper.sign_in(User.first)

But I get:

NoMethodError: undefined method `env' for nil:NilClass

Anyway, I would like to use the real devise helper and not this test helper. Is there any way to achieve this?


Solution

  • Here's one way I was able to do it:

    >> ApplicationController.allow_forgery_protection = false
    >> app.post('/sign_in', {"user"=>{"login"=>"login", "password"=>"password"}})
    

    Then you can do:

     >> app.get '/some_other_path_that_only_works_if_logged_in'
     >> pp app.response.body