From 5f13bc4957640ecd99ee6004876bb09de4e1e2a4 Mon Sep 17 00:00:00 2001 From: balzack Date: Mon, 12 Aug 2024 00:12:51 +0200 Subject: [PATCH] connecting login --- app/client/web/src/access/Access.tsx | 78 +++++++++++++++---- app/client/web/src/access/useAccess.hook.ts | 27 +++---- .../web/src/context/useAppContext.hook.ts | 17 ++-- 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/app/client/web/src/access/Access.tsx b/app/client/web/src/access/Access.tsx index 406afce0..039dbe20 100644 --- a/app/client/web/src/access/Access.tsx +++ b/app/client/web/src/access/Access.tsx @@ -12,7 +12,7 @@ import { TextInput, } from '@mantine/core' import { useDisclosure } from '@mantine/hooks' -import login from '../images/login.png' +import left from '../images/login.png' import { IconLock, IconUser, @@ -23,15 +23,24 @@ import { export function Access() { const { state, actions } = useAccess() - const [opened, { open, close }] = useDisclosure(false) + const [alertOpened, { open: alertOpen, close: alertClose }] = + useDisclosure(false) + const [urlOpened, { open: urlOpen, close: urlClose }] = useDisclosure(false) - console.log('AVAILABLE: ', state.availableSet) + const login = async () => { + try { + await actions.accountLogin() + } catch (err) { + console.log(err) + alertOpen() + } + } return (
{(state.display === 'medium' || state.display === 'large') && (
- +
)} {state.display != null && ( @@ -60,8 +69,12 @@ export function Access() { <> {state.strings.login} - actions.setPassword(event.currentTarget.value) } + onKeyDown={(ev) => { + if (ev.code === 'Enter' && state.password && state.username) + login() + }} /> - {state.strings.createAccount} - {(state.available === 0 || !state.availableSet) && ( {state.strings.admin} -
)} - + } placeholder={state.strings.host} value={state.node} + onKeyDown={(ev) => { + if (ev.code === 'Enter') urlClose() + }} onChange={(event) => actions.setNode(event.currentTarget.value)} /> + + {state.strings.tryAgain} + ) } diff --git a/app/client/web/src/access/useAccess.hook.ts b/app/client/web/src/access/useAccess.hook.ts index fb5f899c..10265d89 100644 --- a/app/client/web/src/access/useAccess.hook.ts +++ b/app/client/web/src/access/useAccess.hook.ts @@ -18,7 +18,8 @@ export function useAccess() { theme: '', language: '', node: '', - hostname: '', + secure: false, + host: '', available: 0, availableSet: false, themes: settings.state.themes, @@ -31,21 +32,21 @@ export function useAccess() { } useEffect(() => { - const { protocol, hostname, port } = location - setUrl(`${protocol}//${hostname}:${port}`) + const { protocol, host } = location + setUrl(`${protocol}//${host}`) }, []) const setUrl = (node: string) => { try { const url = new URL(node) - const { protocol, hostname, port } = url - getAvailable(`${hostname}:${port}`, protocol === 'https:') - updateState({ node, hostname: hostname }) + const { protocol, host } = url + getAvailable(host, protocol === 'https:') + updateState({ node, host, secure: protocol === 'https:' }) } catch (err) { console.log(err) - const { protocol, hostname, port } = location - getAvailable(`${hostname}:${port}`, protocol === 'https:') - updateState({ node, hostname: location.hostname }) + const { protocol, host } = location + getAvailable(host, protocol === 'https:') + updateState({ node, host, secure: protocol === 'https:' }) } } @@ -55,9 +56,6 @@ export function useAccess() { debounce.current = setTimeout(async () => { try { const available = await app.actions.getAvailable(node, secure) - - console.log('AVAILABLE: ', available) - updateState({ available, availableSet: true }) } catch (err) { console.log(err) @@ -103,7 +101,10 @@ export function useAccess() { setTheme: (theme: string) => { settings.actions.setTheme(theme) }, - login: () => {}, + accountLogin: async () => { + const { username, password, host, secure } = state + await app.actions.accountLogin(username, password, host, secure) + }, } return { state, actions } diff --git a/app/client/web/src/context/useAppContext.hook.ts b/app/client/web/src/context/useAppContext.hook.ts index 3e4c8a32..086984b5 100644 --- a/app/client/web/src/context/useAppContext.hook.ts +++ b/app/client/web/src/context/useAppContext.hook.ts @@ -26,7 +26,14 @@ export function useAppContext() { } const actions = { - accountLogin: async () => { + accountLogin: async ( + username: string, + password: string, + node: string, + secure: boolean + ) => { + console.log('LOGIN:', username, password, node, secure) + const params = { topicBatch: 16, tagBatch: 16, @@ -39,10 +46,10 @@ export function useAppContext() { appName: 'databag', } const login = await sdk.current.login( - 'asdf', - 'asdf', - 'balzack.coredb.org', - true, + username, + password, + node, + secure, null, params )