電話番号必須
まず、登録時に電話番号の入力を要求することから始めましょう。新しいデータベースの移行を生成することでこれを行います:
を編集する。 db/migrate/..._add_phone_number_to_users.rb ファイルに新しいカラムを追加する。 user モデルである:
class AddPhoneNumberToUsers < ActiveRecord::Migration
def change
add_column :users, :phone_number, :string
end
end
を実行して変更を適用する:
Deviseには、編集が必要なテンプレートのコピーを作成するためのRailsジェネレータが用意されています。ジェネレータを実行するには rails generate:devise:views:templates.
ただし、サンプル・アプリケーションでは devise-bootstrap-templates gemの場合は、別のバージョンのジェネレーターを使う必要がある:
これは、複数のビューテンプレートを app/views/deviseしかし、あなたが興味を持っているのは app/views/devise/registrations/edit.html.erb残りは削除してください。
次に、編集テンプレートを修正し、Eメールフィールドの直後に電話番号を入力するフィールドを追加します:
<div class="form-group">
<%= f.label :phone_number %> <i>(Leave blank to disable two factor authentication)</i><br />
<%= f.number_field :phone_number, class: "form-control", placeholder: "e.g. 447555555555 or 1234234234234" %>
</div>
最後に、Deviseにこの追加パラメーターを認識させる必要がある:
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :authenticate_user!
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:phone_number])
end
end
アカウントに電話番号を追加するには、以下を実行します。 rails serverに移動する。 http://localhost:3000/ をクリックし、前のステップで登録したアカウント情報を使用してログインします。
画面右上のEメールアドレスをクリックし、電話番号と登録時のパスワードを入力して「更新」をクリックします。これであなたの電話番号がデータベースに保存されます。
セキュリティとスパム防止のための二要素認証
Rubyアプリケーションに2faを実装する方法を学ぶ