connecting login

This commit is contained in:
balzack 2024-08-12 00:12:51 +02:00
parent 098f095b42
commit 5f13bc4957
3 changed files with 90 additions and 32 deletions

View File

@ -12,7 +12,7 @@ import {
TextInput, TextInput,
} from '@mantine/core' } from '@mantine/core'
import { useDisclosure } from '@mantine/hooks' import { useDisclosure } from '@mantine/hooks'
import login from '../images/login.png' import left from '../images/login.png'
import { import {
IconLock, IconLock,
IconUser, IconUser,
@ -23,15 +23,24 @@ import {
export function Access() { export function Access() {
const { state, actions } = useAccess() 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 ( return (
<div className={classes.split}> <div className={classes.split}>
{(state.display === 'medium' || state.display === 'large') && ( {(state.display === 'medium' || state.display === 'large') && (
<div className={classes.left}> <div className={classes.left}>
<Image className={classes.splash} src={login} fit="contain" /> <Image className={classes.splash} src={left} fit="contain" />
</div> </div>
)} )}
{state.display != null && ( {state.display != null && (
@ -60,8 +69,12 @@ export function Access() {
<> <>
<Title order={3}>{state.strings.login}</Title> <Title order={3}>{state.strings.login}</Title>
<Space h="md" /> <Space h="md" />
<Button size="compact-sm" variant="transparent" onClick={open}> <Button
{state.hostname} size="compact-sm"
variant="transparent"
onClick={urlOpen}
>
{state.host}
</Button> </Button>
<TextInput <TextInput
className={classes.input} className={classes.input}
@ -81,9 +94,18 @@ export function Access() {
onChange={(event) => onChange={(event) =>
actions.setPassword(event.currentTarget.value) actions.setPassword(event.currentTarget.value)
} }
onKeyDown={(ev) => {
if (ev.code === 'Enter' && state.password && state.username)
login()
}}
/> />
<Space h="md" /> <Space h="md" />
<Button variant="filled" className={classes.submit}> <Button
variant="filled"
className={classes.submit}
onClick={login}
disabled={!state.username || !state.password}
>
{state.strings.login} {state.strings.login}
</Button> </Button>
<Button <Button
@ -106,8 +128,12 @@ export function Access() {
<> <>
<Title order={3}>{state.strings.accessAccount}</Title> <Title order={3}>{state.strings.accessAccount}</Title>
<Space h="md" /> <Space h="md" />
<Button size="compact-sm" variant="transparent" onClick={open}> <Button
{state.hostname} size="compact-sm"
variant="transparent"
onClick={urlOpen}
>
{state.host}
</Button> </Button>
<TextInput <TextInput
className={classes.input} className={classes.input}
@ -136,8 +162,12 @@ export function Access() {
<> <>
<Title order={3}>{state.strings.createAccount}</Title> <Title order={3}>{state.strings.createAccount}</Title>
<Space h="md" /> <Space h="md" />
<Button size="compact-sm" variant="transparent" onClick={open}> <Button
{state.hostname} size="compact-sm"
variant="transparent"
onClick={urlOpen}
>
{state.host}
</Button> </Button>
{(state.available === 0 || !state.availableSet) && ( {(state.available === 0 || !state.availableSet) && (
<TextInput <TextInput
@ -197,8 +227,12 @@ export function Access() {
<> <>
<Title order={3}>{state.strings.admin}</Title> <Title order={3}>{state.strings.admin}</Title>
<Space h="md" /> <Space h="md" />
<Button size="compact-sm" variant="transparent" onClick={open}> <Button
{state.hostname} size="compact-sm"
variant="transparent"
onClick={urlOpen}
>
{state.host}
</Button> </Button>
<PasswordInput <PasswordInput
className={classes.input} className={classes.input}
@ -232,16 +266,32 @@ export function Access() {
</div> </div>
</div> </div>
)} )}
<Modal opened={opened} onClose={close} withCloseButton={false} centered> <Modal
opened={urlOpened}
onClose={urlClose}
withCloseButton={false}
centered
>
<TextInput <TextInput
className={classes.urlInput}
size="md" size="md"
leftSectionPointerEvents="none" leftSectionPointerEvents="none"
leftSection={<IconServer />} leftSection={<IconServer />}
placeholder={state.strings.host} placeholder={state.strings.host}
value={state.node} value={state.node}
onKeyDown={(ev) => {
if (ev.code === 'Enter') urlClose()
}}
onChange={(event) => actions.setNode(event.currentTarget.value)} onChange={(event) => actions.setNode(event.currentTarget.value)}
/> />
</Modal> </Modal>
<Modal
opened={alertOpened}
onClose={alertClose}
title={state.strings.operationFailed}
>
{state.strings.tryAgain}
</Modal>
</div> </div>
) )
} }

View File

@ -18,7 +18,8 @@ export function useAccess() {
theme: '', theme: '',
language: '', language: '',
node: '', node: '',
hostname: '', secure: false,
host: '',
available: 0, available: 0,
availableSet: false, availableSet: false,
themes: settings.state.themes, themes: settings.state.themes,
@ -31,21 +32,21 @@ export function useAccess() {
} }
useEffect(() => { useEffect(() => {
const { protocol, hostname, port } = location const { protocol, host } = location
setUrl(`${protocol}//${hostname}:${port}`) setUrl(`${protocol}//${host}`)
}, []) }, [])
const setUrl = (node: string) => { const setUrl = (node: string) => {
try { try {
const url = new URL(node) const url = new URL(node)
const { protocol, hostname, port } = url const { protocol, host } = url
getAvailable(`${hostname}:${port}`, protocol === 'https:') getAvailable(host, protocol === 'https:')
updateState({ node, hostname: hostname }) updateState({ node, host, secure: protocol === 'https:' })
} catch (err) { } catch (err) {
console.log(err) console.log(err)
const { protocol, hostname, port } = location const { protocol, host } = location
getAvailable(`${hostname}:${port}`, protocol === 'https:') getAvailable(host, protocol === 'https:')
updateState({ node, hostname: location.hostname }) updateState({ node, host, secure: protocol === 'https:' })
} }
} }
@ -55,9 +56,6 @@ export function useAccess() {
debounce.current = setTimeout(async () => { debounce.current = setTimeout(async () => {
try { try {
const available = await app.actions.getAvailable(node, secure) const available = await app.actions.getAvailable(node, secure)
console.log('AVAILABLE: ', available)
updateState({ available, availableSet: true }) updateState({ available, availableSet: true })
} catch (err) { } catch (err) {
console.log(err) console.log(err)
@ -103,7 +101,10 @@ export function useAccess() {
setTheme: (theme: string) => { setTheme: (theme: string) => {
settings.actions.setTheme(theme) settings.actions.setTheme(theme)
}, },
login: () => {}, accountLogin: async () => {
const { username, password, host, secure } = state
await app.actions.accountLogin(username, password, host, secure)
},
} }
return { state, actions } return { state, actions }

View File

@ -26,7 +26,14 @@ export function useAppContext() {
} }
const actions = { const actions = {
accountLogin: async () => { accountLogin: async (
username: string,
password: string,
node: string,
secure: boolean
) => {
console.log('LOGIN:', username, password, node, secure)
const params = { const params = {
topicBatch: 16, topicBatch: 16,
tagBatch: 16, tagBatch: 16,
@ -39,10 +46,10 @@ export function useAppContext() {
appName: 'databag', appName: 'databag',
} }
const login = await sdk.current.login( const login = await sdk.current.login(
'asdf', username,
'asdf', password,
'balzack.coredb.org', node,
true, secure,
null, null,
params params
) )