Laravel Auth related

/config/auth.php

Config different “guard” for authentication and each of them has a “provider”. This provider map to either a database table or eloquent model.

Example:

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\UserM::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

Usage:

$credentials = [
	'account' => $account, 
	'password' => $password
];
//logg(Hash::make($password));

//This script will validate credentials and save session
if(Auth::guard('admin')->attempt($credentials)){
    return redirect('/admin');
}

Auth::routes() 流程

https://www.kancloud.cn/hongjh/laravel/288380

Assigning Middleware To Routes

// Within App\Http\Kernel Class...

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

Usage:

Route::get('admin/profile', function () {
    //
})->middleware('auth');

Route::get('/', function () {
    //
})->middleware('web');

Route::group(['middleware' => ['web']], function () {
    //
});

Route::put('post/{id}', function ($id) {
    //
})->middleware('role:editor');

In controller

//guest is an route middleware and will redirect authenticated user to another uri
//"vender" after "guest:" is a parameter for the middleware
//'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class
$this->middleware('guest:vendor', ['except' => 'logout']);

//This script will use guard 'admin' to verify user.
public function __construct(){
	$this->middleware('auth:admin');
}