Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes redirect on login (#6900) #6911

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cypress/e2e/auth_spec/redirect.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference types="cypress" />

import { cy, describe, it } from "local-cypress";

Om-Thorat marked this conversation as resolved.
Show resolved Hide resolved
describe("redirect", () => {
it("Check if login redirects to the right url", () => {
cy.log("Logging in the user staffdev:Coronasafe@123");

cy.visit("/resource/board");
cy.get("input[id='username']").type("staffdev");
Om-Thorat marked this conversation as resolved.
Show resolved Hide resolved
cy.get("input[id='password']").type("Coronasafe@123");
cy.get("button").contains("Login").click();

cy.get("p").contains("Sign Out").should("exist");
cy.url().should("include", "/resource/board");
});
});
20 changes: 12 additions & 8 deletions src/Providers/AuthUserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function AuthUserProvider({ children, unauthorized }: Props) {
localStorage.setItem(LocalStorageKeys.refreshToken, query.data.refresh);

await refetch();
navigate(getRedirectOr("/"));
navigate(getRedirectOr(location.pathname));
}

return query;
Expand All @@ -55,9 +55,7 @@ export default function AuthUserProvider({ children, unauthorized }: Props) {
localStorage.removeItem(LocalStorageKeys.refreshToken);

await refetch();

const redirectURL = getRedirectURL();
navigate(redirectURL ? `/?redirect=${redirectURL}` : "/");
navigate(getRedirectOr(`/login?redirect=${location.href}`));
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
}, [refetch]);

// Handles signout from current tab, if signed out from another tab.
Expand Down Expand Up @@ -118,18 +116,24 @@ const getRedirectURL = () => {

const getRedirectOr = (fallback: string) => {
const url = getRedirectURL();

const reserverdURLS = ["/login", "/session-expired"];
if (url) {
try {
const redirect = new URL(url);
console.log(redirect);
if (window.location.origin === redirect.origin) {
return redirect.pathname + redirect.search;
console.log(redirect);
return redirect.href;
Om-Thorat marked this conversation as resolved.
Show resolved Hide resolved
}
console.error("Redirect does not belong to same origin.");
} catch {
console.error(`Invalid redirect URL: ${url}`);
}
if (reserverdURLS.includes(location.pathname)) {
return "/";
}
return location.origin + location.pathname;
} else {
return fallback;
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
}

return fallback;
};