fix: stop redundant fetching /auth/login

This commit is contained in:
dec0dOS 2022-06-18 14:30:48 +03:00
parent 4151978a68
commit ce9f7943c0

View file

@ -1,23 +1,32 @@
import { useEffect } from "react";
import { Grid, Typography } from "@material-ui/core"; import { Grid, Typography } from "@material-ui/core";
import { useLocalStorage } from "react-use"; import { useLocalStorage } from "react-use";
import axios from "axios";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import axios from "axios";
function HomeLoggedOut() { function HomeLoggedOut() {
const [, setLoggedIn] = useLocalStorage("loggedIn", false); const [, setLoggedIn] = useLocalStorage("loggedIn", false);
const [, setToken] = useLocalStorage("token", null); const [, setToken] = useLocalStorage("token", null);
const [, setDisableAuth] = useLocalStorage("disableAuth", false); const [, setDisableAuth] = useLocalStorage("disableAuth", false);
const history = useHistory(); const history = useHistory();
axios.get("/auth/login").then(function (response) {
if (!response.data.enabled) { useEffect(() => {
setLoggedIn(true); async function fetchData() {
setDisableAuth(true); axios.get("/auth/login").then(function (response) {
setToken(""); if (!response.data.enabled) {
history.go(0); setLoggedIn(true);
} else { setDisableAuth(true);
setDisableAuth(false); setToken("");
history.go(0);
} else {
setDisableAuth(false);
}
});
} }
}); fetchData();
}, [history, setDisableAuth, setLoggedIn, setToken]);
return ( return (
<Grid <Grid
container container