Login.tsx
2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useCallback, useState } from "react";
import { Form, Input, Button, Checkbox, Layout } from "antd";
import { UserOutlined, LockOutlined } from "@ant-design/icons";
import { useHistory, Link } from "react-router-dom";
import styles from "./Login.module.scss";
export type LoginProps = {
login: (
username: string,
password: string,
remember: boolean
) => Promise<void>;
};
export function Login({ login }: LoginProps) {
const [error, setError] = useState<boolean>(false);
const history = useHistory();
const handleLogin = useCallback(
async ({ username, password, remember }) => {
setError(false);
try {
await login(username, password, remember);
history.push("/");
} catch {
setError(true);
}
},
[login, history]
);
return (
<Layout className={styles.layout}>
<Layout.Content className={styles.content}>
<Form
name="login"
initialValues={{ remember: true }}
onFinish={handleLogin}
>
<Form.Item
name="username"
rules={[{ required: true, message: "아이디를 입력하세요" }]}
{...(error && {
validateStatus: "error",
})}
>
<Input prefix={<UserOutlined />} placeholder="아이디" />
</Form.Item>
<Form.Item
name="password"
rules={[{ required: true, message: "비밀번호를 입력하세요" }]}
{...(error && {
validateStatus: "error",
help: "로그인에 실패했습니다",
})}
>
<Input
prefix={<LockOutlined />}
type="password"
placeholder="비밀번호"
/>
</Form.Item>
<Form.Item>
<Form.Item name="remember" valuePropName="checked" noStyle>
<Checkbox>자동 로그인</Checkbox>
</Form.Item>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
로그인
</Button>
<Link to="/signup" style={{ marginLeft: 24 }}>
회원가입
</Link>
</Form.Item>
</Form>
</Layout.Content>
</Layout>
);
}