2025-10-31 13:46:17 +01:00
|
|
|
import {Button, Flex, Form, Input, message} from 'antd'
|
2024-12-30 23:50:15 +01:00
|
|
|
import {t} from 'ttag'
|
2025-10-31 13:56:11 +01:00
|
|
|
import React, {useContext, useEffect, useState} from 'react'
|
2024-12-30 23:50:15 +01:00
|
|
|
import {getUser, login} from '../utils/api'
|
|
|
|
|
import {useNavigate} from 'react-router-dom'
|
2024-08-22 01:44:50 +02:00
|
|
|
|
2024-12-30 23:50:15 +01:00
|
|
|
import {showErrorAPI} from '../utils/functions/showErrorAPI'
|
2025-12-08 18:18:33 +01:00
|
|
|
import {AuthenticatedContext} from "../contexts"
|
2024-08-05 03:11:03 +02:00
|
|
|
|
2024-12-30 23:50:15 +01:00
|
|
|
interface FieldType {
|
|
|
|
|
username: string
|
|
|
|
|
password: string
|
2024-08-05 03:11:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function LoginForm({ssoLogin}: { ssoLogin?: boolean }) {
|
|
|
|
|
const navigate = useNavigate()
|
2024-08-07 15:57:16 +02:00
|
|
|
const [messageApi, contextHolder] = message.useMessage()
|
2024-08-05 03:11:03 +02:00
|
|
|
const {setIsAuthenticated} = useContext(AuthenticatedContext)
|
2025-10-31 22:47:37 +01:00
|
|
|
const [loading, setLoading] = useState(false)
|
2024-08-05 03:11:03 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getUser().then(() => {
|
|
|
|
|
setIsAuthenticated(true)
|
|
|
|
|
navigate('/home')
|
|
|
|
|
})
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const onFinish = (data: FieldType) => {
|
2025-10-31 22:47:37 +01:00
|
|
|
setLoading(true)
|
2025-10-31 13:56:11 +01:00
|
|
|
|
2024-08-05 03:11:03 +02:00
|
|
|
login(data.username, data.password).then(() => {
|
|
|
|
|
setIsAuthenticated(true)
|
|
|
|
|
navigate('/home')
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
setIsAuthenticated(false)
|
2024-08-07 15:57:16 +02:00
|
|
|
showErrorAPI(e, messageApi)
|
2025-10-31 22:47:37 +01:00
|
|
|
setLoading(false)
|
2024-08-05 03:11:03 +02:00
|
|
|
})
|
|
|
|
|
}
|
2024-12-30 23:50:15 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{contextHolder}
|
|
|
|
|
<Form
|
|
|
|
|
name='basic'
|
|
|
|
|
labelCol={{span: 8}}
|
|
|
|
|
wrapperCol={{span: 16}}
|
|
|
|
|
style={{maxWidth: 600}}
|
|
|
|
|
onFinish={onFinish}
|
|
|
|
|
autoComplete='off'
|
2025-10-31 13:56:11 +01:00
|
|
|
disabled={loading}
|
2024-08-05 03:11:03 +02:00
|
|
|
>
|
2024-12-30 23:50:15 +01:00
|
|
|
<Form.Item
|
|
|
|
|
label={t`Email address`}
|
|
|
|
|
name='username'
|
|
|
|
|
rules={[{required: true, message: t`Required`}]}
|
|
|
|
|
>
|
|
|
|
|
<Input autoFocus/>
|
|
|
|
|
</Form.Item>
|
2024-08-05 03:11:03 +02:00
|
|
|
|
2024-12-30 23:50:15 +01:00
|
|
|
<Form.Item<FieldType>
|
|
|
|
|
label={t`Password`}
|
|
|
|
|
name='password'
|
|
|
|
|
rules={[{required: true, message: t`Required`}]}
|
|
|
|
|
>
|
|
|
|
|
<Input.Password/>
|
2024-08-05 03:11:03 +02:00
|
|
|
</Form.Item>
|
2024-12-30 23:50:15 +01:00
|
|
|
|
2025-10-31 13:46:17 +01:00
|
|
|
<Flex wrap justify="center" gap="middle">
|
2025-12-08 18:18:33 +01:00
|
|
|
<Button type='primary' htmlType='submit'>
|
|
|
|
|
{t`Submit`}
|
|
|
|
|
</Button>
|
2025-10-31 13:46:17 +01:00
|
|
|
{ssoLogin &&
|
2024-12-30 23:50:15 +01:00
|
|
|
<Button type='dashed' htmlType='button' href='/login/oauth'>
|
|
|
|
|
{t`Log in with SSO`}
|
2025-10-31 13:46:17 +01:00
|
|
|
</Button>}
|
|
|
|
|
</Flex>
|
2024-12-30 23:50:15 +01:00
|
|
|
</Form>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|