-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLoginWrapper.js
More file actions
71 lines (65 loc) · 1.72 KB
/
Copy pathLoginWrapper.js
File metadata and controls
71 lines (65 loc) · 1.72 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
import React from "react";
import {
Box,
Flex,
Text,
Input,
Button,
FormControl,
FormLabel,
FormHelperText,
} from "@chakra-ui/core";
import Header from "../ui/Header";
import db from "../data/database";
export default class LoginWrapper extends React.Component {
state = {
username: localStorage.getItem("username") || null,
};
login = (username) => {
localStorage.setItem("username", username);
this.setState({ username: username });
};
logout = () => {
localStorage.removeItem("username");
this.setState({ username: undefined });
};
render() {
let { username } = this.state;
let inputRef = React.createRef();
// If we are logged in, then pass the username to the children.
if (username) {
const results = db.queryAll("founders", { query: { username } });
return this.props.children({
username,
logout: this.logout,
onboarded: results.length > 0,
});
}
// Otherwise, show a login form.
return (
<Flex direction="column" align="center">
<Box width={"100%"} maxWidth={"1000px"} pl={2} pr={2}>
<Header />
<Box
borderWidth="1px"
rounded="lg"
p={6}
m={"auto"}
overflow="hidden"
>
<FormControl>
<FormLabel>Username</FormLabel>
<Input ref={inputRef} />
<FormHelperText>Your email, or literally anything</FormHelperText>
</FormControl>
<FormControl>
<Button mt={4} onClick={() => this.login(inputRef.current.value)}>
Login
</Button>
</FormControl>
</Box>
</Box>
</Flex>
);
}
}