1import { from } from 'rxjs';
2
3var observableFromPromise = from(promiseSrc);
4
1import { tap } from 'rxjs/operators';
2
3const TOKEN_HEADER_KEY = 'Authorization';
4
5@Injectable()
6export class Interceptor implements HttpInterceptor {
7
8 constructor(private token: TokenStorage, private router: Router) { }
9
10 intercept(req: HttpRequest<any>, next: HttpHandler):
11 Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
12 let authReq = req;
13 if (this.token.getToken() != null) {
14 authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + this.token.getToken())});
15 }
16 return next.handle(authReq).pipe(tap(
17 (err: any) => {
18 if (err instanceof HttpErrorResponse) {
19 console.log(err);
20 console.log('req url :: ' + req.url);
21 if (err.status === 401) {
22 this.router.navigate(['user']);
23 }
24 }
25 }
26 ));
27 }
28
29}
30