This repository has been archived on 2024-08-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
khufu/src/Stash.js

69 lines
2 KiB
JavaScript
Raw Normal View History

2021-08-14 17:56:29 -06:00
// Module Imports
2021-08-14 16:28:05 -06:00
import React from "react";
import axios from "axios";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
2021-08-14 17:56:29 -06:00
// Local Imports
2021-08-14 16:28:05 -06:00
import StashBoard from "./stash/StashBoard";
import { api } from "./config.json";
const getCairoApiInstance = (cairoAuthToken) =>
2021-08-14 17:56:29 -06:00
axios.create({ headers: { Authorization: `Bearer ${cairoAuthToken}` } });
2021-08-14 16:28:05 -06:00
class Stash extends React.Component {
constructor(props) {
super(props);
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
this.state = { cairoApi: null };
if (params.cairoAuthToken) this.setLocalToken(params.cairoAuthToken);
}
componentDidMount() {
this.verifyLocalToken();
}
verifyLocalToken() {
const token = localStorage.getItem("cairoAuthToken");
axios
2021-08-14 17:56:29 -06:00
.get(api.cairo.urls.verify, {
headers: { authorization: `Bearer ${token}` },
})
2021-08-14 16:28:05 -06:00
.then(() => this.setState({ cairoApi: getCairoApiInstance(token) }))
2021-08-14 17:56:29 -06:00
.catch((error) => {
if (error.response && error.response.status === 401)
return window.location.replace(
2021-08-21 22:50:36 -06:00
`${process.env.REACT_APP_CAIRO_PUBLIC_URL}${api.cairo.urls.login}?redirectUri=${window.location.href}`
2021-08-14 17:56:29 -06:00
);
console.error("Auth server not up/configured properly!");
});
2021-08-14 16:28:05 -06:00
}
setLocalToken(cairoAuthToken) {
localStorage.setItem("cairoAuthToken", cairoAuthToken);
window.history.replaceState({}, document.title, "/");
}
render() {
return (
<>
<ToastContainer
position="top-right"
autoClose={5000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick={true}
rtl={false}
limit={3}
pauseOnFocusLoss
draggable
pauseOnHover={false}
/>
{this.state.cairoApi && <StashBoard cairoApi={this.state.cairoApi} />}
</>
);
}
}
export default Stash;