jeudi 10 octobre 2019

HttpErrorResponse Unauthorized (401) Angular 8 With Laravel 5.8 and Passport Authintication

I have create API on laravel 5.8 using passport Auth. I have the following API routes on laravel :

// private routes
Route::middleware('auth:api')->group(function () {
    Route::get('/getUser', 'Api\AuthController@getUser')->name('getUser');
});

// public routes
Route::post('/login', 'Api\AuthController@login')->name('login.api');
Route::post('/register', 'Api\AuthController@register')->name('register.api');

I test my routes using postman and that works! but when I have call the routing from angular 8, the public routes works, but the private route return the error 401 (Unauthorized).

The error from console

the following code for call the routes in angular:

import { Headers, RequestOptions } from '@angular/http';
import { HttpClient } from '@angular/common/http'; 

headers: Headers = new Headers();
constructor(private http: HttpClient) {}

getUser(accessToken) {
      this.headers.append('Accept','application/json');      
      this.headers.append('Authorization', 'Bearer ' + accessToken);
      this.options = new RequestOptions({headers: this.headers});
      return this.http.get(this.urlServer, this.options);
    }

I have tried a lot of way to solve this problem and see all tickets here, but the issue not solved yet.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire