Commit 27732f8a authored by Thomas Frion's avatar Thomas Frion

[FIX] Bug with refresh token from api client

The probleme was the api call was made before the promise finish
parent 12628318
......@@ -69,8 +69,8 @@ export class MapComponent implements OnInit {
this.marker = L.marker(coordinates, {icon: this.pghmIcon});
this.marker.bindPopup("<b>" + this.unit.name + "</b> (" + this.unit.code + ") <br>"+
"<a href='mailto:" + this.unit.email + "'>" + this.unit.email + "</a><br><br>" +
"<b>Lat: </b>"+ coordinates[0] +", <b>Lon: </b>" + coordinates[1] + "<br>" +
MapComponent.ddToDMS(coordinates[0],coordinates[1]));
MapComponent.ddToDMS(coordinates[0],coordinates[1]) + "<br>" +
"<b>Lat: </b>"+ coordinates[0] +", <b>Lon: </b>" + coordinates[1]);
this.getGendarmeries();
}
......
......@@ -21,12 +21,6 @@ export abstract class ApiService<T> {
this.defaultHeaders = new HttpHeaders({
'Content-Type':'application/json',
});
if(authGuard !== undefined){
this.authGuard.getToken().then((token) => {
this.defaultHeaders = this.defaultHeaders.append('Authorization', 'Bearer ' + token);
});
}
}
/**
......@@ -34,10 +28,12 @@ export abstract class ApiService<T> {
* @param resource The resource name. Ex: .../myResources/
* @param headers HttpHeaders a custom HTTP header
*/
protected get(resource : string, headers? : HttpHeaders) : Promise<T[]> {
protected async get(resource : string, headers? : HttpHeaders) : Promise<T[]> {
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.get<T[]>(
this.baseUrl + resource,
{headers: this.defaultHeaders}
......@@ -49,9 +45,11 @@ export abstract class ApiService<T> {
* @param resource The resource name. Ex: .../myResources/id-of-my-resource
* @param headers HttpHeaders a custom HTTP header
*/
protected read(resource : string, headers? : HttpHeaders) : Promise<T> {
protected async read(resource : string, headers? : HttpHeaders) : Promise<T> {
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.get<T>(
this.baseUrl + resource,
......@@ -65,9 +63,11 @@ export abstract class ApiService<T> {
* @param params Element to create in the database
* @param headers HttpHeaders a custom HTTP header
*/
protected create(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
protected async create(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.post<T>(
this.baseUrl + resource,
......@@ -82,9 +82,11 @@ export abstract class ApiService<T> {
* @param params Element to save
* @param headers HttpHeaders a custom HTTP header
*/
protected update(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
protected async update(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.put<T>(
this.baseUrl + resource,
......@@ -118,4 +120,8 @@ export abstract class ApiService<T> {
return res;
}
protected async authHeader() {
this.defaultHeaders = this.defaultHeaders.set('Authorization', 'Bearer ' + await this.authGuard.getToken());
}
}
......@@ -16,17 +16,16 @@ export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, public router: Router) { }
canActivate(): boolean{
return true; // To debug
// if(!this.authService.loggedIn()) {
// this.router.navigate(['auth']);
// return false;
// } else {
// return true;
// }
if(!this.authService.loggedIn()) {
this.router.navigate(['auth']);
return false;
} else {
return true;
}
}
async getToken() : Promise<string> {
return await this.authService.getToken();
getToken() : Promise<string> {
return this.authService.getToken();
}
}
......@@ -114,6 +114,7 @@ export class AuthService {
newToken.access,
{minutes: this.accessTokenLifeTime}
);
return newToken.access;
}
} else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment