diff options
| author | secondl1ght <[email protected]> | 2022-08-02 13:24:51 -0600 |
|---|---|---|
| committer | secondl1ght <[email protected]> | 2022-08-02 13:24:51 -0600 |
| commit | 1b7bdfb4797c9c6d5aeab0149c5fed352df9080a (patch) | |
| tree | e770849d2aa0efde4fe01c3efdc4d31b7f0ba1fe /src | |
| parent | d99a3524fd2649f1a9ce3623333de56a99e06971 (diff) | |
| parent | 05f5001fc7d325e31d9c777be11343604c72fbfd (diff) | |
| download | coinos-ui-1b7bdfb4797c9c6d5aeab0149c5fed352df9080a.tar.gz coinos-ui-1b7bdfb4797c9c6d5aeab0149c5fed352df9080a.zip | |
Merge branch 'master' of github.com:coinos/coinos-ui-v2
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/RegisterOrLogin.svelte | 14 | ||||
| -rw-r--r-- | src/lib/utils.js | 17 |
2 files changed, 25 insertions, 6 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 6b728152..a812c136 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -14,7 +14,7 @@ export const get = (url, headers = { accept: 'application/json' }) => try { return JSON.parse(body); } catch (e) { - throw Error(body); + throw new Error(body); } }); @@ -23,7 +23,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); |
