Angular 6 renewToken - PullRequest
       15

Angular 6 renewToken

0 голосов
/ 31 августа 2018

Я использую перехватчик, чтобы проверить, что токен жив. Если токен мертв, отправьте запрос на сервер с токеном и renewToken, получите новый. Однако, если токен обновления мертв, и браузер перейдет на страницу входа. Но я не могу справиться с ошибкой renewToken, кто-то может сказать мне почему?

auth.interceptor.ts

export class AuthInterceptor implements HttpInterceptor {
private httpHeaders: HttpHeaders;
constructor(private authorizationService: AuthorizationService,
    private http: HttpClient, private globalSrv: GlobalService, private router: Router) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.url.indexOf('/login') <= 0) {
        this.httpHeaders = this.authorizationService.getHttpHeaders();
        const cloned = req.clone({
            headers: this.httpHeaders
        })
        return next.handle(cloned).pipe(catchError((error, caught) => {
            this.handleAuthError(error);
            return of(error);
        }) as any);
    } else
        return next.handle(req)
}

private handleAuthError(err: HttpErrorResponse){
    if (err.status === 401) {
        this.authorizationService
            .fetchRenewToken(this.globalSrv.authInfo.token,
                             this.globalSrv.authInfo.renewToken)
            .subscribe(response => this.globalSrv.authInfo = response.body;)
    }
}

auth.service.ts

fetchRenewToken(token, renewToken) {
   return this.http.post<Authorization>(
     environment.serverUrl + '/public/renewToken',
     { token, renewToken },
     { observe: 'response' }).pipe(catchError(this.handleError))}

handleError(err: HttpErrorResponse) {
   if(err.status == 500){
      this.router.navigate(['/login'])
   }
   return throwError('here is a testerror');
}
...