Laravel9でGoogle Authenticatorを使用して二段階認証(2FA)を導入する手順

Laravel9でGoogle Authenticatorを使用して二段階認証(2FA)を導入する手順を以下に示します。ユーザーが後から二段階認証を設定できるようにするための設定も含めて説明します。

1. パッケージのインストール

まず、`pragmarx/google2fa`パッケージをインストールします。

composer require pragmarx/google2fa

2. ユーザーテーブルの変更

ユーザーテーブルに二段階認証用のフィールドを追加します。

php artisan make:migration add_2fa_fields_to_users_table --table=users

マイグレーションファイルに以下のコードを追加します:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('google2fa_secret')->nullable();
        $table->boolean('is2fa_enabled')->default(false);
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('google2fa_secret');
        $table->dropColumn('is2fa_enabled');
    });
}

ここから先は

4,656字

¥ 400

この記事が気に入ったらサポートをしてみませんか?