LoginSignup
25
31

More than 5 years have passed since last update.

Laravel 5.3で認証関連機能のテーブル名を変更する方法

Last updated at Posted at 2016-09-28
  • 以前に書いたLaravel 5.3で認証関連機能を実装する方法では、認証やパスワードリセットに使用されるテーブル名が、それぞれ「users」「password_resets」で固定されている。
  • いやいや。。。変えたいじゃん、テーブル名。ってときは以下の手順で。

変更手順

認証に使用するテーブルのeloquent modelを作成

  • この際、Laravelインストール後に作成されているapp/Users.phpを参考に、基底クラスなどを設定する。

    (your/auth/table/eloquent/model_name.php)
    <?php
    
    namespace (Your\Auth\Table\Eloquent);
    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class (Model_Name) extends Authenticatable
    {
        use Notifiable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    
  • 要は、「Illuminate\Foundation\Auth\User as Authenticatable」を継承して「Illuminate\Notifications\Notifiable」をuse。

  • モデルの作成場所やディレクトリの深さはお好みで。

config/auth.phpを修正

  • config/auth.phpに、認証関連の設定をまとめたものが、providerという単位で記載されている。
  • 認証・パスワードに使用するテーブルを指定したproviderを作成し、それぞれの箇所でこのproviderを使うように変更する。

    config/auth.php
     <?php
    
     return [
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Defaults
        |--------------------------------------------------------------------------
        |
        | This option controls the default authentication "guard" and password
        | reset options for your application. You may change these defaults
        | as required, but they're a perfect start for most applications.
        |
        */
    
        'defaults'  => [
            'guard'     => 'web',
    -       'passwords' => 'users',
    +       'passwords' => '(your-password-reset-provider-name)',
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Guards
        |--------------------------------------------------------------------------
        |
        | Next, you may define every authentication guard for your application.
        | Of course, a great default configuration has been defined for you
        | here which uses session storage and the Eloquent user provider.
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | Supported: "session", "token"
        |
        */
    
        'guards'    => [
            'web' => [
                'driver'   => 'session',
    -           'provider' => 'users',
    +           'provider' => '(your-auth-provider-name)',
            ],
    
            'api' => [
                'driver'   => 'token',
    -           'provider' => 'users',
    +           'provider' => '(your-auth-provider-name)',
            ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | If you have multiple user tables or models you may configure multiple
        | sources which represent each model / table. These sources may then
        | be assigned to any extra authentication guards you have defined.
        |
        | Supported: "database", "eloquent"
        |
        */
    
        'providers' => [
            'users'   => [
                'driver' => 'eloquent',
                'model'  => App\User::class,
            ],
    +       'your-auth-provider-name' => [
    +           'driver' => 'eloquent',
    +           'model'  => (Your\Auth\Table\Eloquent\Model_Name)::class,
    +       ],
    
            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Resetting Passwords
        |--------------------------------------------------------------------------
        |
        | You may specify multiple password reset configurations if you have more
        | than one user table or model in the application and you want to have
        | separate password reset settings based on the specific user types.
        |
        | The expire time is the number of minutes that the reset token should be
        | considered valid. This security feature keeps tokens short-lived so
        | they have less time to be guessed. You may change this as needed.
        |
        */
    
        'passwords' => [
            'users'   => [
                'provider' => 'users',
                'table'    => 'password_resets',
                'expire'   => 60,
            ],
    +       'your-password-reset-provider-name' => [
    +           'provider' => '(your-auth-provider-name)',
    +           'table'    => '(your-password-reset-table-name)',
    +           'expire'   => 60,
    +       ],
        ],
    
     ];
    
    

app/Http/Controllers/Auth/RegistController.phpを修正

  • createメソッドで呼び出されるEloquentモデルクラス名を変更

    app/Http/Controllers/Auth/RegistController.php
    -         return User::create([
    +         return \Your\Auth\Table\Eloquent\Model_Name::create([
    

メール送信サーバ情報の設定

  • パスワードリセット機能では、メール送信によるユーザー確認を行うため、PHPからメールを送るための情報を設定する。
  • .env.xxxxxxを修正

    • config/mail.phpは.env.xxxxxの内容を読み込むので、.env.xxxxxを編集する。

      env.xxxxx
      MAIL_DRIVER=smtp
      MAIL_HOST=(your.smtp.server.name)
      MAIL_PORT=587
      MAIL_USERNAME=(your-mail@user.name)
      MAIL_PASSWORD=(your-secure-mail-password)
      MAIL_ENCRYPTION=null
      MAIL_FROM_ADDRESS=(sender-mail@address)     <-追記
      

確認

  • UI操作しつつ、テーブルレコードやメールをみて動作していることを確認する。
25
31
1

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
25
31