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 @@
-