Http-Interceptor hinzugefügt.

This commit is contained in:
Developer 02
2024-06-24 11:56:49 +02:00
parent d376065246
commit 1c11a0e8f0
17 changed files with 193 additions and 358 deletions

View File

@@ -45,8 +45,8 @@
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
"maximumWarning": "1.5mb",
"maximumError": "2mb"
},
{
"type": "anyComponentStyle",
@@ -114,4 +114,4 @@
}
}
}
}
}

View File

@@ -6,7 +6,8 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy
import { APP_BASE_HREF } from '@angular/common';
import { UrlService } from './services/url.service';
import { API_URL } from './tokens/index'
import { provideHttpClient, withFetch } from '@angular/common/http';
import { HTTP_INTERCEPTORS, provideHttpClient, withFetch } from '@angular/common/http';
import { HttpRequestInterceptor } from './http.interceptor';
export const appConfig: ApplicationConfig = {
providers: [
@@ -23,6 +24,11 @@ export const appConfig: ApplicationConfig = {
provide: API_URL,
useFactory: (urlService: UrlService) => urlService.getApiUrl(),
deps: [UrlService]
},
{
provide: HTTP_INTERCEPTORS,
useClass: HttpRequestInterceptor,
multi: true
}
]
};

View File

@@ -3,11 +3,13 @@ import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
export const authGuard: CanActivateFn = (route, state) => {
const authService = inject(AuthService);
const router = inject(Router);
authService.isAuthenticated().subscribe({next: res => console.log(res)})
return authService.isAuthenticated().pipe(
map(isAuthenticated => {
if (!isAuthenticated) {

View File

@@ -0,0 +1,17 @@
import { TestBed } from '@angular/core/testing';
import { HttpInterceptorFn } from '@angular/common/http';
import { httpInterceptor } from './http.interceptor';
describe('httpInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) =>
TestBed.runInInjectionContext(() => httpInterceptor(req, next));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});

View File

@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const secureReq = req.clone({
withCredentials: true,
setHeaders: {
'X-Insecure-Request': 'true',
'Content-Type': 'application/json',
...req.headers.keys().reduce((headers, key) => {
headers[key] = req.headers.get(key) || '';
return headers;
}, {} as { [name: string]: string })
}
});
return next.handle(secureReq);
}
}

View File

@@ -9,16 +9,12 @@ import { API_URL } from '../tokens/index';
export class EnvelopeReceiverService {
private url: string;
constructor(private http: HttpClient) {
constructor(private http: HttpClient) {
const api_url = inject(API_URL);
this.url = `${api_url}/envelopereceiver`;
}
getEnvelopeReceiver(): Observable<any> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
return this.http.get<any>(this.url, { withCredentials: true , headers });
return this.http.get<any>(this.url);
}
}