From 3bc07b56cda6a1884f2e7604ca0ef9dd3c9d0337 Mon Sep 17 00:00:00 2001 From: Lucas Lara Date: Wed, 4 Jun 2025 10:07:18 +0200 Subject: [PATCH] refs #2171 Add LogoutGuard and update AuthService logout method to support redirect options --- ogWebconsole/src/app/app-routing.module.ts | 3 ++- .../manage-organizational-unit.component.spec.ts | 3 ++- ogWebconsole/src/app/guards/logout.guard.ts | 13 +++++++++++++ ogWebconsole/src/app/services/auth.service.ts | 6 ++++-- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 ogWebconsole/src/app/guards/logout.guard.ts diff --git a/ogWebconsole/src/app/app-routing.module.ts b/ogWebconsole/src/app/app-routing.module.ts index 501a3ad..72f410d 100644 --- a/ogWebconsole/src/app/app-routing.module.ts +++ b/ogWebconsole/src/app/app-routing.module.ts @@ -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 }, diff --git a/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.spec.ts b/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.spec.ts index 74e69bd..eadcfb7 100644 --- a/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.spec.ts +++ b/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.spec.ts @@ -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, diff --git a/ogWebconsole/src/app/guards/logout.guard.ts b/ogWebconsole/src/app/guards/logout.guard.ts new file mode 100644 index 0000000..1f1b671 --- /dev/null +++ b/ogWebconsole/src/app/guards/logout.guard.ts @@ -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; + } +} \ No newline at end of file diff --git a/ogWebconsole/src/app/services/auth.service.ts b/ogWebconsole/src/app/services/auth.service.ts index 48340ae..84fdabb 100644 --- a/ogWebconsole/src/app/services/auth.service.ts +++ b/ogWebconsole/src/app/services/auth.service.ts @@ -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']); + } } }