fix token issues
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,5 @@
|
||||
"/oauth2": {
|
||||
"target": "http://127.0.0.1:8080",
|
||||
"secure": false
|
||||
},
|
||||
"/login": {
|
||||
"target": "http://127.0.0.1:8080",
|
||||
"secure": false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
15
frontend/src/app/core/guards/login.guard.ts
Normal file
15
frontend/src/app/core/guards/login.guard.ts
Normal file
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ export class JwtInterceptor implements HttpInterceptor {
|
||||
|
||||
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user