Adhikar Patil

Adhikar Patil

  • NA
  • 481
  • 127.9k

How to check internet connection in Jwt interceptor in angul

Feb 5 2020 10:53 PM
Hello,
 
I want to check internet connection in Jwt interceptor before every requets it shows message if internet connection is disabled.
  1. import { Injectable } from '@angular/core';  
  2. import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';  
  3. import { Observable } from 'rxjs';  
  4. import { ConnectionService } from 'ng-connection-service';  
  5. @Injectable()  
  6. export class JwtInterceptor implements HttpInterceptor {  
  7. status = 'ONLINE';  
  8. isConnected = true;  
  9. constructor(private connectionService: ConnectionService) { }  
  10. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {  
  11. this.connectionService.monitor().subscribe(isConnected => {  
  12. this.isConnected = isConnected;  
  13. if (this.isConnected) {  
  14. this.status = "ONLINE";  
  15. }  
  16. else {  
  17. this.status = "OFFLINE";  
  18. }  
  19. })  
  20. // add authorization header with jwt token if available  
  21. let currentUser = JSON.parse(localStorage.getItem('currentUser'));  
  22. if (currentUser && currentUser.token) {  
  23. request = request.clone({  
  24. setHeaders: {  
  25. Authorization: `Bearer ${currentUser.token}`  
  26. }  
  27. });  
  28. }  
  29. return next.handle(request);  
  30. }  
  31. }