Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 751
Mr_Nizzle Avatar asked Oct 19 '11 21:10

Mr_Nizzle


People also ask

What is devise authentication?

Devise is a well known solution for authentication in Rails applications. It's full featured (it not only adds authentication but also password recovery, email changing, session timeout, locking, ip tracking, etc.) and can be expanded to add even more (like JWT authentication).

What is devise warden?

The devise gem is basically based on a warden gem, which gives an opportunity to build authorization direct on a Ruby Rack Stack. This gem is pretty straightforward and well documented. Warden fetches a request data and checks if the request includes valid credentials, according to a defined strategy.


1 Answers

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.

like image 94
Thilo Avatar answered Oct 28 '22 00:10

Thilo