Alex Rivera | Logout

How to call devise sign_in and sign_out methods of devise from another controller different from devise?

Asked 2011-10-19T21:38:16.247
14

I have a Ruby on Rails 3.0.7 application and my user authentication with devisebut and I have an alternative sign up and sign in methods, I allow my users to sign up using their Facebook account then I save that login info in my users table the same one that devise uses to register and login users.

Steps

  1. ✔ User click on the Facebook button.
  2. ✔ I save his info (name and email extracted from Koala) I give the user a generic password.
  3. ☐ Login this new user with devise.
  4. ✔ Redirect to my main controller.

I'm just missing the 3rd step because I want to keep using the current_user helper and the user_signed_in? helper too.

So how do I tell devise to sign in this user automatically from my other controller?

I saw something like that on this question Devise: Have multiple controllers handle user sessions and it logs my user in but leads me to an blank page...

Edit
Report

1 Answer

22

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

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

You can use these from any controller.

EDIT

If using sign_in already works for you but leaves the user on a blank page, check your logfile to see if there is a redirect going on, and where it redirects to. Or just make the redirect explicit by using the second of the helpers above.

answered 2011-11-14T15:11:45.740

Your Answer