3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails初心者】ユーザー/管理者を分けて認証機能を作る

Last updated at Posted at 2022-09-14

【Rails初心者】ユーザー/管理者を分けて認証機能を作る

アカウント管理を行える「devise」を使って、「管理者(admin)」と「会員(user)」を作成する方法をまとめてみた

詳しくは、heartcombo/devise のReadmeを参照

全体の流れ

  1. deviseのインストール
  2. installコマンドとconfigファイルの修正
  3. admin/userモデルを作成
  4. admins/usersコントローラを作成
  5. admins/usersビューを作成
  6. routesの編集
  7. デフォルトのビューを削除

1.deviseのインストール

「devise」を「Gemfile」に追加して、インストールします。

Gemfile
gem 'devise'
Console
bundle install
Console
rails g devise:install

2.installコマンドとconfigファイルの修正

修正前を修正後に変更する

Devise がすべてのモデルに同じビューを使用しているため、別のビューを使用できるように修正している。

Readmeでいうと、Configuring viewsの部分を参考に作成してます。

/config/initializers/devise.rb (修正前)
::
# config.scoped_views = false
::
/config/initializers/devise.rb (修正後)
::
config.scoped_views = true
::

3.admin/customerモデルを作成

Console (各モデル作成)
rails g devise admin
rails g devise user
Console
rails db:migrate

4.admins/usersコントローラを作成

Console (各モデル作成)
rails g devise:controllers admins
rails g devise:controllers users

controller作成時に、以下の表示がされる。
「routes.rb で生成されたコントローラーのルートをオーバーライドしていることを確認してください。」とのことなので、後程 routes.rbを修正するために内容を確認しておく。

Command (controller作成時に表示される)
ec2-user:~/environment/hogehoge (main) $ rails g devise:controllers users
      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb
===============================================================================

Some setup you must do manually if you haven't yet:

  Ensure you have overridden routes for generated controllers in your routes.rb.
  For example:

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

===============================================================================

5.admins/usersビューを作成

Console
rails g devise:views admins
rails g devise:views users

6.「Routing」の作成

まずは、理解度を深めるためにrouteを確認する

Command (adminのroutepath)
ec2-user:~/environment/hogehoge (main) $ rails routes |grep admin
                       new_admin_session GET    /admins/sign_in(.:format)               devise/sessions#new
                           admin_session POST   /admins/sign_in(.:format)               devise/sessions#create
                   destroy_admin_session DELETE /admins/sign_out(.:format)              devise/sessions#destroy
                      new_admin_password GET    /admins/password/new(.:format)          devise/passwords#new
                     edit_admin_password GET    /admins/password/edit(.:format)         devise/passwords#edit
                          admin_password PATCH  /admins/password(.:format)              devise/passwords#update
                                         PUT    /admins/password(.:format)              devise/passwords#update
                                         POST   /admins/password(.:format)              devise/passwords#create
               cancel_admin_registration GET    /admins/cancel(.:format)                devise/registrations#cancel
                  new_admin_registration GET    /admins/sign_up(.:format)               devise/registrations#new
                 edit_admin_registration GET    /admins/edit(.:format)                  devise/registrations#edit
                      admin_registration PATCH  /admins(.:format)                       devise/registrations#update
                                         PUT    /admins(.:format)                       devise/registrations#update
                                         DELETE /admins(.:format)                       devise/registrations#destroy
                                         POST   /admins(.:format)                       devise/registrations#create
Command (userのroutepath)
ec2-user:~/environment/hogehoge (main) $ rails routes |grep user
                        new_user_session GET    /users/sign_in(.:format)                devise/sessions#new
                            user_session POST   /users/sign_in(.:format)                devise/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)               devise/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)           devise/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)          devise/passwords#edit
                           user_password PATCH  /users/password(.:format)               devise/passwords#update
                                         PUT    /users/password(.:format)               devise/passwords#update
                                         POST   /users/password(.:format)               devise/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                 devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                   devise/registrations#edit
                       user_registration PATCH  /users(.:format)                        devise/registrations#update
                                         PUT    /users(.:format)                        devise/registrations#update
                                         DELETE /users(.:format)                        devise/registrations#destroy
                                         POST   /users(.:format)                        devise/registrations#create

Controller#Action が同じになっている…
Actionの記載が複雑になるので、adminuserでControllerが分かれるようにする

Controller#Action をカスタマイズしたい場合は、devise_forメソッドを介してカスタマイズする必要がある
Configuring routesを参考に作成

/config/routes.rb
devise_for :admins, controllers: {
  sessions:      'admins/sessions',
  passwords:     'admins/passwords',
  registrations: 'admins/registrations'
}
devise_for :users, controllers: {
  sessions:      'users/sessions',
  passwords:     'users/passwords',
  registrations: 'users/registrations'
}

Controller#Action がそれぞれ変わっていることを確認

Command (adminのroutepath)
ec2-user:~/environment/hogehoge (main) $ rails routes |grep admin
                       new_admin_session GET    /admins/sign_in(.:format)               admins/sessions#new
                           admin_session POST   /admins/sign_in(.:format)               admins/sessions#create
                   destroy_admin_session DELETE /admins/sign_out(.:format)              admins/sessions#destroy
                      new_admin_password GET    /admins/password/new(.:format)          admins/passwords#new
                     edit_admin_password GET    /admins/password/edit(.:format)         admins/passwords#edit
                          admin_password PATCH  /admins/password(.:format)              admins/passwords#update
                                         PUT    /admins/password(.:format)              admins/passwords#update
                                         POST   /admins/password(.:format)              admins/passwords#create
               cancel_admin_registration GET    /admins/cancel(.:format)                admins/registrations#cancel
                  new_admin_registration GET    /admins/sign_up(.:format)               admins/registrations#new
                 edit_admin_registration GET    /admins/edit(.:format)                  admins/registrations#edit
                      admin_registration PATCH  /admins(.:format)                       admins/registrations#update
                                         PUT    /admins(.:format)                       admins/registrations#update
                                         DELETE /admins(.:format)                       admins/registrations#destroy
                                         POST   /admins(.:format)                       admins/registrations#create
Command (userのroutepath)
ec2-user:~/environment/hogehoge (main) $ rails routes |grep user
                        new_user_session GET    /users/sign_in(.:format)                users/sessions#new
                            user_session POST   /users/sign_in(.:format)                users/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)               users/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)           users/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)          users/passwords#edit
                           user_password PATCH  /users/password(.:format)               users/passwords#update
                                         PUT    /users/password(.:format)               users/passwords#update
                                         POST   /users/password(.:format)               users/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                 users/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                users/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                   users/registrations#edit
                       user_registration PATCH  /users(.:format)                        users/registrations#update
                                         PUT    /users(.:format)                        users/registrations#update
                                         DELETE /users(.:format)                        users/registrations#destroy
                                         POST   /users(.:format)                        users/registrations#create

これで、管理者と会員で別々のcontrollerを利用した処理の記載ができるようになりました。

7.デフォルトのビューを削除

デフォルトで作成されるdeviseのビューは必要ないので削除

Console
rails d devise:views

まとめ

Gemfileの使い方について、ザックリと機能に関する用語を調べて、詳しくは検索機能を用いてGithubのReadmeを確認するのが一番早そうだと思いました。

見づらいや誤っている記載等あればコメント頂けると助かりますm(__)m

参考サイト

「devise」で「管理者(admin)」と「会員(customer)」権限を分けて認証機能を構築!
Devise(Github)
メソッド: ActionDispatch::Routing::Mapper#devise_for

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?