【laravel6】ログイン時(auth)に認証条件を追加

laravelのデフォルトでログイン認証は、メールアドレスとパスワードになっている

今回は、それにフラグを追加したかったので、忘れないように記載しておきます!

LoginController

credentials関数をLoginControllerに追加する

今回の条件は、is_deactivetedのフラグが0の場合のみログイン可能

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
     /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     * ログインの条件を追加
     */
    protected function credentials(Request $request)
    {
        $conditions = $request->only($this->username(), 'password');
        $conditions_custom = array_merge(
            $conditions,
            ['is_deactiveted' => '0']
        );
        return $conditions_custom;
    }
}


?>

$conditions → emailカラムとpasswordカラム(laravelのデフォルト)

$conditions_custom → 追加条件を$conditionsの配列に追加

ユーザー名のカスタマイズ

デフォルトでemailが認証に使われているが、ユーザーIDなどに変えたい時

username関数をLoginControllerに追加する

public function username()
{
    return 'user_id';
}

laravel・認証

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA