fix token issues
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.krrishg.config;
|
package com.krrishg.config;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.JwtException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -16,6 +17,11 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
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)
|
@ExceptionHandler(IllegalArgumentException.class)
|
||||||
public ProblemDetail handleBadRequest(IllegalArgumentException ex) {
|
public ProblemDetail handleBadRequest(IllegalArgumentException ex) {
|
||||||
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage());
|
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage());
|
||||||
|
|||||||
@@ -134,9 +134,6 @@ public class SecurityConfig {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
org.springframework.security.core.context.SecurityContextHolder
|
org.springframework.security.core.context.SecurityContextHolder
|
||||||
.clearContext();
|
.clearContext();
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
|
|
||||||
"Invalid or expired token");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,5 @@
|
|||||||
"/oauth2": {
|
"/oauth2": {
|
||||||
"target": "http://127.0.0.1:8080",
|
"target": "http://127.0.0.1:8080",
|
||||||
"secure": false
|
"secure": false
|
||||||
},
|
|
||||||
"/login": {
|
|
||||||
"target": "http://127.0.0.1:8080",
|
|
||||||
"secure": false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
import { AuthGuard } from './core/guards/auth.guard';
|
import { AuthGuard } from './core/guards/auth.guard';
|
||||||
|
import { LoginGuard } from './core/guards/login.guard';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
|
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
|
||||||
{
|
{
|
||||||
path: 'login',
|
path: 'login',
|
||||||
loadComponent: () => import('./auth/login/login.component').then(m => m.LoginComponent),
|
loadComponent: () => import('./auth/login/login.component').then(m => m.LoginComponent),
|
||||||
|
canActivate: [LoginGuard],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'register',
|
path: 'register',
|
||||||
loadComponent: () => import('./auth/register/register.component').then(m => m.RegisterComponent),
|
loadComponent: () => import('./auth/register/register.component').then(m => m.RegisterComponent),
|
||||||
|
canActivate: [LoginGuard],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'oauth2/callback',
|
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>> {
|
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||||
const token = this.authService.getAccessToken();
|
const token = this.authService.getAccessToken();
|
||||||
if (token) {
|
if (token && !req.url.includes('/api/auth/refresh')) {
|
||||||
req = req.clone({
|
req = req.clone({
|
||||||
setHeaders: { Authorization: `Bearer ${token}` },
|
setHeaders: { Authorization: `Bearer ${token}` },
|
||||||
});
|
});
|
||||||
@@ -66,6 +66,7 @@ export class JwtInterceptor implements HttpInterceptor {
|
|||||||
}),
|
}),
|
||||||
catchError((err) => {
|
catchError((err) => {
|
||||||
this.isRefreshing = false;
|
this.isRefreshing = false;
|
||||||
|
this.refreshTokenSubject.next(null);
|
||||||
this.authService.clearSessionForLogout();
|
this.authService.clearSessionForLogout();
|
||||||
this.router.navigate(['/login']);
|
this.router.navigate(['/login']);
|
||||||
return throwError(() => err);
|
return throwError(() => err);
|
||||||
|
|||||||
Reference in New Issue
Block a user