Skip to content

Commit 5e80cf9

Browse files
committed
Added validation of custom URL before it will save
1 parent da6d1dc commit 5e80cf9

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

Auth/Authentication.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ async function storeCredentials(
9595
refreshToken,
9696
options,
9797
);
98-
// if (store_success && refresh_store_success) {
99-
// return true;
100-
// }
101-
// return false;
98+
10299
return store_success && refresh_store_success;
103100
}
104101

Auth/EnvironmentSelect.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,29 @@ export default function EnvironmentSelect({ visible, onDismiss }: EnvironmentSel
1313
const { environment, setEnvironment } = useAppEnvironment();
1414
const [selectedEnv, setSelectedEnv] = useState<string>(environment.name.toLowerCase());
1515
const [customUrl, setCustomUrl] = useState<string>('');
16+
const [error, setError] = useState<string | null>(null);
1617

1718
useEffect(() => {
1819
if (visible) {
1920
setSelectedEnv(environment.name.toLowerCase());
20-
setCustomUrl('');
21+
setCustomUrl(environment.production ? '' : environment.baseUrl.toLowerCase());
22+
setError(null);
2123
}
2224
}, [visible]);
2325

26+
async function validateUrl(url: string) {
27+
const controller = new AbortController();
28+
const id = setTimeout(() => controller.abort(), 3000);
29+
try {
30+
const resp = await fetch(`${url}/api/v1/info`, { method: "GET", signal: controller.signal });
31+
clearTimeout(id);
32+
return resp.ok;
33+
} catch (error: unknown) {
34+
clearTimeout(id);
35+
return false;
36+
}
37+
}
38+
2439
return (
2540
<Modal visible={visible} onDismiss={onDismiss} contentContainerStyle={styles.sheet}>
2641
<Text style={styles.title}>Change Server</Text>
@@ -45,12 +60,22 @@ export default function EnvironmentSelect({ visible, onDismiss }: EnvironmentSel
4560
/>
4661
)}
4762

63+
{error && (
64+
<Text style={styles.errorText}>
65+
{error}
66+
</Text>
67+
)}
68+
4869
<RoundedButton
4970
title="Save Changes"
50-
onPress={() => {
71+
onPress={async () => {
5172
if (selectedEnv === 'other') {
5273
if (!customUrl.trim()) {
53-
onDismiss();
74+
setError('Custom URL cannot be empty.');
75+
return;
76+
}
77+
if (!await validateUrl(customUrl.trim())) {
78+
setError('Custom URL is invalid.');
5479
return;
5580
}
5681
setEnvironment({
@@ -61,6 +86,7 @@ export default function EnvironmentSelect({ visible, onDismiss }: EnvironmentSel
6186
} else {
6287
setEnvironment(APP_ENVIRONMENTS[selectedEnv]);
6388
}
89+
setError(null);
6490
onDismiss();
6591
}}
6692
/>
@@ -93,4 +119,9 @@ const styles = StyleSheet.create({
93119
marginBottom: 16,
94120
textAlign: 'center',
95121
},
122+
errorText: {
123+
color: 'red',
124+
fontSize: 12,
125+
marginTop: 6,
126+
},
96127
});

0 commit comments

Comments
 (0)