3

私の角度アプリには「listdata.component.ts」というコンポーネントがあります。このコンポーネントの ngOnInit() では、2 つの API 呼び出しが発生します。しかし、2 つの要求に対して 401 応答が得られると、ログアウト API 呼び出しが 2 回行われます。私のインターセプターコード

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
    notificationItem: any;
    constructor(public authService: AuthGuardService, private router: Router, private notification: NotificationService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (this.authService.tokenValid()) {
            const authReq = request.clone({ headers: request.headers.set("Authorization", 'Bearer ' + this.authService.getToken()) });
            return next.handle(authReq).do((event: HttpEvent<any>) => {
            }, err => {

                if (err instanceof HttpErrorResponse) {

                    if (err.status === 401) {

                        if(authReq.url.indexOf('/logout') > -1){
                            localStorage.clear();
                            this.router.navigate(['login']);
                        }
                        else
                        {

                this.authService.logout()

                        }
                    }
                }
            });
        }
        else{
            return next.handle(request);
        }
    }
}

1 つの 401 応答の後、すべての 401 要求をキャンセルし、ログアウトを 1 回だけ呼び出したいと思います。

4

0 に答える 0