Devise is a one-time form for multiple users.

I have two Devise models, User and Business; I would like both to be able to log in using one sign in the form. I use backbone js and I have a customized view, so the view is not a concern. The ajax request is used to enter the system; it works for users as expected, but not for business.

I searched google and came up with several solutions that mention using STI to solve this problem, but the project is very developed and I cannot make such a change now. I was thinking about overriding the Devise session controller and:

  • Check if the given email address is a user, then authenticate the user with Warden.
  • If no user with this email address is found, then authenticate using the Business model using Warden.

I can’t understand how to change the code to achieve the above, I don’t know how the overseer works, and what parameters I need to configure to achieve the above, what functions need to be called. Can someone point me in the right direction or give an example of how I can move on.

Thanks.

+4
source share
3 answers

Do not worry, you are not stuck.

I think your best option is to override the Devise session controller and change the "new" and "create" methods for the session.

So, in your own "session_controller.rb" you will have something like this:

class SessionsController < Devise::SessionsController # GET /resource/sign_in def new resource = build_resource(nil, :unsafe => true) clean_up_passwords(resource) respond_with(resource, serialize_options(resource)) end # POST /resource/sign_in def create resource = warden.authenticate!(auth_options) set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_with resource, :location => after_sign_in_path_for(resource) end end 

And on your routes you will use something like (depending on what you called your custom models):

 devise_for :users, :controllers => { :sessions => 'sessions' } devise_for :businesses, :controllers => { :sessions => 'sessions' } 

The specified session controller is not configured at all. I do not know your code, so I can not help. But the main steps are:

  • Inspect the auth_options and resource_name variables to understand how they store data.
  • Add conditional logic to modify these variables if resource not found in the Users table.

Devise is excellent, and the roles are great, but sometimes with one user model or even using STI does not make sense. I am writing a longer post on this exact issue in the near future, as I dealt with this in a recent project.

+6
source

Consider using only the user model and use CanCan and Rolify to meet the unique needs of each type of user. This may mean getting started with some of your code, but in the end, it may be easier to maintain.

Do not tell me about it. Here is a related question with good reviews.

+4
source

I recently came up with this general hack to make this work. My advice is to strongly consider one user model with a role, but it works - at least with the current version of Devise.

https://gist.github.com/jeremyw/5319386

If you monkeypatch or otherwise override the default behavior like this, make sure you have a good set of tests if you ever want to upgrade Devise.

+4
source

Source: https://habr.com/ru/post/1435517/


All Articles