Reputation: 1
I am working with a laravel api, it strictly deals with ajax requests. After successful login, the client will receive a laravel passport token for sequential requests.
The api has this route which simply return the authenticated user's details:
Route::group(['middleware' => 'auth:api'], function() {
Route::post('/user/details', 'API\UserController@details');
});
// in UserController...
public function details() {
$user = Auth::user();
return response()->json([
'user' => $user
], 201);
}
When i use postman to test my api, i get this response if i adding:
{Accept: application/json, Authorization: "Bearer " + LARAVEL_PASSPORT_TOKEN}
to the request header:
but if i only have {Accept: application/json} in the header. It will give me back a normal Json unauthenticated response.
So, it would be great if someone knows what is going on and give me some insight about it.
Thanks in advance.
Upvotes: 0
Views: 1692
Reputation: 359
I think you follow your routes like
Route::get('/login','ApiController@accessToken');
Route::group(['middleware' => ['web','auth:api']], function()
{
Route::post('/todo/','ApiController@store');
Route::get('/todo/','ApiController@index');
Route::get('/todo/{todo}','ApiController@show');
Route::put('/todo/{todo}','ApiController@update');
Route::delete('/todo/{todo}','ApiController@destroy');
});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
And for more information visit article : Create A REST API in Laravel With Authentication Using Passport
Upvotes: 0