mercredi 19 décembre 2018

API with Passport Validation and Error Handling

Im having some issues regarding validating the requests, laravel Route api.php is acting like Route web.php.

My api routes is:

Route::post('login', 'PassportController@login');
Route::post('register', 'PassportController@register');

Route::middleware('auth:api')->group(function () {
    //USERS
    Route::get('get-details', 'UserController@getDetails');
});

I configured all good passport has the documentation told me, and works fine, but now im testing my request where is not included the token to validate and error handling.

My Route:

 http://localhost:8000/api/get-details

My User Controller:

 public function getDetails()
    {

        if(!Auth::check())
        {
            return response()->json(['error' => 'UnAuthorised'], 401);
        }
        //Get Auth User
        $user = Auth::user();

        //Return 404 if user not found
         if(is_null($user)){
             return response()->json(null,404);
         }
         return response()->json($user, 200);
    }

Than using Postman or other tool, i added my route iwthout the token to than case if the token is not included give me a json error message. But insted gives me this error:

InvalidArgumentException
Route [login] not defined.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire