fix token issues

This commit is contained in:
Krrish Ghimire
2026-07-06 00:58:11 +05:45
parent 874eb33678
commit f70fe81432
6 changed files with 26 additions and 8 deletions

View File

@@ -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',

View 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');
}
}

View File

@@ -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);