Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70435

Use Navigator in Interceptor

$
0
0

@lalitkushwah wrote:

Hello All,

I want to perform some routing operations in Interceptor. How can I use NavController inside Custom Interceptor. Here is sample code of my interceptor:

import { Injectable } from “@angular/core”;
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from “@angular/common/http”;
import { Observable } from “rxjs/Observable”;
import ‘rxjs/add/operator/do’;
import { AppModule } from “…/app/app.module”;

@Injectable()
export class PostInterceptor implements HttpInterceptor {

intercept(req: HttpRequest, next: HttpHandler): Observable<HttpEvent> {
let postReq =null;
let authenticationToken = localStorage.getItem(‘token’);

if(authenticationToken != null) {
  console.log('Authentication Token in interceptor null',authenticationToken);
  postReq = req.clone({
    headers: req.headers.set('Bearer', authenticationToken).append('Content-Type', 'application/json')
  });
}
else{
  postReq = req.clone();
}

return next.handle(postReq).do(event => {
  if (event instanceof HttpResponse) {
    if(event.body.loggedIn != null) {
      //loggedIn = false
      if(!event.body.loggedIn) {
          console.log('Logged In false');
      }
      else {
        //Do Nothing
        console.log('LoggedIn True')
      }
    }
    else {
      console.log(event.body);
      AppModule.map.set('prevRequest',postReq);
    }
  }
})

}
}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 70435

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>