diff --git a/.oxfmtrc.json b/.oxfmtrc.json new file mode 100644 index 0000000..0b55248 --- /dev/null +++ b/.oxfmtrc.json @@ -0,0 +1,3 @@ +{ + "ignorePatterns": ["backend/**", "*.min.js"] +} diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 6f595a4..baca460 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -2,7 +2,13 @@ import { ApplicationConfig, provideBrowserGlobalErrorListeners } from "@angular/ import { provideRouter } from "@angular/router"; import { routes } from "./app.routes"; +import { provideHttpClient, withFetch, withInterceptors } from "@angular/common/http"; +import { authInterceptor } from "./core/interceptors/auth-interceptor"; export const appConfig: ApplicationConfig = { - providers: [provideBrowserGlobalErrorListeners(), provideRouter(routes)], + providers: [ + provideBrowserGlobalErrorListeners(), + provideRouter(routes), + provideHttpClient(withFetch(), withInterceptors([authInterceptor])), + ], }; diff --git a/src/app/core/interceptors/auth-interceptor.spec.ts b/src/app/core/interceptors/auth-interceptor.spec.ts new file mode 100644 index 0000000..854ad84 --- /dev/null +++ b/src/app/core/interceptors/auth-interceptor.spec.ts @@ -0,0 +1,17 @@ +import { TestBed } from "@angular/core/testing"; +import { HttpInterceptorFn } from "@angular/common/http"; + +import { authInterceptor } from "./auth-interceptor"; + +describe("authInterceptor", () => { + const interceptor: HttpInterceptorFn = (req, next) => + TestBed.runInInjectionContext(() => authInterceptor(req, next)); + + beforeEach(() => { + TestBed.configureTestingModule({}); + }); + + it("should be created", () => { + expect(interceptor).toBeTruthy(); + }); +}); diff --git a/src/app/core/interceptors/auth-interceptor.ts b/src/app/core/interceptors/auth-interceptor.ts new file mode 100644 index 0000000..246a565 --- /dev/null +++ b/src/app/core/interceptors/auth-interceptor.ts @@ -0,0 +1,20 @@ +import { HttpInterceptorFn } from "@angular/common/http"; + +export const authInterceptor: HttpInterceptorFn = (req, next) => { + const getCookie = (name: string): string | null => { + const match = document.cookie.match(new RegExp("(^|;\\s*)(" + name + ")=([^;]*)")); + return match ? decodeURIComponent(match[3]) : null; + }; + + let headers = req.headers.set("Accept", "application/json"); + const xsrfToken = getCookie("XSRF-TOKEN"); + if (xsrfToken) { + headers = headers.set("X-XSRF-TOKEN", xsrfToken); + } + const clonedRequest = req.clone({ + withCredentials: true, + headers: headers, + }); + + return next(clonedRequest); +}; diff --git a/src/app/core/layouts/header/header.html b/src/app/core/layouts/header/header.html index 1c825ed..33dd20a 100644 --- a/src/app/core/layouts/header/header.html +++ b/src/app/core/layouts/header/header.html @@ -33,7 +33,7 @@