aboutsummaryrefslogtreecommitdiff
path: root/src/routes/Login.js
blob: 9125f7dd8cfde33145f38ae0ac6a12527c5d0230 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { useEffect, useState } from "react";
import React from "react";
import { useNavigate, useLocation } from "react-router-dom";
import Button from "../uikit/Button.js";
import Input from "../uikit/Input.js";
import { useAuth } from "../hooks/useAuth";
import { useFeatures } from "../hooks/useFeatures";
import { isExpired } from "react-jwt";
import { styled } from "@stitches/react";
import { debounce } from "lodash";
import { Link } from "react-router-dom";

const Flex = styled("div", {
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  minHeight: "100vh",
  // If ever we decide to move the title out of the login card itself
  flexDirection: "column",
});

const LoginCard = styled("div", {
  boxShadow: "0 4px 6px rgba(0,0,0,0.1)",
  borderRadius: "12px",
  padding: "1em",
  border: "1px solid #D4D4D8",
  backgroundColor: "#F4F4F5",
  width: "360px",
});

const Title = styled("h1", {
  textAlign: "center",
  fontSize: "3em",
  fontWeight: "300",
  margin: "1rem 0",
});

const Subtitle = styled("h1", {
  textAlign: "center",
  fontSize: "2.5em",
  margin: "1rem 0",
  fontWeight: 300,
});

const StyledLabel = styled("label", {
  display: "block",
  paddingBottom: "0.25em",
  fontSize: "0.9em",
});

const AlignRight = styled("div", {
  display: "flex",
  alignItems: "right",
  justifyContent: "right",
});

function Login(props) {
  let auth = useAuth();
  let navigate = useNavigate();
  let location = useLocation();
  let features = useFeatures();
  const [totpRequired, setTotpRequired] = useState(false);

  let from = location.state?.from?.pathname || "/zones";

  useEffect(() => {
    if (auth.token && !isExpired(auth.token)) {
      navigate(from, { replace: true });
    }
  }, [auth.token, from, navigate]);

  const handleSubmit = () => {
    fetch("/api/v1/users/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: document.getElementById("email").value.trim(),
        password: document.getElementById("password").value.trim(),
      }),
    }).then((res) => {
      if (res.status === 200) {
        res.json().then((data) => {
          auth.signin(data.token, () => {
            navigate(from, { replace: true });
          });
        });
      } else {
        alert("Invalid email or password");
      }
    });
  };

  const checkTotpRequired = debounce((e) => {
    if (!features.totp) {
      return;
    }
    fetch("/api/v1/users/totp?email=" + e.target.value).then((res) => {
      if (res.status === 200) {
        res.json().then((data) => {
          console.debug(data.totp);
          setTotpRequired(data.totp);
        });
      }
    });
  }, 200);

  function onKeyPress(e) {
    if (e.key === "Enter") {
      e.preventDefault();
      handleSubmit();
    }
  }

  return (
    <Flex>
      <Title>HOSTSdotTXT</Title>
      <LoginCard>
        <Subtitle>Log In</Subtitle>
        <StyledLabel htmlFor="email">Email</StyledLabel>
        <Input id="email" type="email" onChange={checkTotpRequired}></Input>
        <StyledLabel htmlFor="password">Password</StyledLabel>
        <Input id="password" type="password" onKeyUp={onKeyPress}></Input>
        {features.totp && (
          <>
            <StyledLabel htmlFor="totp">TOTP Code</StyledLabel>
            <Input
              id="totp"
              disabled={!totpRequired}
              placeholder={totpRequired ? "" : "Not Required"}
            ></Input>
          </>
        )}
        <AlignRight>
          {/* <Button secondary>Cancel</Button> */}
          <Button onClick={handleSubmit} primary>
            Log In {"\u2794"}
          </Button>
        </AlignRight>
        <center>
          {features.signup && (
            <p>
              Don't have an account? <Link to="/signup">Sign up!</Link>
            </p>
          )}
        </center>
      </LoginCard>
    </Flex>
  );
}
export default Login;