-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathApp.js
More file actions
90 lines (83 loc) · 3.1 KB
/
Copy pathApp.js
File metadata and controls
90 lines (83 loc) · 3.1 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
import React, { useState, useRef, useEffect } from "react";
import { Routes, Route } from "react-router-dom";
import Navigation from "./Navigation";
import QueryPage from "./query/QueryPage";
import RecentPage from "./recent/RecentPage";
import StatusPage from "./status/StatusPage";
import ConfigPage from "./config/ConfigPage";
import AboutPage from "./about/AboutPage";
import AuthPage from "./auth/AuthPage";
import api, { parseJWT } from "./api";
import "./App.css";
import { refreshAccesToken, storeTokenData, clearTokenData } from "./utils";
function getCurrentTokenOrNull() {
// This function handles missing and corrupted token in the same way.
try {
return parseJWT(localStorage.getItem("rawToken"));
} catch {
return null;
}
}
function App() {
const [config, setConfig] = useState(null);
const tokenIntervalRef = useRef(null);
useEffect(() => {
api.get("/server").then((response) => {
setConfig(response.data);
});
tokenIntervalRef.current = setInterval(() => {
refreshAccesToken();
}, 900000); // refresh token every 15 minutes just in case user was idle.
return () => clearInterval(tokenIntervalRef.current);
}, []);
const login = async (token_data) => {
token_data.not_before_policy = token_data["not-before-policy"];
delete token_data["not-before-policy"];
const response = await api.post("/login", token_data);
storeTokenData(token_data["access_token"]);
const location_href = localStorage.getItem("currentLocation");
if (location_href) {
window.location.href = location_href;
} else {
window.location.href = "/";
}
};
const logout = () => {
clearTokenData(tokenIntervalRef.current);
if (config !== null) {
const logout_url = new URL(config["openid_url"] + "/logout");
logout_url.searchParams.append(
"redirect_uri",
window.location.origin
);
window.location.href = logout_url;
} else {
// Shouldn't happen, but reload just in case.
window.location.href = "/";
}
};
const token = getCurrentTokenOrNull();
return (
<div className="App">
<Navigation session={token} config={config} logout={logout} />
<Routes>
<Route exact path="/" element={<QueryPage />} />
<Route path="/query/:hash" element={<QueryPage />} />
<Route exact path="/recent" element={<RecentPage />} />
<Route exact path="/config" element={<ConfigPage />} />
<Route exact path="/status" element={<StatusPage />} />
<Route
exact
path="/about"
element={<AboutPage config={config} />}
/>
<Route
exact
path="/auth"
element={<AuthPage config={config} login={login} />}
/>
</Routes>
</div>
);
}
export default App;