Skip to content

Commit 90a1cca

Browse files
authored
fix: TTA-209 add subscription to login component for redirecting user (#949)
* fix: TTA-209 add subscription to login component for redirecting users when token expires to login page * TTA-209 added redirectioning for any 401 status response
1 parent bb04771 commit 90a1cca

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/app/modules/login/login.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ export class LoginComponent implements OnInit {
6464
ngOnInit() {
6565

6666
this.googleAuthSDK();
67-
if (this.isProduction && this.azureAdB2CService.isLogin()) {
68-
this.router.navigate(['']);
69-
} else {
7067
this.loginService.isLogin().subscribe(isLogin => {
7168
if (isLogin) {
7269
this.router.navigate(['']);
7370
}
7471
});
75-
}
72+
7673
window.handleCredentialResponse = (response) => {
7774
const {credential = ''} = response;
7875
this.featureToggleCookiesService.setCookies();

src/app/modules/shared/interceptors/inject.token.interceptor.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import {
44
HttpInterceptor,
55
HttpHandler,
66
HttpRequest,
7+
HttpErrorResponse,
78
} from '@angular/common/http';
89
import { Observable } from 'rxjs';
10+
import { tap } from 'rxjs/operators';
911

1012
import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';
1113
import { environment } from './../../../../environments/environment';
1214
import { EnvironmentType } from 'src/environments/enum';
1315
import { LoginService } from '../../login/services/login.service';
16+
import { catchError } from 'rxjs/operators';
17+
import { Router } from '@angular/router';
1418

1519
@Injectable()
1620
export class InjectTokenInterceptor implements HttpInterceptor {
1721
isProduction = environment.production === EnvironmentType.TT_PROD_LEGACY;
18-
constructor(private azureAdB2CService: AzureAdB2CService, private loginService: LoginService) { }
22+
constructor(private azureAdB2CService: AzureAdB2CService, private loginService: LoginService, private router: Router) { }
1923

2024
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
2125
if (request.url.startsWith(environment.timeTrackerApiUrl)) {
@@ -25,7 +29,17 @@ export class InjectTokenInterceptor implements HttpInterceptor {
2529
headers: request.headers.set('Authorization',
2630
'Bearer ' + token)
2731
});
28-
return next.handle(requestWithHeaders);
32+
return next.handle(requestWithHeaders)
33+
.pipe(
34+
tap(() => { }, (err: any) => {
35+
if (err instanceof HttpErrorResponse) {
36+
if (err.status === 401) {
37+
this.loginService.logout();
38+
window.open("/login", "_self")
39+
}
40+
}
41+
})
42+
);
2943
} else {
3044
return next.handle(request);
3145
}

0 commit comments

Comments
 (0)