Skip to content

Commit

Permalink
fixed token refresh flow (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhinkie authored Aug 9, 2022
1 parent 06ab45e commit 7f6073e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/app/authentication/_interceptor/token.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export class TokenInterceptor implements HttpInterceptor {
}

private addToken(request: HttpRequest<any>): HttpRequest<any> {
const token = TokenInterceptor.authService.getAccesToken()
if (token == null) {
return request;
} else {
if (TokenInterceptor.authService.isAuthenticated()) {
const token = TokenInterceptor.authService.getAccessToken();
return request.clone({ setHeaders: { Authorization: `Bearer ${token}` } });
} else {
return request;
}
}
}
22 changes: 13 additions & 9 deletions src/app/authentication/_services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class AuthenticationService {
private initSubjectsPipe() {
this.userSubject = new BehaviorSubject<UserInfoModel>(null);
this.rolePASubject = new BehaviorSubject<UserRole[]>(null);
this.accessTokenSubject = new BehaviorSubject<string>(this.getAccesToken());
this.accessTokenSubject = new BehaviorSubject<string>(this.getAccessToken());

this.accessTokenSubject.subscribe(token => {
if (token === 'logout') {
Expand All @@ -54,7 +54,7 @@ export class AuthenticationService {
this.getRoles();
this.router.navigate(['/']);

} else if (token && this.getRefreshToken() && this.jwtHelper.isTokenExpired(this.getAccesToken())) {
} else if (token && this.getRefreshToken() && this.jwtHelper.isTokenExpired(this.getAccessToken())) {
this.refreshToken();

} else {
Expand Down Expand Up @@ -184,7 +184,7 @@ export class AuthenticationService {
window.open(environment.logoutUrl + params, '_self');
}

public getAccesToken(): string {
public getAccessToken(): string {
return localStorage.getItem(localAccessTokenKey);
}

Expand All @@ -193,13 +193,17 @@ export class AuthenticationService {
}

public isAuthenticated(): boolean {
if (!this.jwtHelper.isTokenExpired(this.getAccesToken())) {
return true;
} else if (!this.jwtHelper.isTokenExpired(this.getRefreshToken())) {
this.refreshToken();
return true
if(this.getAccessToken() || this.getRefreshToken()) {
if (!this.jwtHelper.isTokenExpired(this.getAccessToken())) {
return true;
} else if (!this.jwtHelper.isTokenExpired(this.getRefreshToken())) {
this.refreshToken();
return true;
} else {
this.logout();
return false;
}
} else {
this.logout();
return false;
}
}
Expand Down

0 comments on commit 7f6073e

Please sign in to comment.