简体   繁体   中英

Two sign_in forms each on own url in rails devise

Its possible to create 2 sign in views / 2 forms each on own urls in DEVISE ? I need one for web administrator (back administration) and one for users (application), 1 model (with roles)

  1. web administration / admin.domain.tld/login
  2. user application / app.domain.tld/login

(after unsucessful sign in must stay on some url admin on admin.domain.tld/login , and user on app.domain.tld/login)

is this possible ? how to do it ?

i have it

controllers/admin/sessions_controller.rb

class Admin::SessionsController < Devise::SessionsController
end

controllers/app/sessions_controller.rb

class App::SessionsController < Devise::SessionsController
end

views/admin/sessions/new.html.erb

<%= simple_form_for(resource, :as => resource_name, url: admin_login_path) do |f| %>
    <div class="form-inputs">
      <%= f.input :email, :required => false, :autofocus => true %>
      <%= f.input :password, :required => false %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Sign in" %>
    </div>
<% end %>

views/app/sessions/new.html.erb

<%= simple_form_for(resource, :as => resource_name, url: app_login_path) do |f| %>
    <div class="form-inputs">
      <%= f.input :email, :required => false, :autofocus => true %>
      <%= f.input :password, :required => false %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Sign in" %>
    </div>
<% end %>

config/routes.rb

  namespace :app do
    devise_for :users

    devise_scope :app_user do
      get 'login' => 'sessions#new'
      post 'login' => 'sessions#create'
    end
  end

  namespace :admin do
    devise_for :users

    devise_scope :admin_user do
      get 'login' => 'sessions#new'
      post 'login' => 'sessions#create'
    end
  end

all works fine, also possible add :constraints => {:host => "domain"} and edit routes to desired url's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM