0

I am working on a Login with Laravel Passport to later consume this API in Angular and i find the following problem

When I throw a GET in Postman to the following path http://127.0.0.1:8000/api/tramites with the Accept: application / json headers and my Authorization token: Bearer xxxx it returns me

"message": "Unauthenticated."

I do not understand why this happens since I am passing the generated user token, it would have to access.

api.php

Route::group(['middleware' => 'auth:api'], function(){
Route::ApiResource('tramites', 'TramiteController');});

PersonaController.php

public function login(Request $request)
    {
        $persona = Persona::whereUsuario($request->Usuario)->first();
        if(!is_null($persona) && ($request->Contrasena == $persona->Contrasena))
        {
            
            $persona->App_Id = Str::random(100);
            $persona->save();

            return response()->json([
                'res' => true,
                'token' => $persona->App_Id,
                'message' => 'Login succesfully'
            ], 200);
        }
        else{
            return response()->json([
                'res' => false,
                'message' => 'Login wrong'
            ], 400);
        }
        
    }
Elio Farac
  • 11
  • 2
  • 1
    may i know how what headers u r passing – Kamlesh Paul Sep 03 '20 at 11:52
  • @KamleshPaul I am using postman, therefore the default headers that also come in it, and Accept: application/json and Authorization with a value "Bearer xxxxxxx" – Elio Farac Sep 03 '20 at 12:00
  • 2
    Welcome to SO ... have you changed your `api` guard to use the `passport` driver? – lagbox Sep 03 '20 at 12:02
  • @lagbox Thanks! Yes, I modified auth.php correctly, or so I think haha – Elio Farac Sep 03 '20 at 12:30
  • if you had cached your configuration at some point you will want to clear that `php artisan config:clear` to make sure it picks up the changes in the config files (run this command either way just in case) – lagbox Sep 03 '20 at 14:25

1 Answers1

0

Check if your .htaccess file has this directive:

RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

I had a similar problem: Laravel sanctum unauthenticated

Marco Cazzaro
  • 661
  • 8
  • 13