0
votes

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:

enter image description here

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.

1
Yes, i m sure it's running. it gives me back response if i remove the token from the request header - Newgenbie
where is employees route? which you calling you have only defined details route of user which is a post request - Salman Zafar
it should be Route::get('/user/details', 'API\UserController@details'); and in postman http:\\siteurl\api\user\details - Salman Zafar
still not working if i change the request type. And the image i use is redirected from other post. So it's not correctly representing my path. But i am the url i m using i correct - Newgenbie

1 Answers

0
votes

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