domain-watchdog/assets/pages/LoginPage.tsx

86 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-08-02 16:17:55 +02:00
import React, {createContext, useContext, useEffect, useState} from "react";
2024-07-26 18:31:47 +02:00
import {Alert, Button, Card, Flex, Form, Input} from "antd";
2024-08-02 16:17:55 +02:00
import {getUser, login} from "../utils/api";
2024-07-26 18:31:47 +02:00
import {useNavigate} from "react-router-dom";
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
2024-07-26 18:31:47 +02:00
type FieldType = {
username: string;
password: string;
};
export const AuthenticatedContext = createContext<any>(null)
2024-07-23 18:37:59 +02:00
2024-08-02 16:17:55 +02:00
export default function LoginPage() {
2024-07-26 18:31:47 +02:00
const [error, setError] = useState()
const navigate = useNavigate()
2024-07-28 15:36:22 +02:00
const {setIsAuthenticated} = useContext(AuthenticatedContext)
2024-07-26 18:31:47 +02:00
const onFinish = (data: FieldType) => {
login(data.username, data.password).then(() => {
setIsAuthenticated(true)
2024-07-27 12:59:40 +02:00
navigate('/home')
2024-07-26 18:31:47 +02:00
}).catch((e) => {
setIsAuthenticated(false)
setError(e.response.data.message)
})
}
2024-08-02 16:17:55 +02:00
useEffect(() => {
getUser().then(() => {
setIsAuthenticated(true)
navigate('/home')
})
}, [])
2024-07-26 18:31:47 +02:00
return <Flex gap="middle" align="center" justify="center" vertical><Card
2024-07-28 15:36:22 +02:00
title={t`Log in`}
2024-07-26 18:31:47 +02:00
>
{error &&
<Alert
type='error'
2024-07-28 15:36:22 +02:00
message={t`Error`}
2024-07-26 18:31:47 +02:00
banner={true}
role='role'
description={error}
style={{marginBottom: '1em'}}
/>}
<Form
name="basic"
labelCol={{span: 8}}
wrapperCol={{span: 16}}
style={{maxWidth: 600}}
onFinish={onFinish}
autoComplete="off"
>
<Form.Item
2024-07-28 15:36:22 +02:00
label={t`Username`}
2024-07-26 18:31:47 +02:00
name="username"
2024-07-28 15:36:22 +02:00
rules={[{required: true, message: t`Required`}]}
2024-07-26 18:31:47 +02:00
>
2024-07-30 17:48:09 +02:00
<Input autoFocus/>
2024-07-26 18:31:47 +02:00
</Form.Item>
<Form.Item<FieldType>
2024-07-28 15:36:22 +02:00
label={t`Password`}
2024-07-26 18:31:47 +02:00
name="password"
2024-07-28 15:36:22 +02:00
rules={[{required: true, message: t`Required`}]}
2024-07-26 18:31:47 +02:00
>
<Input.Password/>
</Form.Item>
<Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button type="primary" htmlType="submit">
2024-07-28 15:36:22 +02:00
{t`Submit`}
2024-07-26 18:31:47 +02:00
</Button>
</Form.Item>
2024-07-26 23:55:12 +02:00
<Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button type="primary" htmlType="button" href="/login/oauth">
2024-07-28 15:36:22 +02:00
{t`Log in with SSO`}
2024-07-26 23:55:12 +02:00
</Button>
</Form.Item>
2024-07-26 18:31:47 +02:00
</Form>
</Card>
</Flex>
2024-07-23 18:37:59 +02:00
}