Spaces:
Running
Running
Download src/app/auth/auth.interceptor.ts from pykara/py-trade: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/pykara/py-trade/resolve/main/src/app/auth/auth.interceptor.ts
- Command line
-
hf download hf://spaces/pykara/py-trade/src/app/auth/auth.interceptor.ts
-
curl -L -o auth.interceptor.ts https://huggingface.co/spaces/pykara/py-trade/resolve/main/src/app/auth/auth.interceptor.ts
1.07 kB
| import { Injectable } from '@angular/core'; | |
| import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpErrorResponse } from '@angular/common/http'; | |
| import { Observable, throwError } from 'rxjs'; | |
| import { AuthService } from './auth.service'; | |
| import { catchError } from 'rxjs/operators'; | |
| import { AuthUiService } from './auth-ui.service'; | |
| () | |
| export class AuthInterceptor implements HttpInterceptor { | |
| constructor( | |
| private auth: AuthService, | |
| private authUi: AuthUiService | |
| ) {} | |
| intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
| const token = this.auth.getAccessToken(); | |
| let authReq = req; | |
| if (token && !req.headers.has('Authorization')) { | |
| authReq = req.clone({ | |
| setHeaders: { Authorization: `Bearer ${token}` } | |
| }); | |
| } | |
| return next.handle(authReq).pipe( | |
| catchError((err: HttpErrorResponse) => { | |
| if (err.status === 401) { | |
| this.auth.logout(); | |
| this.authUi.openSignin(); | |
| } | |
| return throwError(() => err); | |
| }) | |
| ); | |
| } | |
| } | |