Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在UI中处理授权错误_Node.js_Angular_Http_Angular Http Interceptors - Fatal编程技术网

Node.js 在UI中处理授权错误

Node.js 在UI中处理授权错误,node.js,angular,http,angular-http-interceptors,Node.js,Angular,Http,Angular Http Interceptors,我已经尝试在我的Angular中实现授权数小时了,下面是:。我已经构建了一个HTTP拦截器来捕获错误,但我不确定如何在登录视图中显示这一点。我试图传递一个变量,但无法使其工作 以下是我的AuthService的几种方法: login(email: string, password: string) { this.logout(); return this.http .post("http://localhost:3000/user/login", {

我已经尝试在我的Angular中实现授权数小时了,下面是:。我已经构建了一个HTTP拦截器来捕获错误,但我不确定如何在登录视图中显示这一点。我试图传递一个变量,但无法使其工作

以下是我的AuthService的几种方法:

login(email: string, password: string) {
    this.logout();

    return this.http
        .post("http://localhost:3000/user/login", {
            email,
            password
        })
        .do(res => {
            this.setSession(res);
        })
        .shareReplay();
}

    private setSession(authResult) {
    const expiresAt = moment().add(authResult.data.expiresIn, "second");

    localStorage.setItem("id_token", authResult.data.idToken);
    localStorage.setItem("expires_at", JSON.stringify(expiresAt.valueOf()));
}
这是我的Http接收器:

@可注射() 导出类AuthInterceptor实现HttpInterceptor{

constructor(private router: Router, private _data: AuthdataService) { }


intercept(req: HttpRequest<any>,
    next: HttpHandler): Observable<HttpEvent<any>> {

    const idToken = localStorage.getItem("id_token");

    if (idToken) {
        const cloned = req.clone({
            headers: req.headers.set("Authorization",
                "Bearer " + idToken)
        });

        return next.handle(cloned);
    }
    else {
        return next.handle(req).do(
            succ => console.log(succ),
            err => {
                if (err.status === 401) {
                    console.error('AUTHENTICATION FAILED');
                    this.router.navigateByUrl('/login');
                    //HOW CAN I SEND THIS TO THE UI?
                }
            }

        );
    }
}
}
我尝试从我的服务调用isLoggedIn(),它检查localstorage中的令牌,但是永远不会命中else语句,因为如果dataservce抛出401错误,则不会执行该方法

总而言之:我如何向我的登录HTML组件显示“错误的电子邮件或密码”之类的消息


谢谢您的帮助。

您可以从拦截器中抛出错误,并在登录组件中捕获它

应该是这样的:

return Observable.throw(err);
在登录组件中捕获它:

 loginUser() {
if (this.loginForm.valid) {
  const email = this.loginForm.value.email;
  const password = this.loginForm.value.password;

  //set loading to true and then false if error
  this.loading = false;
  this._data.login(email, password).subscribe(() => {
    if (this._data.isLoggedIn()) {
      this.router.navigateByUrl("/");
    } else {
      //never reaches here
    }
  });
}
}
this._data.login(email, password).subscribe(() => {
   if (this._data.isLoggedIn()) {
      this.router.navigateByUrl("/");
   } else {
      //never reaches here
   }
}, err => {
   /* Write your logic here*/
});

嗨@Anshuman,我好像用不上。注意“订阅”类型-抱歉,我刚开始学习Angular很高兴听到这个消息,你也是:)