Commit e848bafa authored by Thomas Frion's avatar Thomas Frion

[FIX] Bug when we refresh the token

* Synchronization problem
parent e10eaf82
......@@ -24,7 +24,7 @@ export class AuthGuard implements CanActivate {
}
}
getToken() : string {
getToken() {
return this.authService.getToken();
}
......
......@@ -45,7 +45,7 @@ export class AuthService {
CookieService.setCookie(
this.tokenName.access,
res.access,
{minutes: this.accessTokenLifeTime}
{seconds: 5} //{minutes: this.accessTokenLifeTime}
);
CookieService.setCookie(
......@@ -96,22 +96,25 @@ export class AuthService {
* We get the token reach the API
* @returns the access token
*/
getToken() : string{
async getToken() {
const token : Token = this.getActiveToken();
if(token.access != null){
return token.access;
}else if(token.refresh != null){
const newToken : Promise<TokenRefresh> = this.tokenApi.refresh(token);
newToken.then((res) => {
const newToken = await this.tokenApi.refresh(token);
if(newToken && newToken.access){
CookieService.setCookie(
this.tokenName.access,
res.access,
newToken.access,
{minutes: this.accessTokenLifeTime}
);
return res.access;
}).catch((err) => {
this.router.navigate(['auth']);
});
return newToken.access;
}
// newToken.then((res) => {
// return res;
// }).catch((err) => {
// this.router.navigate(['auth']);
// });
} else
this.router.navigate(['auth']);
}
......
/**
* @interface :
* Model expected from API when we try to log in
*/
export interface Token {
refresh: string;
access: string;
}
\ No newline at end of file
......@@ -9,6 +9,11 @@ export class CookieService {
public static setCookie(name: string, value: string, option: any) : void {
let date = new Date();
if(option.seconds){
date.setSeconds(date.getSeconds() + option.seconds);
}
if(option.minutes){
date.setMinutes(date.getMinutes() + option.minutes);
}
......
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