From 904e425c0fbeb649059c609b9d0628923bbb0754 Mon Sep 17 00:00:00 2001
From: ncamera <nico_camera98@hotmail.com>
Date: Fri, 11 Nov 2022 02:20:53 -0300
Subject: [PATCH] Se arreglan redirecciones al login
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

No estaba funcionando bien la redirección luego del logout, al igual que tampoco estaba funcionando la redirección con la URL vacía. En ambos casos se enviaba a la pantalla de error (404).

Uno de los motivos por los que esto sucedía, era porque no se borraba de la sesión el currentUser al hacer logout.
---
 .../src/app/app-routing.module.ts             | 22 ++++++++++++++-----
 .../src/app/layout/layout.component.ts        |  6 ++---
 .../src/app/not-found/not-found.component.ts  |  6 +++--
 .../components/header/header.component.ts     | 10 ++++++---
 .../src/app/shared/guards/auth.guard.ts       | 22 ++++++++-----------
 .../src/app/shared/services/ghci.service.ts   |  8 +++----
 .../app/shared/services/haskell.service.ts    |  6 ++---
 .../app/shared/services/usuario.service.ts    |  6 ++---
 8 files changed, 49 insertions(+), 37 deletions(-)

diff --git a/Frontend Angular 4/src/app/app-routing.module.ts b/Frontend Angular 4/src/app/app-routing.module.ts
index 02db45e..2e6a3ff 100755
--- a/Frontend Angular 4/src/app/app-routing.module.ts	
+++ b/Frontend Angular 4/src/app/app-routing.module.ts	
@@ -6,12 +6,14 @@ import { AuthGuard } from "./shared/guards/auth.guard";
 const routes: Routes = [
   {
     path: "",
-    loadChildren: () => import('./layout/layout.module').then(m => m.LayoutModule),
+    loadChildren: () =>
+      import("./layout/layout.module").then((m) => m.LayoutModule),
     canActivate: [AuthGuard],
   },
   {
     path: "login",
-    loadChildren: () => import('./login/login.module').then(m => m.LoginModule),
+    loadChildren: () =>
+      import("./login/login.module").then((m) => m.LoginModule),
     data: {
       language:
         navigator.language &&
@@ -23,23 +25,31 @@ const routes: Routes = [
   },
   {
     path: "es/login",
-    loadChildren: () => import('./login/login.module').then(m => m.LoginModule),
+    loadChildren: () =>
+      import("./login/login.module").then((m) => m.LoginModule),
     data: { language: "es" },
   },
   {
     path: "en/login",
-    loadChildren: () => import('./login/login.module').then(m => m.LoginModule),
+    loadChildren: () =>
+      import("./login/login.module").then((m) => m.LoginModule),
     data: { language: "en" },
   },
   {
     path: "not-found",
-    loadChildren: () => import('./not-found/not-found.module').then(m => m.NotFoundModule),
+    loadChildren: () =>
+      import("./not-found/not-found.module").then((m) => m.NotFoundModule),
   },
   { path: "**", redirectTo: "not-found" },
 ];
 
 @NgModule({
-  imports: [RouterModule.forRoot(routes, { useHash: true, relativeLinkResolution: 'legacy' })],
+  imports: [
+    RouterModule.forRoot(routes, {
+      useHash: true,
+      relativeLinkResolution: "legacy",
+    }),
+  ],
   exports: [RouterModule],
 })
 export class AppRoutingModule {}
diff --git a/Frontend Angular 4/src/app/layout/layout.component.ts b/Frontend Angular 4/src/app/layout/layout.component.ts
index 2cd5a97..48f2e15 100755
--- a/Frontend Angular 4/src/app/layout/layout.component.ts	
+++ b/Frontend Angular 4/src/app/layout/layout.component.ts	
@@ -18,9 +18,9 @@ export class LayoutComponent implements OnInit {
   }
   ngOnInit() {
     if (this.router.url === "/") {
-      this.router.navigate([
-        "/" + this.translateService.get("i18n.code").value + "/login",
-      ]);
+      this.translateService
+        .get("i18n.code")
+        .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
     }
   }
 }
diff --git a/Frontend Angular 4/src/app/not-found/not-found.component.ts b/Frontend Angular 4/src/app/not-found/not-found.component.ts
index cf853e6..6b46f94 100755
--- a/Frontend Angular 4/src/app/not-found/not-found.component.ts	
+++ b/Frontend Angular 4/src/app/not-found/not-found.component.ts	
@@ -12,7 +12,9 @@ export class NotFoundComponent {
 
   constructor(public translate: TranslateService) {
     this.translateService = translate;
-    this.urlLogin =
-      "/" + this.translateService.get("i18n.code").value + "/login";
+
+    this.translateService.get("i18n.code").subscribe((res) => {
+      this.urlLogin = "/" + res + "/login";
+    });
   }
 }
diff --git a/Frontend Angular 4/src/app/shared/components/header/header.component.ts b/Frontend Angular 4/src/app/shared/components/header/header.component.ts
index 7eefc9c..359b3fb 100755
--- a/Frontend Angular 4/src/app/shared/components/header/header.component.ts	
+++ b/Frontend Angular 4/src/app/shared/components/header/header.component.ts	
@@ -37,10 +37,14 @@ export class HeaderComponent implements OnInit {
   }
 
   logout() {
+    // Se borra de la sesión la información del login
+    this.authService.logout();
+
     this.sessionService.reset();
     this.ghciService.desconectarWS();
-    this.router.navigate([
-      "/" + this.translateService.get("i18n.code").value + "/login",
-    ]);
+
+    this.translateService
+      .get("i18n.code")
+      .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
   }
 }
