Angular2 route guard Rückkehr Beobachten<bool>, wie, um Fehler zu behandeln

Habe ich eine route guard wie unten

@Injectable()
export class AuthGuard implements CanActivate {

constructor(private router: Router, private authenticationSvc: AuthenticationService) { }

canActivate(): Observable<boolean> {
    return this.authenticationSvc.getAuthenticatedUser().map(
        r => {
            if (this.authenticationSvc.isAuthenticated()) {
                //logged in so return true
                return true;
            }
            this.router.navigateByUrl('/login');
            return false;
        })
}

Das Problem ist, dass manchmal getAuthenticatedUser ein 401 zurück, und ich habe eine http-interceptor, die Griffe der 401 und der redirect auf die login-Seite. Das Problem ist, dass diese .Karte nie aufgelöst wird, weil die http-Anforderung gibt eine Fehlermeldung aus, und die Winkel-router stecken, die auf diese erste routing-Anfrage und kann nicht mit der nachfolgenden Anfrage von den interceptor. Wie kann ich mit diesem Fehler und habe die Beobachtbaren zurückgegeben beheben false und Dinge bewegen?

  getAuthenticatedUser() {
         let getUserObservable = this.http.get(ApiUrl + 'security/getAuthenticatedUser')
            .map((res: any) => res.json())
            .share()

        //Get the result for the cache
        getUserObservable.subscribe(
            r => {
                if (r.success) {
                    this.authenticatedUser = r.result.user;
                }
            }); 

        //return the observable
        return getUserObservable;
  } 

- und http-intercepter unten

export class HttpInterceptor extends Http {
    authSvc: AuthenticationService;
    lastClicked: any = 0;
    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router, private injector: Injector) {
        super(backend, defaultOptions);
    }
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    return this.intercept(super.request(url, options));
}

get(url: string, options?: RequestOptionsArgs): Observable<Response> {
    return this.intercept(super.get(url, options));
}

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    return this.intercept(super.post(url, body, this.getRequestOptionArgs(options)));
}

put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
}

delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
    return this.intercept(super.delete(url, options));
}

getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
    if (options == null) {
        options = new RequestOptions();
    }
    if (options.headers == null) {
        options.headers = new Headers();
    }
    options.headers.append('Content-Type', 'application/json');
    return options;
}

 intercept(observable: Observable<Response>): Observable<Response> {
    return observable.catch((err, source) => {
        //If we get a 401 from the api that means out FormsAuthenticationTicket has expired, clear the auth cookie and navigate back to terms page
        if (err.status == 401) {
            this._router.navigateByUrl('/login');
        }

        return Observable.throw(err);
    });
}
  • Sollten Sie nicht dann/catch-die Antwort von getAuthenticatedUser? stackoverflow.com/questions/23559341/...
  • zeig mir, was du meinst, zusätzlichen http-intercepter und getAuthenticatedUser() für die Referenz
InformationsquelleAutor Josh | 2017-03-01
Schreibe einen Kommentar