LoginSignup
0
1

More than 3 years have passed since last update.

rails+deviseでメール認証を導入する

Last updated at Posted at 2018-09-24

この記事の概要

  • rails6
  • devise

でメール認証を作成

devise導入から初期設定まで

deviseの導入

公式github
https://github.com/heartcombo/devise

gem 'devise'

gemを導入し、下記コマンドで設定ファイルを生成

rails g devise:install

下記のコメントが出るのでこのコメントの内容を設定していく

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

Depending on your application's configuration some manual setup may be required:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

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

     In production, :host should be set to the actual host of your application.

     * Required for all applications. *

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

     * Not required for API-only Applications *

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

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

     * Not required for API-only Applications *

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

     * Not required *

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

各種設定

default_url_optionsの設定

config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

開発環境ではmailerのdefaulturlをlocalにしますよというもの。

rootパスの設定

config/routes.rb
root "home#index"

flashメッセージ

app/views/layouts/application.html.erb
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

viewの作成

rails g devise:views

上記でviewを作成。細かいviewの調整は各ファイルで。日本語化も必要。

userモデルの作成

rails g devise user

userはmodel名。これでuserモデルができる。

メールによる認証をする場合は下記のコメントアウトを外す

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

それぞれの意味は下記がわかりやすい
https://qiita.com/ozackiee/items/21fcad4a1564136b9510#%E3%83%9E%E3%82%A4%E3%82%B0%E3%83%AC%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E7%B7%A8%E9%9B%86

カスタマイズ

deviseにないカラムを追加する際

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?
  protect_from_forgery with: :exception

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: %i(name self_introduction sex img_name))
  end
end

上記のようにdeviseにストロングパラメーターの許可を与えないといけない。

各viewfileでデザインを変更する

deviseを導入するとviews/deviseにviewfileが自動で置かれるのでここを編集してデザインを変えていく。

独自ドメインメールの設定

参考資料
https://peraichi.com/univ/52

上記でムームードメインで独自ドメインのメールアドレスをとり、gmailで取得、送信できるようにする。下記がドメイン周りでやったこと。

  • 2段階認証をONに
  • アプリパスワードの設定

deviseのメール設定

上記をもとに下記の手順で

confirmableを追加

app/models/user.rb
class User < ActiveRecord::Base
devise :confirmable
end

mailadreesをconfigに設定

config/environments/development.rb
  config.action_mailer.smtp_settings = {
    :enable_starttls_auto => true,
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => 'gmail.com',
    :user_name => "yourmail", #gmailアドレス
    :password => ENV["MAIL_ADDRESS_PASS"], #gmailパスワード
    :authentication => 'login'
  }
  • passはdotenvで管理。gmailパスワードはアプリパスワード。
  • user_nameはgmailにログインしているメールアドレス
  • portは587。gmailでの設定は465でもここは587になる。

送信元のメールアドレスの設定

devise.rb
config.mailer_sender = 'メールアドレス'

上記の記述をして送信する際のfromに相当するアドレスを設定。

で確認すると登録画面に飛べるはず

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