'This action is unauthorized in laravel 8

I'm using spatie/laravel-permission and when I add my permission to my api route, I'm getting

message: "This action is unauthorized.

but my user does have permission

My route

Route::get('/customer/items', [ CustomerController::class, 'getItems'])->middleware('can:customer');

My CustomerController

public function getItems()
{
    $items = Item::all();

    return [
        'items' => $items
    ];
}


Solution 1:[1]

I was this problem because my guard was auth:api

Route::prefix('coin')->middleware(['auth:api','can:manageCoins'])->group(function () {
    Route::get('', [CoinController::class, 'getAllPagination']);
});

in role table and permission table filed guard_name = web

I change all guard_name to api

fixed it middleware can work for guard api

note : run this command php artisan optimize:clear

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mohammad nazari