LoginSignup
1
2

More than 3 years have passed since last update.

【Rails】deviseでの機能制限(ログイン・ログアウト)

Posted at

deviseでの機能制限(ログイン・ログアウト)

目次

  1. ログイン・ログアウト機能のみ利用したい
  2. deviseでのデフォルト機能について
  3. 機能を制限する

1. ログイン・ログアウト機能のみ利用したい

私は予約管理ができるアプリを作成していました。(ユーザーは会員登録不要)
管理者画面では、権限のあるユーザーのみ利用できるようにするため、gem 'devise'を利用し認証機能を作成することに決めました。
devise関連のインストールと設定を完了後、利用できる機能を確認したところ、不要な機能(ユーザー登録・パスワード設定、etc..)が多いことに気づきました。
管理者画面は基本的に2人しか利用しないので、2人のユーザー情報だけをアプリ作成時に設定しました。(rails console もしくはSQLで設定)

2. deviseでのデフォルト機能について

module 機能
database_authenticatable DBに保存するパスワードの暗号化(この機能が無いとユーザー登録ができません)
registerable サインアップ処理
recoverable パスワードリセット
rememberable クッキーにログイン情報を保持
trackable サインイン回数・時刻・IPアドレスを保存
validatable メールアドレスとパスワードのバリデーション

デフォルトではtrackableを除いた機能が有効になっています。

app/models/user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable :validatable
end

3. 機能を制限する

今回利用する機能は主に「ログイン・ログアウト」のみなので以下のように変更を行います。
registerable サインアップ処理 (不要機能)
recoverable パスワードリセット(不要機能)

app/models/user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :rememberable, :validatable
end

確認のため、ターミナルでrake routesでルーティングを確認します。

    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

「ログイン・ログアウト」時に利用するルーティングのみになっています。
※ viewファイルなどは削除するのか残しておくのか適宜判断するようにしましょう。
以上でRailsにおいてのdeviseでの機能制限となります。

【Rails】deviseでのセッションタイムアウト設定

1
2
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
1
2