I am working with Laravel Sanctum for the first time, especially with the API's. I had to create an API that is only for reporting that returns a JSON, the topic that I can't access by POSTMAN since it tells me "message": "Unauthenticated."
I followed the steps for the cofiguration that the documentation in my project told me.
I followed the steps for the cofiguration that the documentation told me in my project, which was to install sanctum, run the provider, create the token migration, reference in the user sanctum model and place HasApiTokens, at this point I don't know what to do anymore.
On one hand, the documentation tells me that I can access by placing this line token = $user->createToken('token-name');
but I don't know where to place it, because it won't have an interface since the purpose is to access through PowerBi
This is my route API
Route::group(['middleware'=> 'auth:sanctum'], function (){
Route::get('/reports','Api\ReportController@getTicketsCotizados');
});
My Controller
public function __construct()
{
$this->middleware('auth');
}
public function getTicketsCotizados(){
$this->authorize('view ticket');
$cotizados =Ticket::ticketWithLeadForStatus(3)->get();
return response()->json($cotizados, 200);
}
Kernel
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
User
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use Notifiable, UsesTenantConnection, HasApiTokens, HasRoles;
(...)
}
Guards
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
'hash' => false,
],