From f70fe814329537a886abb17e9c52393c073a8618 Mon Sep 17 00:00:00 2001 From: Krrish Ghimire Date: Mon, 6 Jul 2026 00:58:11 +0545 Subject: [PATCH] fix token issues --- .../krrishg/config/GlobalExceptionHandler.java | 6 ++++++ .../java/com/krrishg/config/SecurityConfig.java | 3 --- frontend/proxy.conf.json | 4 ---- frontend/src/app/app.routes.ts | 3 +++ frontend/src/app/core/guards/login.guard.ts | 15 +++++++++++++++ .../src/app/core/interceptors/jwt.interceptor.ts | 3 ++- 6 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 frontend/src/app/core/guards/login.guard.ts diff --git a/backend/src/main/java/com/krrishg/config/GlobalExceptionHandler.java b/backend/src/main/java/com/krrishg/config/GlobalExceptionHandler.java index 823941c..3bb5fd4 100644 --- a/backend/src/main/java/com/krrishg/config/GlobalExceptionHandler.java +++ b/backend/src/main/java/com/krrishg/config/GlobalExceptionHandler.java @@ -1,5 +1,6 @@ package com.krrishg.config; +import io.jsonwebtoken.JwtException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -16,6 +17,11 @@ public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + @ExceptionHandler(JwtException.class) + public ProblemDetail handleJwtException(JwtException ex) { + return ProblemDetail.forStatusAndDetail(HttpStatus.UNAUTHORIZED, ex.getMessage()); + } + @ExceptionHandler(IllegalArgumentException.class) public ProblemDetail handleBadRequest(IllegalArgumentException ex) { return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage()); diff --git a/backend/src/main/java/com/krrishg/config/SecurityConfig.java b/backend/src/main/java/com/krrishg/config/SecurityConfig.java index a8d2423..3fd86be 100644 --- a/backend/src/main/java/com/krrishg/config/SecurityConfig.java +++ b/backend/src/main/java/com/krrishg/config/SecurityConfig.java @@ -134,9 +134,6 @@ public class SecurityConfig { } catch (Exception e) { org.springframework.security.core.context.SecurityContextHolder .clearContext(); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, - "Invalid or expired token"); - return; } } diff --git a/frontend/proxy.conf.json b/frontend/proxy.conf.json index 8668d7c..92df52a 100644 --- a/frontend/proxy.conf.json +++ b/frontend/proxy.conf.json @@ -6,9 +6,5 @@ "/oauth2": { "target": "http://127.0.0.1:8080", "secure": false - }, - "/login": { - "target": "http://127.0.0.1:8080", - "secure": false } } diff --git a/frontend/src/app/app.routes.ts b/frontend/src/app/app.routes.ts index 3365c73..4e91faa 100644 --- a/frontend/src/app/app.routes.ts +++ b/frontend/src/app/app.routes.ts @@ -1,15 +1,18 @@ import { Routes } from '@angular/router'; import { AuthGuard } from './core/guards/auth.guard'; +import { LoginGuard } from './core/guards/login.guard'; export const routes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, { path: 'login', loadComponent: () => import('./auth/login/login.component').then(m => m.LoginComponent), + canActivate: [LoginGuard], }, { path: 'register', loadComponent: () => import('./auth/register/register.component').then(m => m.RegisterComponent), + canActivate: [LoginGuard], }, { path: 'oauth2/callback', diff --git a/frontend/src/app/core/guards/login.guard.ts b/frontend/src/app/core/guards/login.guard.ts new file mode 100644 index 0000000..485c7fc --- /dev/null +++ b/frontend/src/app/core/guards/login.guard.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@angular/core'; +import { CanActivate, Router, UrlTree } from '@angular/router'; +import { AuthService } from '../services/auth.service'; + +@Injectable({ providedIn: 'root' }) +export class LoginGuard implements CanActivate { + constructor(private authService: AuthService, private router: Router) {} + + canActivate(): boolean | UrlTree { + if (!this.authService.isAuthenticated()) { + return true; + } + return this.router.parseUrl('/dashboard'); + } +} diff --git a/frontend/src/app/core/interceptors/jwt.interceptor.ts b/frontend/src/app/core/interceptors/jwt.interceptor.ts index 0d8d073..858dbea 100644 --- a/frontend/src/app/core/interceptors/jwt.interceptor.ts +++ b/frontend/src/app/core/interceptors/jwt.interceptor.ts @@ -19,7 +19,7 @@ export class JwtInterceptor implements HttpInterceptor { intercept(req: HttpRequest, next: HttpHandler): Observable> { const token = this.authService.getAccessToken(); - if (token) { + if (token && !req.url.includes('/api/auth/refresh')) { req = req.clone({ setHeaders: { Authorization: `Bearer ${token}` }, }); @@ -66,6 +66,7 @@ export class JwtInterceptor implements HttpInterceptor { }), catchError((err) => { this.isRefreshing = false; + this.refreshTokenSubject.next(null); this.authService.clearSessionForLogout(); this.router.navigate(['/login']); return throwError(() => err);