fix: simplify code and check login status on home page load

This commit is contained in:
Sambhav Saggi 2022-05-31 21:00:43 -04:00
parent 036e5779ba
commit ddb3f442f8
No known key found for this signature in database
GPG key ID: 8F731DA8AFBAE35D
2 changed files with 78 additions and 61 deletions

View file

@ -47,11 +47,15 @@ function Bar() {
// name: "Settings",
// to: "/settings",
// },
!disabledAuth && {
...(!disabledAuth
? [
{
name: "Log out",
divide: true,
onClick: onLogOutClick,
},
]
: []),
];
return (
@ -74,8 +78,7 @@ function Bar() {
</Typography>
</Box>
{/* The filter removes all elements that are "true" or "false" */}
{loggedIn &&
menuItems.filter((e) => typeof e !== "boolean").length > 0 && (
{loggedIn && menuItems.length > 0 && (
<>
<Button color="inherit" onClick={openMenu}>
<MenuIcon></MenuIcon>

View file

@ -1,5 +1,7 @@
import { useState, useEffect } from "react";
import { useHistory } from "react-router-dom";
import { useLocalStorage } from "react-use";
import axios from "axios";
import { Divider, Button, Grid, Typography, Box } from "@material-ui/core";
import useStyles from "./HomeLoggedIn.styles";
@ -11,10 +13,22 @@ import { generateNetworkConfig } from "utils/NetworkConfig";
function HomeLoggedIn() {
const [networks, setNetworks] = useState([]);
const [, setLoggedIn] = useLocalStorage("loggedIn", false);
const [, setDisableAuth] = useLocalStorage("disableAuth", false);
const [token, setToken] = useLocalStorage("token", null);
const classes = useStyles();
const history = useHistory();
axios.get("/auth/login").then(function (response) {
if (response.data.enabled) {
setDisableAuth(false);
if (!token || token.length === 0) {
setLoggedIn(false);
}
}
});
const createNetwork = async () => {
const network = await API.post("network", generateNetworkConfig());
console.log(network);