I'm trying to use the laravel passport to authenticate users. I followed every step in laravel passport documentation. But it shows error when I access a route that is protected by 'auth:api' middleware. I've searched over the internet but any of the answers are not working so far. Please see the code, and what I've tried so far below.
Tech used
- Laravel 6
- Laravel Passport
- Vue 2
- Axios
Tried so far:
- Changed
Passport::routes();toPassport::routes(null, ['prefix' => 'api/oauth', 'middleware' => ['auth:api', 'web', 'auth']]);inAuthServiceProvider. Also triedPassport::routes(null, ['prefix' => 'api/oauth'); - Tried to run
php artisan cache:clear
routes/api.php
Route::middleware('auth:api')->group(function() {
Route::get('user', function() {
return request()->user();
});
Route::get('posts', 'PostController@index')->name('posts');
Route::post('posts/store', 'PostController@store')->name('posts.store');
Route::get('posts/{id}/show')->name('posts.show');
Route::get('users/{id}/show')->name('users.show');
});
Axios
mounted() {
axios.get('/api/posts')
.then(res => this.posts = res.data).catch(error => console.log('Unable to fetch posts.'))
}
Error

Headers:
Please let me know if you need some details that is not indicated on my post yet.
Setup
- Run
composer require laravel/passport:9for the installation. - Run
php artisan migratefor the tables. - Run
php artisan passport:installto generate the encryption keys. - Added
Laravel\Passport\HasApiTokenstrait inUsermodel. - Added
Passport::routes();inAuthServiceProvider. - Changed
guard'sapidriver topassportinconfig/auth.php.
Full source code:

