Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Devise for Two Different Models but the same Login Form

I have seen lots of similar questions here but nothing that quite fits my need.

I am a pretty experience rails developer but this new project is my first time using both Rails 3 and Devise (I'm normally on authlogic).

My app has two different models that I want to authenticate via devise.

One, User is just a standard users model Two, Business is similar to a user, (it has an email address column too) but it has additional info in the database (address, phone number, etc..)

I want to be able to log them both in via the same login form. Then obviously once they are logged in they will be presented with different info depending on what type of Model has logged in.

It may or may not be relevant that I was planning on using OmniAuth to allow Users (though probably not businesses) to sign up/on via facebook.

Thanks!

What's the easiest way to go about doing this?

like image 219
goddamnyouryan Avatar asked Mar 25 '11 20:03

goddamnyouryan


2 Answers

I think the only way to handle this would be to have your own custom sign in form and controller that determined the type of user and then sign them in correctly. I would recommend an approach like what mark mentioned for simplicity (take a look at something like CanCan to manage roles).

Another potential problem with having multiple user models is that you will have multiple versions of all the devise helper methods. So for current_<resource> and <resource>_signed_in? you would have current_user, current_business_user, user_signed_in? and business_user_signed_in?. Then you would either have to implement your own versions of these methods or you would need to check both versions everywhere you used them.

like image 108
Braden Becker Avatar answered Nov 04 '22 16:11

Braden Becker


Can do this in application_controller?

current_user = current_resource_a || current_resource_b

like image 34
cactis Avatar answered Nov 04 '22 15:11

cactis