railsでdeviseを用いてメール認証、ユーザー登録を実装する際の手順をまとめました。
公式
https://github.com/heartcombo/devise
devise導入、初期設定
まずは下記をGemfileに記載し、gemをinstall
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の設定
開発環境ではmailerのdefaulturlをlocalにしますよというもの。
config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
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
メール設定
メール認証する際に送るメールのアドレスを設定します。今回はgmailに独自ドメインを設定する前提で進めます。
gmailに独自ドメイン設定
gmailアカウントに独自ドメイン設定。これはどこでドメイン取得しているかによって変わってくるのですが、例えばムームードメインだと下記。
Gmailの設定
https://support.muumuu-domain.com/hc/ja/articles/4407628959635-Gmail%E3%81%AE%E8%A8%AD%E5%AE%9A
workplace経由で独自ドメインを設定する場合はworkplaceで下記を設定
- MXレコードの設定
- TXTレコードを設定
MXレコードはgoogleからは2023年4月以降1つだけでいいとでたが、この一つのレコードではだめだった。この原因は分からないが4月以前の5つのレコードを入れる形で通った。
Google Workspace の MX レコードの値
https://support.google.com/a/answer/174125?hl=ja
gmailのsmtp設定
gmailは下記の設定が必要になる
- 2段階認証がONでないとアプリパスワードが発行できないのでONに
- アプリパスワードを発行
confirmableを追加
app/models/user.rb
class User < ActiveRecord::Base
devise :confirmable
end
mailadressをconfigに設定
config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail.com',
:user_name => "infohogehoge@gmail.com", #gmailアドレス
:password => ENV["MAIL_ADDRESS_PASS"], #gmailパスワード
:authentication => 'plain',
:enable_starttls_auto => true
}
rails7で多少違うためrails6の人は注意が必要。細かい解説は下記。
config.action_mailer.delivery_method = :smtp
この行は、RailsのActionMailerが電子メールを送信するためのデリバリメソッド(送信方法)をSMTP(Simple Mail Transfer Protocol)に設定しています。これは、メールを送信するための一般的なプロトコルです。
config.action_mailer.raise_delivery_errors = true
この行は、メールの送信に失敗した場合にエラーを発生させるようにRailsに指示しています。true
に設定すると、メールの送信に何らかの問題があった場合(例えば、SMTPサーバーへの接続が拒否された場合)に、Railsはエラーを発生させます。
config.action_mailer.perform_deliveries = true
この行は、実際にメールを送信するかどうかを設定します。true
に設定すると、メールが実際に送信されます。これはテスト環境では通常false
に設定され、開発や本番環境ではtrue
に設定されます。
-
:address => "smtp.gmail.com"
: メールを送信するためのSMTPサーバーのアドレスを設定しています。 -
:port => 587
: SMTPサーバーへの接続に使用するポートを設定しています。587は一般的なメール送信用のポートです。 -
:domain => 'gmail.com'
: メールの送信元として使用するドメインを設定しています。ここでは、Gmailを使用しています。 -
:user_name => "infohogehoge@gmail.com"
: SMTPサーバーへの認証に使用するユーザー名(ここではGmailアドレス)を設定しています。 -
:password => ENV["MAIL_ADDRESS_PASS"]
: SMTPサーバーへの認証に使用するパスワードを設定しています。ここでは環境変数を使ってパスワードを設定しています。これは、パスワードをコード内に直接書くことなくセキュリティを保つための一般的な方法です。 -
:authentication => 'plain'
: 使用するSMTP認証のタイプを設定しています。plain
は一般的な認証タイプです。 -
:enable_starttls_auto => true
: SMTPサーバーへの接続にSTARTTLSを自動的に使用するように設定しています。STARTTLSは、接続を暗号化し、メールの送信をより安全にするためのプロトコルです。
メールアドレスの設定
devise.rb
config.mailer_sender = 'メールアドレス'