Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
TTA-209 added redirectioning for any 401 status response
  • Loading branch information
andresacg30 committed Nov 2, 2022
commit af1b014ce24e3e60f28da7f346a431aadb490dc0
18 changes: 16 additions & 2 deletions src/app/modules/shared/interceptors/inject.token.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import {
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpErrorResponse,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';
import { environment } from './../../../../environments/environment';
import { EnvironmentType } from 'src/environments/enum';
import { LoginService } from '../../login/services/login.service';
import { catchError } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable()
export class InjectTokenInterceptor implements HttpInterceptor {
isProduction = environment.production === EnvironmentType.TT_PROD_LEGACY;
constructor(private azureAdB2CService: AzureAdB2CService, private loginService: LoginService) { }
constructor(private azureAdB2CService: AzureAdB2CService, private loginService: LoginService, private router: Router) { }

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.url.startsWith(environment.timeTrackerApiUrl)) {
Expand All @@ -25,7 +29,17 @@ export class InjectTokenInterceptor implements HttpInterceptor {
headers: request.headers.set('Authorization',
'Bearer ' + token)
});
return next.handle(requestWithHeaders);
return next.handle(requestWithHeaders)
.pipe(
tap(() => { }, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.loginService.logout();
window.open("/login", "_self")
}
}
})
);
} else {
return next.handle(request);
}
Expand Down