import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { Router } from '@angular/router';
-import { ToastsManager } from 'ng2-toastr';
-
import { AuthService } from '../../../shared/api/auth.service';
import { Credentials } from '../../../shared/models/credentials';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
constructor(private authService: AuthService,
private authStorageService: AuthStorageService,
- private router: Router,
- public toastr: ToastsManager,
- private vcr: ViewContainerRef) {
- this.toastr.setRootViewContainerRef(vcr);
+ private router: Router) {
}
ngOnInit() {
import {
- HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest
+ HttpErrorResponse,
+ HttpEvent,
+ HttpHandler,
+ HttpInterceptor,
+ HttpRequest
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
-import { ToastsManager } from 'ng2-toastr';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Observable';
+import { NotificationType } from '../enum/notification-type.enum';
import { AuthStorageService } from './auth-storage.service';
+import { NotificationService } from './notification.service';
@Injectable()
export class AuthInterceptorService implements HttpInterceptor {
-
- constructor(private router: Router,
- private authStorageService: AuthStorageService,
- public toastr: ToastsManager) {
- }
+ constructor(
+ private router: Router,
+ private authStorageService: AuthStorageService,
+ public notificationService: NotificationService
+ ) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
- return next.handle(request)
- .catch((resp) => {
- if (resp instanceof HttpErrorResponse) {
- switch (resp.status) {
- case 404:
- this.router.navigate(['/404']);
- break;
- case 401:
- this.authStorageService.remove();
- this.router.navigate(['/login']);
- // falls through
- default:
- this.toastr.error(resp.error.detail || '',
- `${resp.status} - ${resp.statusText}`);
- }
+ return next.handle(request).catch(resp => {
+ if (resp instanceof HttpErrorResponse) {
+ switch (resp.status) {
+ case 404:
+ this.router.navigate(['/404']);
+ break;
+ case 401:
+ this.authStorageService.remove();
+ this.router.navigate(['/login']);
+ // falls through
+ default:
+ this.notificationService.show(
+ NotificationType.error,
+ resp.error.detail || '',
+ `${resp.status} - ${resp.statusText}`
+ );
}
- // Return the error to the method that called it.
- return Observable.throw(resp);
- });
+ }
+ // Return the error to the method that called it.
+ return Observable.throw(resp);
+ });
}
}