When we develop new application with Laravel 8 or Laravel 9, We encounter “Auth guard api is not defined” which is a common issue we face while building Laravel applications. It is mostly found in Laravel 8/9 applications. We can fix this issue by following the simple steps shown below.
Step 1: Edit / Update config/auth.php
Open the auth.php file in the config directory of your Laravel project. In the code of auth.php file, look for the ‘guards
‘ array. You will see two guards, one is web and another is api. Inside the api array, make sure hash is false in case if driver => ‘token’. If driver is passport i-e driver => ‘passport’, then we should make hash => true. Consider the code snippet below.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => true,
],
],
The guard configuration of the auth.php file should be as shown above. You may be facing the error if the ‘api
‘ section inside the guard array is not present.
Next, you need to clear your application cache.
Step 2: Clear Cache
You may now clear your application’s cache by passing the command below in your application’s terminal.
php artisan config:clear
Clearing cache helps reflect the changes we make to the application configuration files which are either inside config directory or in .env file.
Please take note if your active development server is running, you will have to restart your application to reflect the changes.
Therefore, by simply assuring the presence of ‘api’ guard in your auth.php file, we can fix the auth guard api is not defined error in Laravel