aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-08-02 10:06:57 -0700
committerAdam Soltys <[email protected]>2022-08-02 10:06:57 -0700
commit05f5001fc7d325e31d9c777be11343604c72fbfd (patch)
treee6f066b9df653d97d3c0b611cdfbb69e2bcc4098
parent63a4af3686b7a81874a87259f6ec5dd44aa97e48 (diff)
downloadcoinos-ui-05f5001fc7d325e31d9c777be11343604c72fbfd.tar.gz
coinos-ui-05f5001fc7d325e31d9c777be11343604c72fbfd.zip
catch login and registration errors
-rw-r--r--src/components/RegisterOrLogin.svelte14
-rw-r--r--src/lib/utils.js17
-rw-r--r--src/routes/__layout.svelte4
3 files changed, 27 insertions, 8 deletions
diff --git a/src/components/RegisterOrLogin.svelte b/src/components/RegisterOrLogin.svelte
index ae1948f5..157c2821 100644
--- a/src/components/RegisterOrLogin.svelte
+++ b/src/components/RegisterOrLogin.svelte
@@ -1,4 +1,5 @@
<script>
+ import { toast } from '@zerodevx/svelte-toast';
import { Icon } from '$comp';
import { post } from '$lib/utils';
import { user, token } from '$lib/store';
@@ -18,10 +19,15 @@
}[page];
let submit = async () => {
- let r = await post(url, { username, password });
- if (!r.token) r = await post('/login', { username, password });
- $token = r.token;
- auth();
+ try {
+ let r = await post(url, { username, password });
+ if (!r.token) r = await post('/login', { username, password });
+ $token = r.token;
+ auth();
+ } catch (e) {
+ if (!e.message) e.message = 'Login failed';
+ toast.push(e.message);
+ }
};
$: if ($user) goto(`/${username}/receive`);
diff --git a/src/lib/utils.js b/src/lib/utils.js
index db8a490a..669b2b5e 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -13,7 +13,7 @@ export const get = (url, headers = { accept: 'application/json' }) =>
try {
return JSON.parse(body);
} catch (e) {
- throw Error(body);
+ throw new Error(body);
}
});
@@ -22,7 +22,20 @@ export const post = (url, body) =>
method: 'POST',
body: JSON.stringify(body),
headers: { accept: 'application/json', 'content-type': 'application/json' }
- }).then((r) => r.json());
+ })
+ .then((r) => r.text())
+ .then((body) => {
+ try {
+ body = JSON.parse(body);
+ } catch (e) {
+ throw new Error(body);
+ }
+
+ if (body instanceof Error) throw body;
+ if (body.name === 'Error') throw new Error(body.message);
+
+ return body;
+ });
export const copy = (text) => {
navigator.clipboard.writeText(text);
diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte
index 406c29e2..ef4ba1d4 100644
--- a/src/routes/__layout.svelte
+++ b/src/routes/__layout.svelte
@@ -9,9 +9,9 @@
import { browser } from '$app/env';
import { goto } from '$app/navigation';
import { LoadingSplash } from '$comp';
+ import { protectedRoutes } from '$lib/utils';
browser && ($token ||= $session.token);
- let protectedRoutes = [/receive/, /customers/, /dashboard/, /settings/, /transactions/];
let ready = false;
onMount(() => {
@@ -20,7 +20,7 @@
goto('/login');
toast.push('Please sign in to continue.');
}
- user.subscribe((u) => (ready = true));
+ user.subscribe((u) => u?.username && (ready = true));
} else {
ready = true;
}