【问题标题】:When I try to make middle-ware for authentication error is occurred当我尝试制作中间件进行身份验证时发生错误
【发布时间】:2019-12-14 19:06:08
【问题描述】:

我尝试将 jwt Web 令牌挂在我的传出请求中,在显示添加拦截器“ERROR TypeError: Cannot read property 'length' of undefined”后我使用了拦截器方法。 当拦截器文件被删除时,应用程序正常工作

这是我的拦截类

【问题讨论】:

  • 会出现 authToken 未定义。

标签: angular typescript angular7


【解决方案1】:

这是一个简化版本,但它在我的应用中就像一个魅力:

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

        req = this.addAuthHeader(req);
    }

    addAuthHeader(request) {
         const r: HttpRequest<any> = request.clone({
             headers: new HttpHeaders({
                    Authorization: 'Bearer ' + this.authService.getToken()
             })
         });
         return r;
    }

供参考 --> how-to-add-multiple-headers-in-angular-5-httpinterceptorinterceptor-angular-4-3-set-multiple-headers-on-the-cloned-request

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 2018-06-15
    • 2018-05-03
    • 1970-01-01
    • 2018-10-09
    • 2021-02-06
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多