Repository: chaoss/grimoirelab-sortinghat (language: python)

Issue:
Redirect to the original URL after log in
When the user introduces an URL and they aren't log in, the system redirects to the log in page. After they are log in, the system sends the user to the main page and not to the one originally introduced by the user.

The system should redirect to the original URL after log in.


Discussion / hints:

Fix patch:
diff --git a/releases/unreleased/redirect-to-the-original-url-after-log-in.yml b/releases/unreleased/redirect-to-the-original-url-after-log-in.yml
new file mode 100644
index 00000000..6e7d31af
--- /dev/null
+++ b/releases/unreleased/redirect-to-the-original-url-after-log-in.yml
@@ -0,0 +1,8 @@
+---
+title: Redirect to the original URL after log in
+category: added
+author: Eva Millán <evamillan@bitergia.com>
+issue: 925
+notes: >
+  Users are now redirected to the page they originally
+  requested after they log in.
diff --git a/ui/src/router/index.js b/ui/src/router/index.js
index e4ef4e63..c47ff6c8 100644
--- a/ui/src/router/index.js
+++ b/ui/src/router/index.js
@@ -77,6 +77,9 @@ router.beforeEach((to, from, next) => {
     if (!isAuthenticated) {
       next({
         path: "/login",
+        query: {
+          redirect: to.fullPath,
+        },
       });
     } else {
       next();
diff --git a/ui/src/views/Login.vue b/ui/src/views/Login.vue
index a07809d5..22c34ee2 100644
--- a/ui/src/views/Login.vue
+++ b/ui/src/views/Login.vue
@@ -66,7 +66,7 @@ export default {
         };
         const response = await this.login(authDetails);
         if (response) {
-          this.$router.push("/");
+          this.$router.push(this.$route.query.redirect || "/");
           this.$logger.info(`Log in user ${this.username}`);
         }
       } catch (error) {


Test patch:
diff --git a/ui/tests/e2e/specs/spec.cy.js b/ui/tests/e2e/specs/spec.cy.js
index e554e921..9cb0f3c8 100644
--- a/ui/tests/e2e/specs/spec.cy.js
+++ b/ui/tests/e2e/specs/spec.cy.js
@@ -41,6 +41,23 @@ describe("Login", () => {
     cy.location("pathname").should("equal", "/login");
     cy.contains("Invalid credentials").should("be.visible");
   });
+
+  it("Redirects to the original URL after login", () => {
+    const path = "/settings/general";
+
+    cy.visit(path);
+    cy.location("pathname").should("equal", "/login");
+
+    cy.get("[id=username]").type(Cypress.env("USERNAME"));
+    cy.get("[id=password]").type(Cypress.env("PASSWORD"));
+    cy.contains("button", "Log in").click();
+    cy.wait("@auth")
+      .its("response.body.user")
+      .should("equal", Cypress.env("USERNAME"));
+
+    // Redirects to the original path
+    cy.location("pathname").should("equal", path);
+  });
 });
 
 describe("Authenticated operations", () => {