From 49ff565fc0eab6f35e89f7ca0f0b6f5ff818e46d Mon Sep 17 00:00:00 2001 From: itzMiney Date: Fri, 20 Mar 2026 17:37:05 +0100 Subject: [PATCH] added better auth guard to admin page --- src/app/admin.guard.ts | 13 +++++++++++++ src/app/admin/admin.component.ts | 7 +++++-- src/app/app.component.ts | 5 ----- src/app/app.routes.ts | 3 ++- src/app/login/login.component.ts | 4 ++++ src/app/shared/services/auth.service.ts | 12 ++++++------ 6 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 src/app/admin.guard.ts diff --git a/src/app/admin.guard.ts b/src/app/admin.guard.ts new file mode 100644 index 0000000..44220e9 --- /dev/null +++ b/src/app/admin.guard.ts @@ -0,0 +1,13 @@ +import { inject } from '@angular/core'; +import { CanActivateFn, Router, UrlTree } from '@angular/router'; +import { map, Observable } from 'rxjs'; +import { AuthService } from './shared/services/auth.service'; + +export const adminGuard: CanActivateFn = (): Observable => { + const authService = inject(AuthService); + const router = inject(Router); + + return authService.validateToken().pipe( + map((isValid) => isValid ? true : router.createUrlTree(['/login'])) + ); +}; diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index c0a04ae..4bf898c 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -93,7 +93,6 @@ export class AdminComponent implements OnInit, OnDestroy { ngOnInit() { this.titleService.setTitle('Admin Panel | itzMiney') this.isNarrow = this.deviceService.isNarrow; - this.authService.validateToken().subscribe(); this.startTokenValidation(); this.loadArticles(); } @@ -104,7 +103,11 @@ export class AdminComponent implements OnInit, OnDestroy { startTokenValidation() { this.tokenValidationInterval = interval(60000).subscribe(() => { - this.authService.validateToken().subscribe(); + this.authService.validateToken().subscribe((isValid) => { + if (!isValid) { + this.authService.logout(); + } + }); }); } diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 2e78710..7169b5b 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -46,11 +46,6 @@ export class AppComponent implements OnInit { this.showFooter = false; break; } - case 'shortener': { - this.showNavbar = false; - this.showFooter = false; - break; - } default: { this.showNavbar = true; this.showFooter = true; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index f850333..523094f 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -11,6 +11,7 @@ import {SocialsComponent} from './socials/socials.component'; // import {TosComponent} from './tos/tos.component'; import {ImprintComponent} from './imprint/imprint.component'; import {PrivacyComponent} from './privacy/privacy.component'; +import {adminGuard} from './admin.guard'; // import {SuccessComponent} from './checkout/success/success.component'; // import {CancelComponent} from './checkout/cancel/cancel.component'; @@ -20,7 +21,7 @@ export const routes: Routes = [ { path: 'portfolio', component: PortfolioComponent }, { path: 'blog', component: BlogComponent }, { path: 'login', component: LoginComponent }, - { path: 'admin', component: AdminComponent }, + { path: 'admin', component: AdminComponent, canActivate: [adminGuard] }, { path: 'socials', component: SocialsComponent }, // { path: 'services', component: ServicesComponent }, // { path: 'tos', component: TosComponent }, diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index d790308..ca0a96d 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -32,6 +32,10 @@ export class LoginComponent implements OnInit { ngOnInit() { this.titleService.setTitle('Login | itzMiney') this.isNarrow = this.deviceService.isNarrow; + + if (this.authService.token) { + void this.router.navigate(['/admin']); + } } onLogin() { diff --git a/src/app/shared/services/auth.service.ts b/src/app/shared/services/auth.service.ts index 03fe330..6c291f2 100644 --- a/src/app/shared/services/auth.service.ts +++ b/src/app/shared/services/auth.service.ts @@ -46,20 +46,20 @@ export class AuthService { const token = this.token; if (!token) { - this.logout(); + localStorage.removeItem('token'); return of(false); } try { const payload = JSON.parse(atob(token.split('.')[1])); - const isValid = payload.exp * 1000; + const expiresAt = payload.exp * 1000; - if (!isValid) { - this.logout(); + if (!payload.exp || Date.now() >= expiresAt) { + localStorage.removeItem('token'); return of(false); } } catch { - this.logout(); + localStorage.removeItem('token'); return of(false); } @@ -68,7 +68,7 @@ export class AuthService { }).pipe( map(() => true), catchError(() => { - this.logout(); + localStorage.removeItem('token'); return of(false); }) );