Commit e11348ad authored by Thomas Frion's avatar Thomas Frion

[CLEAN] Better version of the solution to the synchronization problem

parent e848bafa
......@@ -23,16 +23,17 @@ export class TokenApi extends ApiService<models.Token> {
return this.create('/token/', JSON.stringify(user), headers);
}
public refresh(token : models.Token, headers?:HttpHeaders) : Promise<models.TokenRefresh> {
public async refresh(token : models.Token, headers?:HttpHeaders) : Promise<models.TokenRefresh> {
if(headers !== undefined){
this.defaultHeaders = headers;
}
return this.http.post<models.TokenRefresh>(
const restult = await this.http.post<models.TokenRefresh>(
this.baseUrl + '/token/refresh',
JSON.stringify({refresh: token.refresh}),
{headers: this.defaultHeaders}
).toPromise();
return restult;
}
public getUserFromJWT(token: string, headers?: HttpHeaders) : Promise<models.PghmUser>{
......
......@@ -24,8 +24,16 @@ export class AuthGuard implements CanActivate {
}
}
getToken() {
return this.authService.getToken();
getToken() : string {
let res : string;
this.authService.getToken().then(
(r) => {
res = r;
}
)
return res;
}
}
......@@ -45,7 +45,7 @@ export class AuthService {
CookieService.setCookie(
this.tokenName.access,
res.access,
{seconds: 5} //{minutes: this.accessTokenLifeTime}
{minutes: this.accessTokenLifeTime}
);
CookieService.setCookie(
......@@ -86,17 +86,14 @@ export class AuthService {
* @returns true if we are connected false otherwise.
*/
loggedIn() : boolean {
let token = this.getToken();
console.log(token);
return token != null;
return this.getToken() != null;
}
/**
* We get the token reach the API
* @returns the access token
*/
async getToken() {
async getToken() : Promise<string> {
const token : Token = this.getActiveToken();
if(token.access != null){
return token.access;
......@@ -110,11 +107,6 @@ export class AuthService {
);
return newToken.access;
}
// newToken.then((res) => {
// return res;
// }).catch((err) => {
// this.router.navigate(['auth']);
// });
} else
this.router.navigate(['auth']);
}
......
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