I'm working on a Laravel API and using Postman to test it,
- I have a route /api/login that authenticates the user and return a token.
Route::post('login', [ApiAuthenticationController::class, 'login']);
The Login Method:
class ApiAuthenticationController extends Controller
{
/**
* Api user login
*
* @param Request $request
* @return JsonResponse
*/
public function login(Request $request): \Illuminate\Http\JsonResponse
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
$user = Auth::user();
$token = $user->createToken('token')->accessToken;
return response()->json([
'token' => $token,
]);
}
return response()->json([
'error' => 'Invalid username or password',
], 401);
}
}
here's the the response:
When I try to send a request to a route let's say: api/v1/admin/proposals with the Barear token that I retrieved, I get 403 forbidden and the message USER NOT LOGGED IN
Route::group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers\Api\V1'], function () {
Route::middleware(["role:admin"])->prefix('admin')->group(function () {
Route::resource('proposals', AdminProposalController::class);
});
});
Note that the user has the role of admin, and I have Laravel/Passport installed.
is there any parameter to change inside postman?