Auth guard api is not defined is a common issue we may encounter while building Laravel applications. It is mostly found in Laravel 8 applications. We can fix this issue by following the simple steps shown below.
Step 1: Edit / Update auth.php
Open the auth.php file in the config directory of your Laravel project. Look for the ‘
guards‘ array in the auth file, and check for the ‘
api‘ guard, inside it. Consider the code snippet below.
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ],
The guard configuration of the auth.php file should be as shown below. 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
This helps reflect changes in your application
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