Hello,
I want to check internet connection in Jwt interceptor before every requets it shows message if internet connection is disabled.
- import { Injectable } from '@angular/core';
- import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
- import { Observable } from 'rxjs';
- import { ConnectionService } from 'ng-connection-service';
- @Injectable()
- export class JwtInterceptor implements HttpInterceptor {
- status = 'ONLINE';
- isConnected = true;
- constructor(private connectionService: ConnectionService) { }
- intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
- this.connectionService.monitor().subscribe(isConnected => {
- this.isConnected = isConnected;
- if (this.isConnected) {
- this.status = "ONLINE";
- }
- else {
- this.status = "OFFLINE";
- }
- })
-
- let currentUser = JSON.parse(localStorage.getItem('currentUser'));
- if (currentUser && currentUser.token) {
- request = request.clone({
- setHeaders: {
- Authorization: `Bearer ${currentUser.token}`
- }
- });
- }
- return next.handle(request);
- }
- }