-login guard
All checks were successful
Build Docker Image / Preparing Dependecies (push) Successful in 4s
All checks were successful
Build Docker Image / Preparing Dependecies (push) Successful in 4s
-caching -budget
This commit is contained in:
@@ -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;
|
||||
|
||||
30
ng-ttc-frontend/src/app/services/login.guard.ts
Normal file
30
ng-ttc-frontend/src/app/services/login.guard.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user