Rails 3 - How can you get access to Devise's current_user in the IRB console?

守給你的承諾、 提交于 2019-11-29 20:41:56

current_user is a property of the controller so after app.post('/sign_in', ... you can call app.controller.current_user in your rails console to get the User object

It might be possible that you are not really logging in. One thing to keep in mind is that Devise I build on top of Warden which is rack middleware.

I tried your app.post method of logging in on an app I'm working on that uses Devise. After posting to the login page and getting a 302 redirect the app.session showed the warden user id.

>> app.session
{
 "_csrf_token"=>"dT0/BqgLb84bnE+f1g...",
 "warden.user.user.key"=>["User", [42843], "$2a$10$1OU.1BixIba..."],
 "session_id"=>"0dd49c05ff4e6362c207c6eb877f86cd"
}

I was able to fetch the user like this:

>> current_user = User.find(app.session["warden.user.user.key"][1][0])

When I logged out and then tried logging in with a bad password I get a 200 and then the app.session is missing the warden user info and only contained the csrf token and session id like your example.

BTW: Once logged in app.controller.current_user was nil even when the warden user id was in the session.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!