2019-06-13 19:02:34 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
|
|
|
|
|
|
|
|
import {Observable, throwError} from 'rxjs';
|
|
|
|
import {catchError} from 'rxjs/operators';
|
|
|
|
import {ToasterService} from './toaster.service';
|
|
|
|
import {Router} from '@angular/router';
|
|
|
|
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class HttpAuthInterceptor implements HttpInterceptor {
|
|
|
|
|
|
|
|
constructor(private router: Router, private dialog: MatDialog, private toaster: ToasterService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler):
|
|
|
|
Observable<HttpEvent<any>> {
|
|
|
|
return next.handle(req)
|
|
|
|
.pipe(
|
|
|
|
catchError((error: any) => {
|
|
|
|
if (error.status === 401) {
|
|
|
|
this.toaster.show('Danger', 'User has been logged out');
|
|
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
|
|
width: '250px',
|
|
|
|
data: {
|
|
|
|
title: 'Logged out!',
|
2019-07-13 16:02:18 +00:00
|
|
|
content: 'You have been logged out.\n' +
|
|
|
|
'You can press Cancel to stay on page and login in another tab to resume here,' +
|
|
|
|
' or you can press Ok to navigate to the login page.'
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
|
|
if (result) {
|
|
|
|
this.router.navigate(['login']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return throwError(error);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|