Forráskód Böngészése

feat: implement session expiration handling and simplify logout process across layout components

EmilianoChavarria 3 hete
szülő
commit
5b4618bdc2

+ 0 - 6
Front/src/app/modules/Administrador/pages/layout-page/layout-page.component.ts

@@ -225,14 +225,8 @@ export class LayoutPageComponent {
         confirmButtonColor: 'rgb(237,46,56)',
         confirmButtonText: 'Ok'
       }).then(() => {
-        this._authService.logout().subscribe(() => {
           localStorage.clear();
           this._router.navigate(['']); // ruta login explícita
-        }, () => {
-          // Incluso si falla el logout del backend, borra y navega
-          localStorage.clear();
-          this._router.navigate(['']);
-        });
       });
     });
     

+ 21 - 6
Front/src/app/modules/Alumno/pages/layout-page/layout-page.component.ts

@@ -7,6 +7,7 @@ import { Keepalive } from '@ng-idle/keepalive';
 import { MatDialog } from '@angular/material/dialog';
 import { Router } from '@angular/router';
 import Swal from 'sweetalert2';
+import { NavbarHomeComponent } from '../../../../shared/navbar-home/navbar-home.component';
 
 @Component({
   selector: 'app-layout-page',
@@ -35,6 +36,7 @@ export class LayoutPageComponent implements OnInit {
     private keepalive: Keepalive,
     public dialog: MatDialog,
     private _router: Router,
+    private navbarHome: NavbarHomeComponent
   ) { }
 
 
@@ -63,6 +65,25 @@ export class LayoutPageComponent implements OnInit {
 
     })
 
+    const expira = localStorage.getItem('expira');
+    if (expira) {
+      const expiraDate = new Date(parseInt(expira, 10));
+      const now = new Date();
+
+      if (now > expiraDate) {
+
+        console.log('La fecha de expiración ha pasado.');
+        Swal.fire({
+          icon: 'info',
+          title: 'Ha estado inactivo por mucho tiempo, por favor inicie sesión nuevamente.'
+        })
+        // this.navbarHome.actualizarActividad();
+        this.navbarHome.logout();
+      } else {
+        console.log('La fecha de expiración no ha pasado.');
+      }
+    }
+
     let userDataS = localStorage.getItem('userDataS');
     if (!userDataS) {
       // Configuración de idle
@@ -83,14 +104,8 @@ export class LayoutPageComponent implements OnInit {
           confirmButtonColor: 'rgb(237,46,56)',
           confirmButtonText: 'Ok'
         }).then(() => {
-          this._authService.logout().subscribe(() => {
             localStorage.clear();
             this._router.navigate(['']); // ruta login explícita
-          }, () => {
-            // Incluso si falla el logout del backend, borra y navega
-            localStorage.clear();
-            this._router.navigate(['']);
-          });
         });
       });
       

+ 21 - 6
Front/src/app/modules/Padres/pages/layout-page/layout-page.component.ts

@@ -10,6 +10,7 @@ import { ActividadService } from '../../../Administrador/services/actividad.serv
 import { ActividadUser } from '../../../Administrador/interfaces/ActividadUser.interface';
 import { format } from 'date-fns';
 import Swal from 'sweetalert2';
+import { NavbarHomeComponent } from '../../../../shared/navbar-home/navbar-home.component';
 
 @Component({
   selector: 'app-layout-page',
@@ -57,6 +58,7 @@ export class LayoutPageComponent {
     private _enviarInfo: EnviarInfoService,
     private authService: AuthService,
     private actividadService: ActividadService,
+    private navbarHome: NavbarHomeComponent
     // private navbarHome: NavbarHomeComponent
   ) {
     
@@ -189,6 +191,25 @@ export class LayoutPageComponent {
       this._enviarInfo.changeColorLinks(this.links);
     })
 
+    const expira = localStorage.getItem('expira');
+    if (expira) {
+      const expiraDate = new Date(parseInt(expira, 10));
+      const now = new Date();
+
+      if (now > expiraDate) {
+
+        console.log('La fecha de expiración ha pasado.');
+        Swal.fire({
+          icon: 'info',
+          title: 'Ha estado inactivo por mucho tiempo, por favor inicie sesión nuevamente.'
+        })
+        // this.navbarHome.actualizarActividad();
+        this.navbarHome.logout();
+      } else {
+        console.log('La fecha de expiración no ha pasado.');
+      }
+    }
+
     // const actual = new Date();
     // console.log(actual.toLocaleTimeString());
 
@@ -212,14 +233,8 @@ export class LayoutPageComponent {
           confirmButtonColor: 'rgb(237,46,56)',
           confirmButtonText: 'Ok'
         }).then(() => {
-          this._authService.logout().subscribe(() => {
             localStorage.clear();
             this._router.navigate(['']); // ruta login explícita
-          }, () => {
-            // Incluso si falla el logout del backend, borra y navega
-            localStorage.clear();
-            this._router.navigate(['']);
-          });
         });
       });
       

+ 22 - 0
Front/src/app/modules/Profesor/pages/layout-page/layout-page.component.ts

@@ -7,6 +7,7 @@ import { Keepalive } from '@ng-idle/keepalive';
 import { MatDialog } from '@angular/material/dialog';
 import { Router } from '@angular/router';
 import Swal from 'sweetalert2';
+import { NavbarHomeComponent } from '../../../../shared/navbar-home/navbar-home.component';
 
 @Component({
   selector: 'app-layout-page',
@@ -35,6 +36,7 @@ export class LayoutPageComponent {
     private keepalive: Keepalive,
     public dialog: MatDialog,
     private _router: Router,
+    private navbarHome: NavbarHomeComponent
   ) { }
 
 
@@ -59,6 +61,26 @@ export class LayoutPageComponent {
       this._enviarInfo.changeTextColor(this.fontColorBar);
       this._enviarInfo.changeColorLinks(this.links);
     })
+
+    const expira = localStorage.getItem('expira');
+    if (expira) {
+      const expiraDate = new Date(parseInt(expira, 10));
+      const now = new Date();
+
+      if (now > expiraDate) {
+
+        console.log('La fecha de expiración ha pasado.');
+        Swal.fire({
+          icon: 'info',
+          title: 'Ha estado inactivo por mucho tiempo, por favor inicie sesión nuevamente.'
+        })
+        // this.navbarHome.actualizarActividad();
+        this.navbarHome.logout();
+      } else {
+        console.log('La fecha de expiración no ha pasado.');
+      }
+    }
+
     let userDataS = localStorage.getItem('userDataS');
     if (!userDataS) {
       // Configuración de idle