From 4a4c8bd4e3521d7a007d61e9f01e2ccc22ef762a Mon Sep 17 00:00:00 2001 From: kusowl Date: Tue, 24 Feb 2026 18:14:21 +0530 Subject: [PATCH] feature: user logout and auth states added s authState which helps conditonaly render components based on this state stored user details in localStoarge so that server side end point does not get hit in every page load. add a guard which protects routes and redirects to login if user is not logged in. create a logout route --- src/app/app.routes.ts | 2 + src/app/core/guards/auth-guard.spec.ts | 17 ++++ src/app/core/guards/auth-guard.ts | 11 +++ src/app/core/layouts/header/header.html | 16 ++-- src/app/core/layouts/header/header.ts | 8 +- src/app/core/models/user.model.ts | 8 ++ .../core/services/local-storage.service.ts | 39 +++++++++ src/app/core/services/local-storage.spec.ts | 16 ++++ src/app/features/auth/auth.routes.ts | 17 +++- .../features/auth/components/login/login.ts | 2 +- .../features/auth/services/auth-service.ts | 81 +++++++++++++++++-- 11 files changed, 202 insertions(+), 15 deletions(-) create mode 100644 src/app/core/guards/auth-guard.spec.ts create mode 100644 src/app/core/guards/auth-guard.ts create mode 100644 src/app/core/services/local-storage.service.ts create mode 100644 src/app/core/services/local-storage.spec.ts diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 0f47205..d948d53 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,10 +1,12 @@ import { Routes } from "@angular/router"; import { Home } from "./features/home/home"; +import { authGuard } from "./core/guards/auth-guard"; export const routes: Routes = [ { path: "", component: Home, + canActivate: [authGuard], }, { path: "", diff --git a/src/app/core/guards/auth-guard.spec.ts b/src/app/core/guards/auth-guard.spec.ts new file mode 100644 index 0000000..82a0aa2 --- /dev/null +++ b/src/app/core/guards/auth-guard.spec.ts @@ -0,0 +1,17 @@ +import { TestBed } from "@angular/core/testing"; +import { CanActivateFn } from "@angular/router"; + +import { authGuard } from "./auth-guard"; + +describe("authGuard", () => { + const executeGuard: CanActivateFn = (...guardParameters) => + TestBed.runInInjectionContext(() => authGuard(...guardParameters)); + + beforeEach(() => { + TestBed.configureTestingModule({}); + }); + + it("should be created", () => { + expect(executeGuard).toBeTruthy(); + }); +}); diff --git a/src/app/core/guards/auth-guard.ts b/src/app/core/guards/auth-guard.ts new file mode 100644 index 0000000..8b3e945 --- /dev/null +++ b/src/app/core/guards/auth-guard.ts @@ -0,0 +1,11 @@ +import { CanActivateFn, Router } from "@angular/router"; +import { inject } from "@angular/core"; +import { AuthService } from "../../features/auth/services/auth-service"; + +export const authGuard: CanActivateFn = (route, state) => { + const authService = inject(AuthService); + const router = inject(Router); + + if (authService.isAuthenticated()) return true; + return router.navigate(["/login"]); +}; diff --git a/src/app/core/layouts/header/header.html b/src/app/core/layouts/header/header.html index 33dd20a..508f08a 100644 --- a/src/app/core/layouts/header/header.html +++ b/src/app/core/layouts/header/header.html @@ -3,14 +3,14 @@ class="bg-gray-50 wrapper py-4 flex gap-x-5 sm:gap-x-10 items-center shadow-lg shadow-gray-400/20" >
- eKart + eKart
@@ -32,8 +32,14 @@
-