-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
133 lines (122 loc) · 3.25 KB
/
App.js
File metadata and controls
133 lines (122 loc) · 3.25 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
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
import React, { useState, useEffect } from "react";
import { Switch, Route, useHistory, useLocation } from "react-router-dom";
import { Container } from "@material-ui/core";
import jwt from "jsonwebtoken";
import AuthForm from "./components/Authentication/AuthForm";
import BookList from "./components/Comics/BookList";
import PullList from "./components/Comics/PullList";
import AllLists from "./components/Admin/AllLists";
import { UserContext } from "./contexts/UserContext";
import { flureeQuery, lookForDbs } from "./utils/flureeFunctions";
import setupFluree from "./setup/setupFluree";
import NavBar from "./components/Navigation/NavBar";
function App() {
const history = useHistory();
const location = useLocation();
const initialUser = {
_id: 0,
username: "",
role: "",
};
const [user, setUser] = useState(initialUser);
const [session, setSession] = useState({
loggedIn: false,
token: "",
});
useEffect(() => {
lookForDbs()
.then((database) => {
console.log(database);
if (!database) {
setupFluree();
}
})
.catch((err) => err);
}, []);
useEffect(() => {
if (session.loggedIn === false) {
const token = localStorage.getItem("authToken");
if (token) {
setSession({
loggedIn: true,
token,
});
}
}
}, [location, session.loggedIn]);
useEffect(() => {
if (session.token) {
const decodedToken = jwt.decode(session.token);
const userAuth = decodedToken.sub;
const query = {
"selectOne": [
{
"_user/_auth": [
"_id", "username"
]},
{"roles": ["id"]}
],
"from": ["_auth/id", userAuth],
"opts": {
"compact": true
}
};
flureeQuery(query)
.then((res) => {
console.log("user", res);
const gotUser = res["_user"][0];
const role = res.roles[0].id
console.log("ping");
setUser({
username: gotUser.username,
_id: gotUser._id,
role: role,
pull_list: gotUser.pull_list || [],
});
history.push("/books")
})
.catch((err) => {
console.log(err);
});
} else {
history.push("/login")
}
}, [session]);
const logout = () => {
localStorage.clear();
setSession({
loggedIn: false,
token: "",
});
setUser(initialUser);
history.push("/login");
};
return (
<div className="App">
<UserContext.Provider value={user}>
<NavBar logout={logout} />
<Container maxWidth="md">
<Switch>
<Route path="/books">
{/* {user.role === "employee" && <h1>Employee</h1>} */}
<BookList />
</Route>
<Route path="/register">
<AuthForm register />
</Route>
<Route path="/login">
<AuthForm />
</Route>
<Route path="/list">
<PullList />
</Route>
<Route path="/adminlist">
<AllLists />
</Route>
</Switch>
</Container>
</UserContext.Provider>
</div>
);
}
export default App;