fix token issues
This commit is contained in:
@@ -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