refs #2171 Add LogoutGuard and update AuthService logout method to support redirect options

pull/26/head
Lucas Lara García 2025-06-04 10:07:18 +02:00
parent e964c6b47a
commit 3bc07b56cd
4 changed files with 21 additions and 4 deletions

View File

@ -40,6 +40,7 @@ import {
RunScriptAssistantComponent
} from "./components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component";
import { roleGuard } from './guards/role.guard';
import { LogoutGuard } from './guards/logout.guard';
const routes: Routes = [
{ path: '', redirectTo: 'auth/login', pathMatch: 'full' },
{
@ -76,7 +77,7 @@ const routes: Routes = [
path: 'auth',
component: AuthLayoutComponent,
children: [
{ path: 'login', component: LoginComponent },
{ path: 'login', component: LoginComponent, canActivate: [LogoutGuard] },
],
},
{ path: '**', component: PageNotFoundComponent },

View File

@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ManageOrganizationalUnitComponent } from './manage-organizational-unit.component';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { ToastrModule } from 'ngx-toastr';
@ -35,6 +35,7 @@ describe('ManageOrganizationalUnitComponent', () => {
MatInputModule,
MatSelectModule,
MatSlideToggleModule,
MatDialogModule,
MatCheckboxModule,
TranslateModule.forRoot(),
BrowserAnimationsModule,

View File

@ -0,0 +1,13 @@
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthService } from '@services/auth.service';
@Injectable({ providedIn: 'root' })
export class LogoutGuard implements CanActivate {
constructor(private auth: AuthService) {}
canActivate(): boolean {
this.auth.logout({ redirect: false });
return true;
}
}

View File

@ -106,7 +106,7 @@ export class AuthService {
}
/** Logout: limpia tokens y redirige al login */
logout(): void {
logout(options: { redirect?: boolean } = { redirect: true }): void {
localStorage.removeItem('loginToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('isSuperAdmin');
@ -114,6 +114,8 @@ export class AuthService {
localStorage.removeItem('groupsView');
localStorage.removeItem('language');
this.tokenPayload = null;
this.router.navigate(['/auth/login']);
if (options.redirect && this.router.url !== '/auth/login') {
this.router.navigate(['/auth/login']);
}
}
}