Use devise on multiple models

Implement the authentication process for each user and administrator using devise. The namespace is implemented without dividing it so that the explanations unrelated to devise do not increase, but it seems that it is often better to separate it when actually developing from 0.

See the following article for the basic usage of devise [Rails] Key points for using devise at a minimum --Qiita

install devise

Gemfile


gem 'devise'
$ bundle install
$ rails g devise:install

config/environments/development.rb


config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

erb:app/views/layouts/application.html.erb


<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Create Model

$ rails g devise User
$ rails g devise AdminUser
$ rails db:migrate

Create Controller

$ rails g devise:controllers users
$ rails g devise:controllers admin_users

Create View

config/initializers/devise.rb


config.scoped_views = true
$ rails g devise:views users
$ rails g devise:views admin_users

Create routing

config.routes.rb


Rails.application.routes.draw do
  devise_for :users, controllers: {
    sessions: 'users/sessions',
    passwords: 'users/passwords',
    registrations: 'users/registrations'
  }
  devise_for :admin_users, controllers: {
    sessions: 'admin_users/sessions',
    passwords: 'admin_users/passwords',
    registrations: 'admin_users/registrations'
  }
end

at this point http://localhost:3000/users/sign_in http://localhost:3000/admin_users/sign_in Access and confirm that the login screen is displayed.

Create a test page

$ rails g controller static_pages index
$ rails g scaffold UserPost title body
$ rails g scaffold AdminUserPost title body
$ rails db:migrate

config/routes.rb


Rails.application.routes.draw do
  root to: 'static_pages#index'
  resources :admin_user_posts
  resources :user_posts
  #abridgement
end

erb:app/views/static_pages/index.html.erb


<h2>user</h2>
<%= link_to 'Login', new_user_session_path %>
<%= link_to 'sign up', new_user_registration_path %>
<h2>Administrator</h2>
<%= link_to 'Login', new_admin_user_session_path %>
<%= link_to 'sign up', new_admin_user_registration_path %>

erb:app/views/user_posts/index.html.erb


<%= link_to 'Log out', destroy_user_session_path, method: :delete %>
.
.
.

erb:app/views/admin_user_posts/index.html.erb


<%= link_to 'Log out', destroy_admin_user_session_path, method: :delete %>
.
.
.

Correct the transition destination after login

After logging in as User, go to user_posts # index Set to transition to admin_user_posts # index after logging in as AdminUser.

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    case resource
    when User
      user_posts_path
    when AdminUser
      admin_user_posts_path
    end
  end
end

Make it inaccessible without logging in

The User Post screen can be displayed only when logged in as User You can display the AdminUserPost screen only when you are logged in as AdminUser Set as.

app/controllers/user_posts_controller.rb


class UserPostsController < ApplicationController
  before_action :authenticate_user!
  #abridgement
end

app/controllers/admin_user_posts_controller.rb


class AdminUserPostsController < ApplicationController
  before_action :authenticate_admin_user!
  #abridgement
end

This time, there is only one controller for both the user screen and the management screen, so there is no problem with this method, but in reality, multiple controllers will be created, so name_space is separated and each application_controller is set to before_action. I think it's better.

Recommended Posts

Use devise on multiple models
[Rails] Manage multiple models using devise gem
Manipulate multiple models on form objects [De-accepts_nested_attributes_for]
Use perltidy on CentOS 8
Use Flutter on Ubuntu
Notes on multiple inheritance
Use mod_auth_cas on CentOS 8
Use bat on Centos.
Use mkdir on ubuntu
Use Corretto 11 on Heroku
Use cpplapack on ubuntu
Use Swift Package on Playground
Use multiple databases with Rails 6.0
Use native code on Android
Use multiple checkboxes in Rails6!
Use PG Backups on Heroku
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
How to use Bio-Formats on Ubuntu 20.04
Use Docker on your M1 Mac
Preparing to use electron-react-boilerplate on Ubuntu 20.4
Ruby On Rails devise routing conflict
Use the iostat command on CentOS 8
Use completion in Eclipse on mac
Use Docker CE (official) on CentOS 8