Skip to content

Commit 65d7992

Browse files
committed
fixup! fix: login redirect
1 parent 5b3d48c commit 65d7992

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

site/src/pages/LoginPage/LoginPage.tsx

+22-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const LoginPage: FC = () => {
2828
const navigate = useNavigate();
2929
const { metadata } = useEmbeddedMetadata();
3030
const buildInfoQuery = useQuery(buildInfo(metadata["build-info"]));
31+
let redirectError: Error | null = null;
3132
let redirectUrl: URL | null = null;
3233
try {
3334
redirectUrl = new URL(redirectTo);
@@ -49,19 +50,25 @@ export const LoginPage: FC = () => {
4950
});
5051
}, [isSignedIn, buildInfoQuery.data, user?.id]);
5152

52-
// The reason we need `window.location.href` for api redirects is that we
53-
// need the page to reload and make a request to the backend. If we use
54-
// `<Navigate>` react would handle the redirect itself and never request the
55-
// page from the backend.
56-
if (isSignedIn && isApiRouteRedirect) {
57-
const sanitizedUrl = new URL(redirectTo, window.location.origin);
58-
window.location.href = sanitizedUrl.pathname + sanitizedUrl.search;
59-
return null;
60-
}
61-
if (isSignedIn && !isApiRouteRedirect) {
62-
return (
63-
<Navigate to={redirectUrl ? redirectUrl.pathname : redirectTo} replace />
64-
);
53+
if (isSignedIn) {
54+
// The reason we need `window.location.href` for api redirects is that
55+
// we need the page to reload and make a request to the backend. If we
56+
// use `<Navigate>`, react would handle the redirect itself and never
57+
// request the page from the backend.
58+
if (isApiRouteRedirect) {
59+
const sanitizedUrl = new URL(redirectTo, window.location.origin);
60+
window.location.href = sanitizedUrl.pathname + sanitizedUrl.search;
61+
// Setting the href should immediately request a new page. Show an
62+
// error state if it doesn't.
63+
redirectError = new Error("unable to redirect")
64+
} else {
65+
return (
66+
<Navigate
67+
to={redirectUrl ? redirectUrl.pathname : redirectTo}
68+
replace
69+
/>
70+
);
71+
}
6572
}
6673

6774
if (isConfiguringTheFirstUser) {
@@ -75,14 +82,15 @@ export const LoginPage: FC = () => {
7582
</Helmet>
7683
<LoginPageView
7784
authMethods={authMethodsQuery.data}
78-
error={signInError}
85+
error={signInError ?? redirectError}
7986
isLoading={isLoading || authMethodsQuery.isLoading}
8087
buildInfo={buildInfoQuery.data}
8188
isSigningIn={isSigningIn}
8289
onSignIn={async ({ email, password }) => {
8390
await signIn(email, password);
8491
navigate("/");
8592
}}
93+
redirectTo={redirectTo}
8694
/>
8795
</>
8896
);

site/src/pages/LoginPage/LoginPageView.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CustomLogo } from "components/CustomLogo/CustomLogo";
55
import { Loader } from "components/Loader/Loader";
66
import { type FC, useState } from "react";
77
import { useLocation } from "react-router-dom";
8-
import { retrieveRedirect } from "utils/redirect";
98
import { SignInForm } from "./SignInForm";
109
import { TermsOfServiceLink } from "./TermsOfServiceLink";
1110

@@ -16,6 +15,7 @@ export interface LoginPageViewProps {
1615
buildInfo?: BuildInfoResponse;
1716
isSigningIn: boolean;
1817
onSignIn: (credentials: { email: string; password: string }) => void;
18+
redirectTo: string;
1919
}
2020

2121
export const LoginPageView: FC<LoginPageViewProps> = ({
@@ -25,9 +25,9 @@ export const LoginPageView: FC<LoginPageViewProps> = ({
2525
buildInfo,
2626
isSigningIn,
2727
onSignIn,
28+
redirectTo,
2829
}) => {
2930
const location = useLocation();
30-
const redirectTo = retrieveRedirect(location.search);
3131
// This allows messages to be displayed at the top of the sign in form.
3232
// Helpful for any redirects that want to inform the user of something.
3333
const message = new URLSearchParams(location.search).get("message");

0 commit comments

Comments
 (0)