Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic2 how to get NavController in provider

I create AuthHttpProvider which is Http wrapper which allow to send GET/POST/PUT/DELETE request and automagically add authentication token (jwt) to each request. And I use this wrapper instead Http in all my ionic2 app. Now i want to logout user (and go to loginPage) when I detect that any response from server has 403 Http error code.

So when I try to inject NavController (to use it to loginPage after 403 detection) in constructor by

constructor (public navCtrl: NavController)

I get:

Uncaught (in promise): Error: No provider for NavController

How to get access to navCtrl inside Provider?

like image 946
Kamil Kiełczewski Avatar asked Jun 13 '17 13:06

Kamil Kiełczewski


1 Answers

Here is solution (from this):

import { Injectable, Injector } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from "rxjs";
import 'rxjs/add/operator/map';
import { App } from "ionic-angular";
import { NavController } from "ionic-angular/index";
import { LoginPage } from "../../pages/login/login";
...

@Injectable()
export class AuthHttpProvider {

  private navCtrl: NavController;
  ...

  constructor(public http: Http, ..., private app:App ) {
    this.navCtrl = app.getActiveNav();
  }

  get(url) {  // http GET
    return this.http.get(url, this.getHeaders() )
                    .catch(this.globalErr(this));
  }

  // ... http POST/PUT/DELETE

  private getHeaders() { ... } //(cors, csrf-token ...)

  private globalErr(self) {
    return (err) => {
      if ( err.status == 403 ) { self.logout(); }
      return Observable.throw(err);
    };
  }

  public logout() {
    // ...
    this.navCtrl.setRoot(LoginPage);
    this.navCtrl.popToRoot;
  }
}

But better one will be if Provider be able to raise some event/exception and UI will able to intercept it and change view to loginPage

like image 141
Kamil Kiełczewski Avatar answered Sep 30 '22 04:09

Kamil Kiełczewski