aboutsummaryrefslogtreecommitdiff
path: root/src/routes/SignUp.js
blob: 5b64cc0a4ab79a8e2c94e857691aeb8ffc30a6f3 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { styled } from "@stitches/react";
import Button from "../uikit/Button.js";
import Input from "../uikit/Input.js";
import { debounce } from "lodash";
import { useEffect, useState } from "react";
import { useAuth } from "../hooks/useAuth.js";
import { useNavigate, useLocation } from "react-router-dom";
import { useFeatures } from "../hooks/useFeatures.js";
import { Link } from "react-router-dom";
import { isExpired } from "react-jwt";

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: "4px",
  padding: "1em",
  border: "1px solid #D4D4D8",
  backgroundColor: "#F4F4F5",
  width: "360px",
});

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

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",
});

export function SignUp() {
  const [passwordsMatch, setPasswordsMatch] = useState(true);
  const [emailValid, setEmailValid] = useState(true);
  const auth = useAuth();
  const features = useFeatures();
  const navigate = useNavigate();

  const checkPasswordsMatch = debounce((e) => {
    const password = document.getElementById("password").value;
    const passwordConfirm = document.getElementById("passwordConfirm").value;

    if (password === "" || passwordConfirm === "") {
      return;
    }
    if (password !== passwordConfirm) {
      console.debug("passwords don't match");
      setPasswordsMatch(false);
    } else {
      console.debug("passwords match");
      setPasswordsMatch(true);
    }
  }, 200);

  const checkEmailValid = debounce((e) => {
    const email = e.target.value;

    if (email === "") {
      return;
    }

    const re = /.{1,64}@.{1,64}\..{1,64}/i;
    if (!re.test(email)) {
      console.debug("email is invalid");
      setEmailValid(false);
    } else {
      console.debug("email is valid");
      setEmailValid(true);
    }
  }, 200);

  const handleSubmit = () => {
    fetch("/api/v1/users", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: document.getElementById("email").value.trim(),
        password: document.getElementById("password").value.trim(),
        display_name:
          document.getElementById("displayName").value.trim() ?? null,
      }),
    }).then((res) => {
      if (res.status === 200) {
        res.json().then((data) => {
          auth.signin(data.token, () => {
            navigate("/");
          });
        });
      } else {
        alert("Something went wrong!");
      }
    });
  };

  let location = useLocation();

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

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

  if (!features.signup) {
    return (
      <Flex>
        <Title>HOSTSdotTXT</Title>
        <LoginCard>
          <Subtitle>Sign Up</Subtitle>
          <center>
            <p>Sorry, but sign-ups are currently disabled.</p>
            <p>
              If you have an account, you can <Link to="/login">sign in.</Link>
            </p>
          </center>
        </LoginCard>
      </Flex>
    );
  }

  return (
    <Flex>
      <Title>HOSTSdotTXT</Title>
      <LoginCard>
        <Subtitle>Sign Up</Subtitle>
        <StyledLabel for="email">
          Email{" "}
          {!emailValid && (
            <span style={{ color: "red" }}>(!) email appears invalid</span>
          )}
        </StyledLabel>
        <Input id="email" onChange={checkEmailValid} type="email"></Input>
        <StyledLabel for="displayName">Display Name</StyledLabel>
        <Input id="displayName"></Input>
        <StyledLabel for="password">Password</StyledLabel>
        <Input
          id="password"
          onChange={checkPasswordsMatch}
          type="password"
        ></Input>
        <StyledLabel for="passwordConfirm">
          Password (Confirm){" "}
          {!passwordsMatch && (
            <span style={{ color: "red" }}>(!) passwords don't match</span>
          )}
        </StyledLabel>
        <Input
          id="passwordConfirm"
          onChange={checkPasswordsMatch}
          type="password"
        ></Input>
        <AlignRight>
          {/* <Button secondary>Cancel</Button> */}
          <Button onClick={handleSubmit} primary>
            Sign Up {"\u2794"}
          </Button>
        </AlignRight>
        <center>
          <p>
            If you have an account, you can <Link to="/login">sign in.</Link>
          </p>
        </center>
      </LoginCard>
    </Flex>
  );
}

export default SignUp;