@@ -28,6 +28,7 @@ export const LoginPage: FC = () => {
28
28
const navigate = useNavigate ( ) ;
29
29
const { metadata } = useEmbeddedMetadata ( ) ;
30
30
const buildInfoQuery = useQuery ( buildInfo ( metadata [ "build-info" ] ) ) ;
31
+ let redirectError : Error | null = null ;
31
32
let redirectUrl : URL | null = null ;
32
33
try {
33
34
redirectUrl = new URL ( redirectTo ) ;
@@ -49,19 +50,25 @@ export const LoginPage: FC = () => {
49
50
} ) ;
50
51
} , [ isSignedIn , buildInfoQuery . data , user ?. id ] ) ;
51
52
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
+ }
65
72
}
66
73
67
74
if ( isConfiguringTheFirstUser ) {
@@ -75,14 +82,15 @@ export const LoginPage: FC = () => {
75
82
</ Helmet >
76
83
< LoginPageView
77
84
authMethods = { authMethodsQuery . data }
78
- error = { signInError }
85
+ error = { signInError ?? redirectError }
79
86
isLoading = { isLoading || authMethodsQuery . isLoading }
80
87
buildInfo = { buildInfoQuery . data }
81
88
isSigningIn = { isSigningIn }
82
89
onSignIn = { async ( { email, password } ) => {
83
90
await signIn ( email , password ) ;
84
91
navigate ( "/" ) ;
85
92
} }
93
+ redirectTo = { redirectTo }
86
94
/>
87
95
</ >
88
96
) ;
0 commit comments