summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/special-pages/AuthPage.tsx
blob: 5c4c9dc8b455ee5421695f95a3691ccb9a081186 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { FunctionComponent, useCallback, useState } from "react";
import {
  Alert,
  Button,
  Card,
  Collapse,
  Form,
  Image,
  Spinner,
} from "react-bootstrap";
import { Redirect } from "react-router-dom";
import { useReduxStore } from "../@redux/hooks/base";
import logo from "../@static/logo128.png";
import { SystemApi } from "../apis";
import "./AuthPage.scss";

interface Props {}

const AuthPage: FunctionComponent<Props> = () => {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");

  const [updating, setUpdate] = useState(false);

  const updateError = useCallback((msg: string) => {
    setError(msg);
    setTimeout(() => setError(""), 2000);
  }, []);

  const onSuccess = useCallback(() => window.location.reload(), []);

  const authState = useReduxStore((s) => s.site.auth);

  const onError = useCallback(() => {
    setUpdate(false);
    updateError("Login Failed");
  }, [updateError]);

  if (authState) {
    return <Redirect to="/"></Redirect>;
  }

  return (
    <div className="d-flex bg-light vh-100 justify-content-center align-items-center">
      <Card className="auth-card shadow">
        <Form
          onSubmit={(e) => {
            e.preventDefault();
            if (!updating) {
              setUpdate(true);
              SystemApi.login(username, password)
                .then(onSuccess)
                .catch(onError);
            }
          }}
        >
          <Card.Body>
            <Form.Group className="mb-5 d-flex justify-content-center">
              <Image width="64" height="64" src={logo}></Image>
            </Form.Group>
            <Form.Group>
              <Form.Control
                disabled={updating}
                name="username"
                type="text"
                placeholder="Username"
                required
                onChange={(e) => setUsername(e.currentTarget.value)}
              ></Form.Control>
            </Form.Group>
            <Form.Group>
              <Form.Control
                disabled={updating}
                name="password"
                type="password"
                placeholder="Password"
                required
                onChange={(e) => setPassword(e.currentTarget.value)}
              ></Form.Control>
            </Form.Group>
            <Collapse in={error.length !== 0}>
              <div>
                <Alert variant="danger" className="m-0">
                  {error}
                </Alert>
              </div>
            </Collapse>
          </Card.Body>
          <Card.Footer>
            <Button type="submit" disabled={updating} block>
              {updating ? (
                <Spinner size="sm" animation="border"></Spinner>
              ) : (
                "LOGIN"
              )}
            </Button>
          </Card.Footer>
        </Form>
      </Card>
    </div>
  );
};

export default AuthPage;