-login guard
All checks were successful
Build Docker Image / Preparing Dependecies (push) Successful in 4s

-caching
-budget
This commit is contained in:
2025-11-19 18:30:35 +07:00
parent 213fd197ef
commit 15308ababa
25 changed files with 667 additions and 479 deletions

View File

@@ -13,6 +13,7 @@ export class CachingInterceptor implements HttpInterceptor {
constructor(private cache: CachingService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('Caching interceptor:', req.urlWithParams, 'isCacheable:', this.isCacheable(req));
if (!this.isCacheable(req)) {
return next.handle(req);
}
@@ -32,12 +33,22 @@ export class CachingInterceptor implements HttpInterceptor {
}
private isCacheable(req: HttpRequest<any>): boolean {
const checkUrl = (methodUrls: string[]) => {
return methodUrls.some(url => {
// Regex to match the configured URL, and ensure it's not just a partial match of a larger URL segment.
// e.g. /api/user should match /api/user, /api/user/123, /api/user?query=1
// but not /api/user-roles
const regex = new RegExp(url + '($|/|\\?)');
return regex.test(req.urlWithParams);
});
};
if (req.method === 'GET') {
return CACHEABLE_URLS.GET.some(url => req.urlWithParams.includes(url));
return checkUrl(CACHEABLE_URLS.GET);
}
if (req.method === 'POST') {
return CACHEABLE_URLS.POST.some(url => req.urlWithParams.includes(url));
return checkUrl(CACHEABLE_URLS.POST);
}
return false;

View File

@@ -0,0 +1,30 @@
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { jwtDecode } from 'jwt-decode';
export const loginGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const accessToken = localStorage.getItem('access_token');
if (accessToken) {
try {
const decodedToken: any = jwtDecode(accessToken);
const currentTime = Date.now() / 1000;
if (decodedToken.exp < currentTime) {
// Token expired
localStorage.removeItem('access_token');
return true;
}
router.navigate(['/main']);
return false;
} catch (error) {
// Error decoding token
localStorage.removeItem('access_token');
return true;
}
} else {
return true;
}
};