How to programmatically sign a user through Devise in Rails

I need to have a custom login mechanism using Devise with Rails 4. Therefore, I found the sign_in method in the "Deploy Test Assistants" section of my documentation :

 sign_in @user # sign_in(resource) 

But is this the right way to sign someone from the Internet? In particular, will he do everything Devise does when a user signs up, for example, writing down date / time stamps, IP addresses, counts, etc.? Or is it just for testing purposes?

+6
source share
3 answers

This is the correct and standard way to programmatically sign a user. Looking at the login code for # create sessions , you can see that they use this method as well.

+4
source

Devise offers a bunch of helpers, two of which are:

 sign_in(resource_or_scope, *args) sign_in_and_redirect(resource_or_scope, *args) 

You can use them from any controller.

If using sign_in already works for you, but leaves the user on a blank page, check your log file to see if it is being redirected and where it is being redirected to. Or just make the redirection explicit using the second of the helpers above.

Literature:

+4
source

In short: Yes, sign_in @user does everything that is developed, usually when a user logs in. This can be useful, for example, allowing the Administrator to log in as one of his users.

How To: Log in as a different user if you are an administrator

+2
source

All Articles