domain-watchdog/assets/pages/LoginPage.tsx

100 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-08-02 16:17:55 +02:00
import React, {createContext, useContext, useEffect, useState} from "react";
2024-08-05 02:29:42 +02:00
import {Alert, Button, Card, Form, Input} from "antd";
2024-08-04 15:49:38 +02:00
import {getConfiguration, getUser, InstanceConfig, 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-08-05 02:29:42 +02:00
import TextPage from "./TextPage";
2024-07-26 18:31:47 +02:00
type FieldType = {
username: string;
password: string;
2024-08-05 02:29:42 +02:00
}
const gridStyle: React.CSSProperties = {
width: '50%',
textAlign: 'center',
}
2024-07-26 18:31:47 +02:00
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
2024-08-04 15:49:38 +02:00
const [error, setError] = useState<string>()
const [configuration, setConfiguration] = useState<InstanceConfig>()
2024-07-26 18:31:47 +02:00
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)
2024-08-05 02:29:42 +02:00
if (e.response.data.message !== undefined) {
setError(e.response.data.message)
} else {
setError(e.response.data['hydra:description'])
}
2024-07-26 18:31:47 +02:00
})
}
2024-08-02 16:17:55 +02:00
useEffect(() => {
getUser().then(() => {
setIsAuthenticated(true)
navigate('/home')
})
2024-08-04 15:49:38 +02:00
getConfiguration().then(setConfiguration)
2024-08-02 16:17:55 +02:00
}, [])
2024-08-05 02:29:42 +02:00
return <Card title={t`Log in`} style={{width: '100%'}}>
<Card.Grid style={gridStyle} hoverable={false}>
{error &&
<Alert
type='error'
message={t`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"
2024-07-26 18:31:47 +02:00
>
2024-08-05 02:29:42 +02:00
<Form.Item
label={t`Username`}
name="username"
rules={[{required: true, message: t`Required`}]}
>
<Input autoFocus/>
</Form.Item>
2024-07-26 18:31:47 +02:00
2024-08-05 02:29:42 +02:00
<Form.Item<FieldType>
label={t`Password`}
name="password"
rules={[{required: true, message: t`Required`}]}
>
<Input.Password/>
</Form.Item>
2024-07-26 18:31:47 +02:00
2024-08-05 02:29:42 +02:00
<Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button block type="primary" htmlType="submit">
{t`Submit`}
</Button>
</Form.Item>
{configuration?.ssoLogin && <Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button type="dashed" htmlType="button" href="/login/oauth" block>
{t`Log in with SSO`}
</Button>
</Form.Item>}
</Form>
</Card.Grid>
<Card.Grid style={gridStyle} hoverable={false}>
<TextPage resource='ads.md'/>
</Card.Grid>
2024-07-26 18:31:47 +02:00
</Card>
2024-07-23 18:37:59 +02:00
}