2024-08-07 15:57:16 +02:00
|
|
|
import {Button, Form, Input, message, Space} from "antd";
|
2024-08-05 03:11:03 +02:00
|
|
|
import {t} from "ttag";
|
2024-08-07 15:57:16 +02:00
|
|
|
import React, {useContext, useEffect} from "react";
|
2024-08-05 03:11:03 +02:00
|
|
|
import {getUser, login} from "../utils/api";
|
|
|
|
|
import {AuthenticatedContext} from "../pages/LoginPage";
|
|
|
|
|
import {useNavigate} from "react-router-dom";
|
2024-08-22 01:44:50 +02:00
|
|
|
|
|
|
|
|
import {showErrorAPI} from "../utils/functions/showErrorAPI";
|
2024-08-05 03:11:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
type FieldType = {
|
|
|
|
|
username: string;
|
|
|
|
|
password: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getUser().then(() => {
|
|
|
|
|
setIsAuthenticated(true)
|
|
|
|
|
navigate('/home')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const onFinish = (data: FieldType) => {
|
|
|
|
|
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)
|
2024-08-05 03:11:03 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return <>
|
2024-08-07 15:57:16 +02:00
|
|
|
{contextHolder}
|
2024-08-05 03:11:03 +02:00
|
|
|
<Form
|
|
|
|
|
name="basic"
|
|
|
|
|
labelCol={{span: 8}}
|
|
|
|
|
wrapperCol={{span: 16}}
|
|
|
|
|
style={{maxWidth: 600}}
|
|
|
|
|
onFinish={onFinish}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
>
|
|
|
|
|
<Form.Item
|
2024-08-05 03:15:11 +02:00
|
|
|
label={t`Email address`}
|
2024-08-05 03:11:03 +02:00
|
|
|
name="username"
|
|
|
|
|
rules={[{required: true, message: t`Required`}]}
|
|
|
|
|
>
|
|
|
|
|
<Input autoFocus/>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item<FieldType>
|
|
|
|
|
label={t`Password`}
|
|
|
|
|
name="password"
|
|
|
|
|
rules={[{required: true, message: t`Required`}]}
|
|
|
|
|
>
|
|
|
|
|
<Input.Password/>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Space>
|
|
|
|
|
<Form.Item wrapperCol={{offset: 8, span: 16}}>
|
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
|
{t`Submit`}
|
|
|
|
|
</Button>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
{ssoLogin && <Form.Item wrapperCol={{offset: 8, span: 16}}>
|
|
|
|
|
<Button type="dashed" htmlType="button" href="/login/oauth">
|
|
|
|
|
{t`Log in with SSO`}
|
|
|
|
|
</Button>
|
|
|
|
|
</Form.Item>}
|
|
|
|
|
</Space>
|
|
|
|
|
</Form>
|
|
|
|
|
</>
|
|
|
|
|
}
|