Using Devise with multiple namespaces for the same model

I want to use Devise with two namespaces: the API namespace and the default namespace, but they seem inconsistent when the user tries to log in. Meaning, whatever namespace referencing Devise does not end first being the endpoint of the redirect. Example: if I try to create a new session in the default namespace, it will fail on this user session path, and then try to create a session on the API / v1 session path.

How to do two actions independently?

They both reference the User object. The user_sessions controller for the default namespace uses "user_sessions". The user user_sessions controller for the API / V1 namespace is "/ api / v1 / user_sessions"

---- ROUTES.RB ------- MySite::Application.routes.draw do namespace :api do namespace :v1 do devise_for :users,:controllers => { :sessions => "api/v1/ user_sessions",:registrations=>"users" } ...... end end devise_for :users,:controllers => { :sessions =>"user_sessions",:registrations=>"users" } do post 'users/sign_in' => 'user_sessions#create', :as => :user_session get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session match 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session <.....> end 

----- DEFAULT NAMESPACE USER_SESSIONS_CONTROLLER -----

 class UserSessionsController < Devise::SessionsController .... end ---- 

API NAMESPACE USER_SESSIONS_CONTROLLER ---- (this applies to my custom Devise base controller noted below)

 class Api::V1::UserSessionsController < Api::V1::DeviseBaseController ... end 

---- CONTROLLER OF BASIC CONTROLLERS FOR PURPOSE OF THE BEST INDICATORS ----

 class Api::V1::DeviseBaseController < Devise::SessionsController respond_to :json end 
+6
source share
2 answers

I found out that this is not possible with Devise, you must have one input / output from the source.

A clean solution. Create routes for both the API and Web namespaces that point to the same Devise controller code (say / user / sessions). There, partial parts for the corresponding response (JSON, HTML). These partial files can be placed in view directories for each namespace, while maintaining cleanliness.

+6
source

You will need to define two different roles for the user, such as admin and end user.

and then define these routes for it

  FourtySixLabs::Application.routes.draw do namespace :admin do resources :posts end namespace :end_user do resources :posts end devise_for :users, :controllers => { :sessions => "users/sessions", :confirmation => "users/confirmations", :passwords => "users/passwords", :registrations => "users/registrations", } devise_for :users, as: :user do get 'admin', :to => 'users/sessions#new', :as => :new_user_session get "end_user", :to => "users/sessions#new" get "sign_out", :to => "users/sessions#destroy" end end 

And then the administrator will start from this URL local: 3000 / admin

And then end_user will log in from this local URL: 3000 / end_user

For role definitions see this

Rails: adding an admin role with a developer who can see all users

I hope you understand the idea

+2
source

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


All Articles