Http-Interceptor hinzugefügt.
This commit is contained in:
@@ -45,8 +45,8 @@
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kb",
|
||||
"maximumError": "1mb"
|
||||
"maximumWarning": "1.5mb",
|
||||
"maximumError": "2mb"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
@@ -114,4 +114,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user