new Landing page, stripped app
This commit is contained in:
34
bizmatch-client/src/app/interceptors/auth.interceptor.ts
Normal file
34
bizmatch-client/src/app/interceptors/auth.interceptor.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, from } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { AuthService } from '../services/auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthInterceptor implements HttpInterceptor {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
// Prüfe, ob die Anfrage an die apiBaseUrl gerichtet ist
|
||||
const isApiRequest = req.url.startsWith(environment.apiBaseUrl);
|
||||
|
||||
if (!isApiRequest) {
|
||||
// Wenn es keine API-Anfrage ist, leite die Anfrage unverändert weiter
|
||||
return next.handle(req);
|
||||
}
|
||||
|
||||
// Wenn es eine API-Anfrage ist, füge den Token hinzu (falls vorhanden)
|
||||
return from(this.authService.getToken()).pipe(
|
||||
switchMap(token => {
|
||||
if (token) {
|
||||
const clonedReq = req.clone({
|
||||
setHeaders: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
return next.handle(clonedReq);
|
||||
}
|
||||
return next.handle(req);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user