From c4c02dc00ed47a6570a25ad4c572f4fa5a9ef3c8 Mon Sep 17 00:00:00 2001 From: balzack Date: Tue, 13 Aug 2024 00:20:52 +0200 Subject: [PATCH] support mfa on login --- app/client/web/src/access/Access.module.css | 9 +++++- app/client/web/src/access/Access.tsx | 35 ++++++++++++++++----- app/client/web/src/access/useAccess.hook.ts | 2 -- app/sdk/src/net/setLogin.ts | 10 +++--- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/app/client/web/src/access/Access.module.css b/app/client/web/src/access/Access.module.css index 9e417e28..0015b4db 100644 --- a/app/client/web/src/access/Access.module.css +++ b/app/client/web/src/access/Access.module.css @@ -8,7 +8,7 @@ } .mfaPin { - padding: 32px; + padding-top: 32px; } .mfaDescription { @@ -19,6 +19,13 @@ display: flex; gap: 8px; } + + .mfaDisabled { + height: 48px; + display: flex; + align-items: center; + color: var(--mantine-color-red-6); + } } .split { diff --git a/app/client/web/src/access/Access.tsx b/app/client/web/src/access/Access.tsx index 051391f5..bd5304ac 100644 --- a/app/client/web/src/access/Access.tsx +++ b/app/client/web/src/access/Access.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useState } from 'react' import { useAccess } from './useAccess.hook' import classes from './Access.module.css' import { @@ -28,13 +28,11 @@ export function Access() { useDisclosure(false) const [urlOpened, { open: urlOpen, close: urlClose }] = useDisclosure(false) const [otpOpened, { open: otpOpen, close: otpClose }] = useDisclosure(false) - -console.log("LANG: ", state.language); + const [disabled, setDisabled] = useState(false) const login = async () => { if (!state.loading) { actions.setLoading(true) - otpClose() try { if (state.mode === 'account') { await actions.accountLogin() @@ -45,9 +43,23 @@ console.log("LANG: ", state.language); } else if (state.mode === 'admin') { await actions.adminLogin() } + otpClose() } catch (err) { - console.log(err) - alertOpen() + console.log(err.message) + if ( + err.message === '405' || + err.message === '403' || + err.message === '429' + ) { + if (err.message === '429') { + setDisabled(true) + } else { + setDisabled(false) + } + otpOpen() + } else { + alertOpen() + } } actions.setLoading(false) } @@ -351,15 +363,24 @@ console.log("LANG: ", state.language);
{state.strings.mfaTitle}
{state.strings.mfaEnter}
actions.setCode(event)} /> +
+ {disabled ? state.strings.mfaDisabled : ''} +
-
diff --git a/app/client/web/src/access/useAccess.hook.ts b/app/client/web/src/access/useAccess.hook.ts index f52ebf18..305692bc 100644 --- a/app/client/web/src/access/useAccess.hook.ts +++ b/app/client/web/src/access/useAccess.hook.ts @@ -98,8 +98,6 @@ export function useAccess() { secure ) updateState({ taken: !available }) - - console.log('TAKEN: ', taken) }, 2000) } diff --git a/app/sdk/src/net/setLogin.ts b/app/sdk/src/net/setLogin.ts index 3ad3b261..0890077e 100644 --- a/app/sdk/src/net/setLogin.ts +++ b/app/sdk/src/net/setLogin.ts @@ -5,9 +5,11 @@ export async function setLogin(node: string, secure: boolean, username: string, const mfa = code ? `&code=${code}` : ''; const endpoint = `http${secure ? 's' : ''}://${node}/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=${pushType}${mfa}`; const auth = encode(`${username}:${password}`); - const response = await axios.post(endpoint, notifications, { auth: `Basic ${auth}` }); - if (response.status >= 400 && response.status < 600) { - throw new Error('setLogin fetch failed'); + try { + const response = await axios.post(endpoint, notifications, { auth: `Basic ${auth}` }); + return response.data; + } + catch(err) { + throw new Error(err.status); } - return response.data; }