Merge pull request #57 from itzMiney/development

added better auth guard to admin page
This commit is contained in:
itzMiney authored and GitHub committed 2026-03-20 17:38:10 +01:00
commit aaa1468a15
6 files changed
+30 -14

No files matched your search

+13
View File
@@ -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<boolean | UrlTree> => {
const authService = inject(AuthService);
const router = inject(Router);
return authService.validateToken().pipe(
map((isValid) => isValid ? true : router.createUrlTree(['/login']))
);
};
+5 -2
View File
@@ -93,7 +93,6 @@ export class AdminComponent implements OnInit, OnDestroy {
ngOnInit() { ngOnInit() {
this.titleService.setTitle('Admin Panel | itzMiney') this.titleService.setTitle('Admin Panel | itzMiney')
this.isNarrow = this.deviceService.isNarrow; this.isNarrow = this.deviceService.isNarrow;
this.authService.validateToken().subscribe();
this.startTokenValidation(); this.startTokenValidation();
this.loadArticles(); this.loadArticles();
} }
@@ -104,7 +103,11 @@ export class AdminComponent implements OnInit, OnDestroy {
startTokenValidation() { startTokenValidation() {
this.tokenValidationInterval = interval(60000).subscribe(() => { this.tokenValidationInterval = interval(60000).subscribe(() => {
this.authService.validateToken().subscribe(); this.authService.validateToken().subscribe((isValid) => {
if (!isValid) {
this.authService.logout();
}
});
}); });
} }
-5
View File
@@ -46,11 +46,6 @@ export class AppComponent implements OnInit {
this.showFooter = false; this.showFooter = false;
break; break;
} }
case 'shortener': {
this.showNavbar = false;
this.showFooter = false;
break;
}
default: { default: {
this.showNavbar = true; this.showNavbar = true;
this.showFooter = true; this.showFooter = true;
+2 -1
View File
@@ -11,6 +11,7 @@ import {SocialsComponent} from './socials/socials.component';
// import {TosComponent} from './tos/tos.component'; // import {TosComponent} from './tos/tos.component';
import {ImprintComponent} from './imprint/imprint.component'; import {ImprintComponent} from './imprint/imprint.component';
import {PrivacyComponent} from './privacy/privacy.component'; import {PrivacyComponent} from './privacy/privacy.component';
import {adminGuard} from './admin.guard';
// import {SuccessComponent} from './checkout/success/success.component'; // import {SuccessComponent} from './checkout/success/success.component';
// import {CancelComponent} from './checkout/cancel/cancel.component'; // import {CancelComponent} from './checkout/cancel/cancel.component';
@@ -20,7 +21,7 @@ export const routes: Routes = [
{ path: 'portfolio', component: PortfolioComponent }, { path: 'portfolio', component: PortfolioComponent },
{ path: 'blog', component: BlogComponent }, { path: 'blog', component: BlogComponent },
{ path: 'login', component: LoginComponent }, { path: 'login', component: LoginComponent },
{ path: 'admin', component: AdminComponent }, { path: 'admin', component: AdminComponent, canActivate: [adminGuard] },
{ path: 'socials', component: SocialsComponent }, { path: 'socials', component: SocialsComponent },
// { path: 'services', component: ServicesComponent }, // { path: 'services', component: ServicesComponent },
// { path: 'tos', component: TosComponent }, // { path: 'tos', component: TosComponent },
+4
View File
@@ -32,6 +32,10 @@ export class LoginComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.titleService.setTitle('Login | itzMiney') this.titleService.setTitle('Login | itzMiney')
this.isNarrow = this.deviceService.isNarrow; this.isNarrow = this.deviceService.isNarrow;
if (this.authService.token) {
void this.router.navigate(['/admin']);
}
} }
onLogin() { onLogin() {
+6 -6
View File
@@ -46,20 +46,20 @@ export class AuthService {
const token = this.token; const token = this.token;
if (!token) { if (!token) {
this.logout(); localStorage.removeItem('token');
return of(false); return of(false);
} }
try { try {
const payload = JSON.parse(atob(token.split('.')[1])); const payload = JSON.parse(atob(token.split('.')[1]));
const isValid = payload.exp * 1000; const expiresAt = payload.exp * 1000;
if (!isValid) { if (!payload.exp || Date.now() >= expiresAt) {
this.logout(); localStorage.removeItem('token');
return of(false); return of(false);
} }
} catch { } catch {
this.logout(); localStorage.removeItem('token');
return of(false); return of(false);
} }
@@ -68,7 +68,7 @@ export class AuthService {
}).pipe( }).pipe(
map(() => true), map(() => true),
catchError(() => { catchError(() => {
this.logout(); localStorage.removeItem('token');
return of(false); return of(false);
}) })
); );