domain-watchdog/assets/pages/LoginPage.tsx

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-07-26 23:55:12 +02:00
import React, {createContext, useContext, useState} from "react";
2024-07-26 18:31:47 +02:00
import {Alert, Button, Card, Flex, Form, Input} from "antd";
import {login} from "../utils/api";
import {useNavigate} from "react-router-dom";
type FieldType = {
username: string;
password: string;
};
export const AuthenticatedContext = createContext<any>(null)
2024-07-23 18:37:59 +02:00
export default function Page() {
2024-07-26 18:31:47 +02:00
const [error, setError] = useState()
const navigate = useNavigate()
const {isAuthenticated, setIsAuthenticated} = useContext(AuthenticatedContext)
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)
})
}
return <Flex gap="middle" align="center" justify="center" vertical><Card
title="Log in"
>
{error &&
<Alert
type='error'
message='Error'
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
label="Username"
name="username"
rules={[{required: true, message: 'Required'}]}
>
<Input/>
</Form.Item>
<Form.Item<FieldType>
label="Password"
name="password"
rules={[{required: true, message: 'Required'}]}
>
<Input.Password/>
</Form.Item>
<Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button type="primary" htmlType="submit">
Submit
</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">
Log in with SSO
</Button>
</Form.Item>
2024-07-26 18:31:47 +02:00
</Form>
</Card>
</Flex>
2024-07-23 18:37:59 +02:00
}