Make a redirect in a extend Http in Angular 4 -


i have set own http service settings custom headers. trying make redirect when got error 401 don't work, don't know why. there no error, promise of router.navigate return false. doing wrong ?

there authhttpservice:

import { injectable } '@angular/core'; import { headers, http, request, requestoptions, requestoptionsargs, response, xhrbackend } '@angular/http'; import { observable } 'rxjs/observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import { router } '@angular/router';  @injectable() export class authhttpservice extends http {    constructor(     backend: xhrbackend,     options: requestoptions,     private router: router   ) {     super(backend, options);   }    request(url: string | request, options?: requestoptionsargs): observable<response> {     const token = localstorage.getitem('token');      if (typeof url === 'string') { // meaning have add token options, not in url       if (!options) {         // let's make option object         options = {headers: new headers()};       }       options.headers.set('authorization', `bearer ${token}`);     } else {       // have add token url object       url.headers.set('authorization', `bearer ${token}`);     }     return super.request(url, options).catch(this.catchautherror(this));   }    private catchautherror(self: authhttpservice) {      // have pass authhttpservice's own instance here `self`     return (res: response) => {       if (res.status === 401) {         // redirect login page if not authenticated         this.router.navigate(['/login'])         return observable.empty();       }       return observable.throw(res);     };   } } 

and in app.module.ts set :

providers: [     {       provide: authhttpservice,       usefactory: (backend: xhrbackend, options: requestoptions, router: router) => {         return new authhttpservice(backend, options, router);       },       deps: [xhrbackend, requestoptions, router]     }   ], 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -