Laravel - Authentication - Closure Request Guards

The simplest way to implement a custom, HTTP request based authentication system is by using the Auth::viaRequest method. This method allows you to quickly define your authentication process using a single closure.

To get started, call the Auth::viaRequest method within the boot method of your AuthServiceProvider. The viaRequest method accepts an authentication driver name as its first argument. This name can be any string that describes your custom guard. The second argument passed to the method should be a closure that receives the incoming HTTP request and returns a user instance or, if authentication fails, null:

    use App\Models\User;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
     * Register any application authentication / authorization services.
     * @return void
    public function boot()
        Auth::viaRequest('custom-token', function (Request $request) {
            return User::where('token', $request->token)->first();

Once your custom authentication driver has been defined, you may configure it as a driver within the guards configuration of your auth.php configuration file:

    'guards' => [
        'api' => [
            'driver' => 'custom-token',