1
1
import { buildInfo } from "api/queries/buildInfo" ;
2
- import { regions } from "api/queries/regions" ;
2
+ // import { regions } from "api/queries/regions";
3
3
import { authMethods } from "api/queries/users" ;
4
4
import { useAuthContext } from "contexts/auth/AuthProvider" ;
5
5
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata" ;
@@ -29,20 +29,18 @@ export const LoginPage: FC = () => {
29
29
const navigate = useNavigate ( ) ;
30
30
const { metadata } = useEmbeddedMetadata ( ) ;
31
31
const buildInfoQuery = useQuery ( buildInfo ( metadata [ "build-info" ] ) ) ;
32
- const regionsQuery = useQuery ( regions ( metadata . regions ) ) ;
33
- const [ redirectError , setRedirectError ] = useState < Error | null > ( null ) ;
34
32
let redirectUrl : URL | null = null ;
35
33
try {
36
34
redirectUrl = new URL ( redirectTo ) ;
37
35
} catch {
38
36
// Do nothing
39
37
}
40
38
41
- const isApiRoute = redirectTo . startsWith ( "/api/v2" ) ;
42
- const isLocalRedirect =
39
+ const isApiRouteRedirect = redirectTo . startsWith ( "/api/v2" ) ;
40
+ const isReactRedirect =
43
41
( ! redirectUrl ||
44
42
( redirectUrl && redirectUrl . host === window . location . host ) ) &&
45
- ! isApiRoute ;
43
+ ! isApiRouteRedirect ;
46
44
47
45
useEffect ( ( ) => {
48
46
if ( ! buildInfoQuery . data || isSignedIn ) {
@@ -56,51 +54,12 @@ export const LoginPage: FC = () => {
56
54
} ) ;
57
55
} , [ isSignedIn , buildInfoQuery . data , user ?. id ] ) ;
58
56
59
- useEffect ( ( ) => {
60
- if ( ! isSignedIn || ! regionsQuery . data || isLocalRedirect ) {
61
- return ;
62
- }
63
-
64
- const regions = regionsQuery . data . regions ;
65
- // Process path app urls. They're in the form of https://dev.coder.com/test
66
- const pathUrls = regions
67
- ? regions
68
- . map ( ( region ) => {
69
- try {
70
- return new URL ( region . path_app_url ) ;
71
- } catch {
72
- return null ;
73
- }
74
- } )
75
- . filter ( ( url ) => url !== null )
76
- : [ ] ;
77
- // Process wildcard hostnames. They're in the form of `*.apps.dev.coder.com`.
78
- const wildcardHostnames = regions
79
- ? regions
80
- . map ( ( region ) => region . wildcard_hostname )
81
- . filter ( ( hostname ) => hostname !== "" )
82
- // remove the leading '*' from the hostname
83
- . map ( ( hostname ) => hostname . slice ( 1 ) )
84
- : [ ] ;
85
-
86
- // Ensure the redirect url matches one of the allowed options.
87
- const allowed =
88
- // For path URLs ensure just the hosts match.
89
- pathUrls . some ( ( url ) => url . host === window . location . host ) ||
90
- // For wildcards, ensure just the suffixes match.
91
- wildcardHostnames . some ( ( wildcard ) => redirectTo . endsWith ( wildcard ) ) ||
92
- // API routes need to be manually set with href, since react's
93
- // navigate will keep us within the SPA.
94
- isApiRoute ;
95
-
96
- if ( allowed ) {
97
- window . location . href = redirectTo ;
98
- } else {
99
- setRedirectError ( new Error ( "invalid redirect url" ) ) ;
100
- }
101
- } , [ isSignedIn , regionsQuery . data , redirectTo , isLocalRedirect , isApiRoute ] ) ;
102
-
103
- if ( isSignedIn && isLocalRedirect ) {
57
+ if ( isSignedIn && ! isReactRedirect ) {
58
+ const sanitizedUrl = new URL ( redirectTo , window . location . origin ) ;
59
+ window . location . href = sanitizedUrl . pathname + sanitizedUrl . search ;
60
+ return null ;
61
+ }
62
+ if ( isSignedIn && isReactRedirect ) {
104
63
return (
105
64
< Navigate to = { redirectUrl ? redirectUrl . pathname : redirectTo } replace />
106
65
) ;
@@ -117,10 +76,8 @@ export const LoginPage: FC = () => {
117
76
</ Helmet >
118
77
< LoginPageView
119
78
authMethods = { authMethodsQuery . data }
120
- error = { redirectError ?? signInError }
121
- isLoading = {
122
- isLoading || authMethodsQuery . isLoading || regionsQuery . isLoading
123
- }
79
+ error = { signInError }
80
+ isLoading = { isLoading || authMethodsQuery . isLoading }
124
81
buildInfo = { buildInfoQuery . data }
125
82
isSigningIn = { isSigningIn }
126
83
onSignIn = { async ( { email, password } ) => {
0 commit comments