Guard in Laravel

When you are using Laravel if you look at the built-in authentication controllers you will notice that they are using guards. Guards instruct Laravel to store and retrieve information about users. The configuration can be found in config/auth.php. The API guards use tokens in the header or query parameter to authenticate users and requests. You can define your custom guard using the extend method on the facade. You have to place the call to extend in the service provider i.e. AuthServiceProvider.

<?php 
namespace App\Providers;

 

use App\Services\Auth\JwtGuard;

use Illuminate\Support\Facades\Auth;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

 

class AuthServiceProvider extends ServiceProvider

{

    /**

     * Register any application authentication / authorization services.

     *

     * @return void

     */

    public function boot()

    {

        $this->registerPolicies();

        Auth::extend('jwt', function ($app, $name, array $config) {

            // Return an instance of Illuminate\Contracts\Auth\Guard...

            return new JwtGuard(Auth::createUserProvider($config['provider']));

        });

    }

}

Refer Laravel docs to know more about creating custom guards.