added better auth guard to admin page
This commit is contained in:
1 parent
f7300f3fb9
commit
49ff565fc0
6 files changed
+30
-14
No files matched your search
@@ -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']))
|
||||
);
|
||||
};
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user