refactor: use window.location.href for redirects

This commit is contained in:
Stavros
2025-01-28 19:08:00 +02:00
parent 3efcb26db1
commit e2e3b3bdc6
4 changed files with 70 additions and 27 deletions

Binary file not shown.

View File

@@ -1,8 +1,9 @@
import { Button, Paper, Text } from "@mantine/core"; import { Button, Code, Paper, Text } from "@mantine/core";
import { notifications } from "@mantine/notifications"; import { notifications } from "@mantine/notifications";
import { Navigate } from "react-router"; import { Navigate } from "react-router";
import { useUserContext } from "../context/user-context"; import { useUserContext } from "../context/user-context";
import { Layout } from "../components/layouts/layout"; import { Layout } from "../components/layouts/layout";
import { ReactNode } from "react";
export const ContinuePage = () => { export const ContinuePage = () => {
const queryString = window.location.search; const queryString = window.location.search;
@@ -12,11 +13,11 @@ export const ContinuePage = () => {
const { isLoggedIn, disableContinue } = useUserContext(); const { isLoggedIn, disableContinue } = useUserContext();
if (!isLoggedIn) { if (!isLoggedIn) {
return <Navigate to="/login" />; return <Navigate to={`/login?redirect_uri=${redirectUri}`} />;
} }
if (disableContinue && redirectUri !== "null") { if (redirectUri === "null") {
window.location.replace(redirectUri!); return <Navigate to="/" />;
} }
const redirect = () => { const redirect = () => {
@@ -26,31 +27,62 @@ export const ContinuePage = () => {
color: "blue", color: "blue",
}); });
setTimeout(() => { setTimeout(() => {
window.location.replace(redirectUri!); window.location.href = redirectUri!;
}, 500); }, 500);
}; };
const urlParsed = URL.parse(redirectUri!);
if (
window.location.protocol === "https:" &&
urlParsed!.protocol === "http:"
) {
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
Insecure Redirect
</Text>
<Text>
Your are logged in but trying to redirect from <Code>https</Code> to{" "}
<Code>http</Code>, please click the button to redirect.
</Text>
<Button fullWidth mt="xl" onClick={redirect}>
Continue
</Button>
</ContinuePageLayout>
);
}
if (disableContinue) {
window.location.href = redirectUri!;
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
Redirecting
</Text>
<Text>You should be redirected to your app soon.</Text>
</ContinuePageLayout>
);
}
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
Continue
</Text>
<Text>Click the button to continue to your app.</Text>
<Button fullWidth mt="xl" onClick={redirect}>
Continue
</Button>
</ContinuePageLayout>
);
};
export const ContinuePageLayout = ({ children }: { children: ReactNode }) => {
return ( return (
<Layout> <Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder> <Paper shadow="md" p={30} mt={30} radius="md" withBorder>
{redirectUri !== "null" ? ( {children}
<>
<Text size="xl" fw={700}>
Continue
</Text>
<Text>Click the button to continue to your app.</Text>
<Button fullWidth mt="xl" onClick={redirect}>
Continue
</Button>
</>
) : (
<>
<Text size="xl" fw={700}>
Logged in
</Text>
<Text>You are now signed in and can use your apps.</Text>
</>
)}
</Paper> </Paper>
</Layout> </Layout>
); );

View File

@@ -65,8 +65,12 @@ export const LoginPage = () => {
color: "green", color: "green",
}); });
setTimeout(() => { setTimeout(() => {
window.location.replace(`/continue?redirect_uri=${redirectUri}`); if (redirectUri === "null") {
}); window.location.replace("/");
} else {
window.location.replace(`/continue?redirect_uri=${redirectUri}`);
}
}, 500);
}, },
}); });
@@ -84,7 +88,14 @@ export const LoginPage = () => {
}); });
}, },
onSuccess: (data) => { onSuccess: (data) => {
window.location.replace(data.data.url); notifications.show({
title: "Redirecting",
message: "Redirecting to your OAuth provider",
color: "blue",
});
setTimeout(() => {
window.location.href = data.data.url;
}, 500);
}, },
}); });

View File

@@ -32,7 +32,7 @@ export const LogoutPage = () => {
color: "green", color: "green",
}); });
setTimeout(() => { setTimeout(() => {
window.location.reload(); window.location.replace("/login");
}, 500); }, 500);
}, },
}); });