0
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 3 years have passed since last update.

【Ruby on Rails】deviseを用いたユーザー認証機能の作成

Last updated at Posted at 2021-01-06

アプリケーションに"devise"を読み込ませる

Gemfile
:
gem 'devise' #追加
terminal
$ bundle install

deviseコマンドでモデル、ビュー、コントローラを作成

terminal
$ rails g devise:install
$ rails g devise User name:string #モデルの作成
$ rails db:migrate
$ rails g devise:views users #ビューの作成
$ rails g devise:controllers users #コントローラの作成

rails g devise "model名" で作成したモデルには初期値ではnameカラムが存在しない為、上記のようにカラムを同時に作成するか、migrationファイルに記述してテーブルに反映させる。

・作成されたモデル

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

デフォルトの機能

  • database_authenticatable(パスワードの正確性を検証)
  • registerable(ユーザー登録や編集、削除)
  • recoverable(パスワードをリセット)
  • rememberable(ログイン情報を保存)
  • validatable(emailのフォーマットなどのバリデーション)

ルーティングの変更

config/routes.rb
...編集
devise_for :users # devise機能の使用の際にURLにusersを含むことの宣言

devise_for :users, controllers: { 
  sessions: 'users/sessions', #usersディレクトリ下のsessionsコントローラを使用
  registrations: 'users/registrations' #usersディレクトリ下のregistrationsコントローラを使用
}

上記のcontrollers: {}指定によりルーティング設定が以下のように変更される

terminal
devise/sessions#create
users/sessions#create

ビューページの編集

新規登録画面に名前の入力フォームを追加

app/views/users/registrations/new.html.erb
... +の部分を追加
+ <div class="field"> 
+  <%= f.label :name %><br />
+  <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
+ </div>

<div class="field">
  <%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
:

ログイン画面の入力フォームをemailからnameに変更
これによりemailでのログイン認証へと変更させる

app/views/users/sessions/new.html.erb
...編集
:
 <div class="field">
   <%= f.label :email %><br />
   <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
 </div><div class="field">
   <%= f.label :name %><br />
   <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
 </div>
:

コントローラの編集

app/controllers/application_controller.rb
...追加
before_action :configure_permitted_parameters,if: :devise_controller? 

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up,keys:[:name])
end

devise利用の機能(ユーザ登録、ログイン認証など)が使われる場合、その前にconfigure_permitted_parametersが実行

devise_parameter_sanitizer.permit(:sign_up,keys[:name])
= ユーザ登録(sign_up)の際のユーザ名(name)のデータ操作を許可

Strong Parametersと同様の機能
・private = 自分のコントローラ内のみで参照
・protected = 呼び出された他のコントローラからも参照可能

ログアウト機能の実装

app/views/layouts/application.html.erb

...+部分の追加
  <body>
+   <% if user_signed_in? %>
+     <%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
+   <% else %>
+     <%= link_to "新規登録", new_user_registration_path %>
+     <%= link_to "ログイン", new_user_session_path %>
+   <% end %>
:

以上

記載内容に間違い等ございましたらご指摘頂けると幸いです。
ご連絡お待ちしております。

0
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
0
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?