{ 'Content-Type': 'application/json' };
return { headers: new HttpHeaders(headers), withCredentials: this.withCredentials };
}
getAuthRequestOptions() {
const headers = { 'Content-Type': 'application/json' };
headers[this.tokenService.headerName] = `${this.tokenService.headerPrefix} ${this.tokenService.get()}`;
return { headers: new HttpHeaders(headers), withCredentials: this.withCredentials };
}
get(url: string, direct?: boolean): Observable<any> {
if (direct || this.direct) {
return this.http.get(url, this.getRequestOptions());
}
return this.http.get(url, this.getAuthRequestOptions());
}
а ты че к каждому новому запросу дописываешь сервис ?)
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any» { this.authService = this.injector.get(AuthService); request = request.clone({ setHeaders: { Authorization: `Bearer ${this.authService.getToken()}` } }); return next.handle(request) .do( (event: HttpEvent<any>) => { if (event instanceof HttpResponse) { console.log('Date : ' + new Date().toDateString()); } }, (err: any) => { console.log(err.error.error); if (err instanceof HttpErrorResponse) { if (err.status === 401) { this.router.navigate(['login']); localStorage.removeItem('token'); } else if (err.status === 500) { this.router.navigate(['login']); localStorage.removeItem('token'); } } } ); }
Обсуждают сегодня