A Laravel Guard defines how users are authenticated for each request. It determines the method of authentication, such as session-based or token-based, and integrates with authentication drivers like web, API, or custom implementations.
Guards are part of Laravel's authentication system, designed to handle diverse authentication scenarios.
config/auth.php
for clarity.Define guards in config/auth.php
:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
Use a guard for authentication:
if (Auth::guard('api')->check()) {
return response('Authenticated', 200);
}
Laravel Guards handle diverse authentication scenarios, improving flexibility and security in applications.