From 0427d1c62dafabb63be74a14df05b3d91c3c2279 Mon Sep 17 00:00:00 2001 From: kusowl Date: Mon, 23 Feb 2026 18:46:23 +0530 Subject: [PATCH] feature: user can login to backend --- .oxfmtrc.json | 3 +++ src/app/app.config.ts | 8 ++++++- .../interceptors/auth-interceptor.spec.ts | 17 +++++++++++++ src/app/core/interceptors/auth-interceptor.ts | 20 ++++++++++++++++ src/app/core/layouts/header/header.html | 2 +- src/app/core/layouts/header/header.ts | 3 ++- src/app/core/tokens/api-url-tokens.ts | 5 ++++ .../features/auth/components/login/login.html | 8 ++++--- .../features/auth/components/login/login.ts | 24 +++++++++++++++++-- .../features/auth/services/auth-service.ts | 16 ++++++++++++- src/environments/environment.development.ts | 1 + src/environments/environment.ts | 1 + 12 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 .oxfmtrc.json create mode 100644 src/app/core/interceptors/auth-interceptor.spec.ts create mode 100644 src/app/core/interceptors/auth-interceptor.ts 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 @@