diff --git a/Frontend Angular 4/src/app/shared/guards/auth.guard.ts b/Frontend Angular 4/src/app/shared/guards/auth.guard.ts
index 91de890..5f93120 100755
--- a/Frontend Angular 4/src/app/shared/guards/auth.guard.ts	
+++ b/Frontend Angular 4/src/app/shared/guards/auth.guard.ts	
@@ -1,10 +1,5 @@
 import { Injectable } from "@angular/core";
-import {
-  CanActivate,
-  ActivatedRouteSnapshot,
-  RouterStateSnapshot,
-  Router,
-} from "@angular/router";
+import { CanActivate, Router } from "@angular/router";
 import { Observable } from "rxjs";
 import { TranslateService } from "@ngx-translate/core";
 
@@ -16,16 +11,17 @@ export class AuthGuard implements CanActivate {
     this.translateService = translate;
   }
 
-  canActivate(
-    next: ActivatedRouteSnapshot,
-    state: RouterStateSnapshot
-  ): Observable<boolean> | Promise<boolean> | boolean {
+  canActivate(): Observable<boolean> | Promise<boolean> | boolean {
+    // El usuario está logeado
     if (sessionStorage.getItem("currentUser")) {
       return true;
     }
-    this.router.navigate([
-      "/" + this.translateService.get("i18n.code").value + "/login",
-    ]);
+
+    // Se navega al login dependiendo del idioma
+    this.translateService
+      .get("i18n.code")
+      .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
+
     return false;
   }
 }
diff --git a/Frontend Angular 4/src/app/shared/services/ghci.service.ts b/Frontend Angular 4/src/app/shared/services/ghci.service.ts
index f3a7732..2e07626 100755
--- a/Frontend Angular 4/src/app/shared/services/ghci.service.ts	
+++ b/Frontend Angular 4/src/app/shared/services/ghci.service.ts	
@@ -108,11 +108,11 @@ export class GHCIService {
         console.log("Conexión con web socket exitosa");
       };
       this.connection.onclose = function (reason) {
-        //Codigo que indica la falta de permisos (sesion expirada por ejemplo)
+        // Codigo que indica la falta de permisos (sesion expirada por ejemplo)
         if (reason.code == 1008) {
-          this.router.navigate([
-            "/" + this.translateService.get("i18n.code").value + "/login",
-          ]);
+          this.translateService
+            .get("i18n.code")
+            .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
         }
         console.log("Conexión con web socket cerrada", reason);
       }.bind(this);
diff --git a/Frontend Angular 4/src/app/shared/services/haskell.service.ts b/Frontend Angular 4/src/app/shared/services/haskell.service.ts
index 690eab3..292fbc9 100755
--- a/Frontend Angular 4/src/app/shared/services/haskell.service.ts	
+++ b/Frontend Angular 4/src/app/shared/services/haskell.service.ts	
@@ -161,9 +161,9 @@ export class HaskellService {
    */
   private handleError(error: any) {
     if (error.status == 401) {
-      this.router.navigate([
-        "/" + this.translateService.get("i18n.code").value + "/login",
-      ]);
+      this.translateService
+        .get("i18n.code")
+        .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
     }
     // In a real world app, we might use a remote logging infrastructure
     // We'd also dig deeper into the error to get a better message
diff --git a/Frontend Angular 4/src/app/shared/services/usuario.service.ts b/Frontend Angular 4/src/app/shared/services/usuario.service.ts
index f470cb8..b7ea17e 100755
--- a/Frontend Angular 4/src/app/shared/services/usuario.service.ts	
+++ b/Frontend Angular 4/src/app/shared/services/usuario.service.ts	
@@ -47,9 +47,9 @@ export class UsuarioService {
 
   private handleError(error: any) {
     if (error.status == 401) {
-      this.router.navigate([
-        "/" + this.translateService.get("i18n.code").value + "/login",
-      ]);
+      this.translateService
+        .get("i18n.code")
+        .subscribe((res) => this.router.navigate(["/" + res + "/login"]));
     }
     let errMsg = error.message
       ? error.message
-- 
GitLab