TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Adhikar Patil
NA
481
128.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.
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"
;
}
})
// add authorization header with jwt token if available
let currentUser = JSON.parse(localStorage.getItem(
'currentUser'
));
if
(currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}
return
next.handle(request);
}
}
Reply
Answers (
0
)
Unable to read the back end api from Electron angular app
Push multiple values into single model