diff options
| author | secondl1ght <[email protected]> | 2022-10-25 18:01:56 -0600 |
|---|---|---|
| committer | secondl1ght <[email protected]> | 2022-10-25 18:01:56 -0600 |
| commit | a67b375473f1f40e1c0febfac16d7e36e0d72561 (patch) | |
| tree | 10a5fa750f2ccb53cff3510ab8fbbce108a35eb6 /src/lib/utils.js | |
| parent | aa6debcd83c0b4067e0f8ce9a11d9c9ac02476ad (diff) | |
| parent | 5a5ea3ccc327ff52fb1c313998d90e4bfefea90a (diff) | |
| download | coinos-ui-a67b375473f1f40e1c0febfac16d7e36e0d72561.tar.gz coinos-ui-a67b375473f1f40e1c0febfac16d7e36e0d72561.zip | |
Merge branch 'master' of github.com:coinos/coinos-ui-v2
Diffstat (limited to 'src/lib/utils.js')
| -rw-r--r-- | src/lib/utils.js | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/lib/utils.js b/src/lib/utils.js index 54e965d9..7eeab648 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -9,6 +9,17 @@ export function scroll(section) { const base = browser ? '' : env.PUBLIC_COINOS_URL; +export const g = (url, fetch, headers) => + fetch(base + url, { headers }) + .then((r) => r.text()) + .then((body) => { + try { + return JSON.parse(body); + } catch (e) { + throw new Error(body); + } + }); + export const get = (url, headers = {}) => fetch(base + url, { headers }) .then((r) => r.text()) @@ -31,6 +42,8 @@ export const post = (url, body, headers) => { throw new Error(body); } + + if (body.error) throw body.error; if (body instanceof Error) throw body; if (body.name === 'Error') throw new Error(body.message); @@ -97,7 +110,17 @@ export const info = (m) => { export const login = async (user, cookies) => { let maxAge = 30 * 24 * 60 * 60; - let { token } = await post('/login', user); + let res = await fetch(base + '/login', { + method: 'POST', + body: JSON.stringify(user), + headers: { 'content-type': 'application/json', accept: 'application/json' } + }); + if (res.status === 401) { + let text = await res.text(); + if (text.startsWith('2fa')) throw new Error('2fa'); + } + + let { token } = await res.json(); if (!token) throw new Error('Login failed'); let expires = new Date(); |
