fix: disable authentication properly

This commit is contained in:
Sambhav Saggi 2022-05-31 18:29:50 -04:00
parent 5096a99f75
commit 75933d7e59
No known key found for this signature in database
GPG key ID: 8F731DA8AFBAE35D
4 changed files with 74 additions and 65 deletions

View file

@ -18,11 +18,13 @@ const app = express();
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
if (process.env.ZU_DISABLE_AUTH === "true") {
app.use(
bearerToken({
headerKey: "Bearer",
})
);
}
if (
process.env.NODE_ENV === "production" &&

View file

@ -21,6 +21,7 @@ import LogIn from "components/LogIn";
function Bar() {
const [loggedIn, setLoggedIn] = useLocalStorage("loggedIn", false);
const [disabledAuth] = useLocalStorage("disableAuth", false);
const [anchorEl, setAnchorEl] = useState(null);
const history = useHistory();
@ -46,7 +47,7 @@ function Bar() {
// name: "Settings",
// to: "/settings",
// },
{
!disabledAuth && {
name: "Log out",
divide: true,
onClick: onLogOutClick,
@ -72,8 +73,9 @@ function Bar() {
</Link>
</Typography>
</Box>
{loggedIn && (
{/* The filter removes all elements that are "true" or "false" */}
{loggedIn &&
menuItems.filter((e) => typeof e !== "boolean").length > 0 && (
<>
<Button color="inherit" onClick={openMenu}>
<MenuIcon></MenuIcon>

View file

@ -6,10 +6,12 @@ import { useHistory } from "react-router-dom";
function HomeLoggedOut() {
const [, setLoggedIn] = useLocalStorage("loggedIn", false);
const [, setToken] = useLocalStorage("token", null);
const [, setDisableAuth] = useLocalStorage("disableAuth", false);
const history = useHistory();
axios.get("/auth/login").then(function (response) {
if (!response.data.enabled) {
setLoggedIn(true);
setDisableAuth(true);
setToken("");
history.go(0);
}

View file

@ -5,7 +5,10 @@ const baseURL = "/api/";
export default axios.create({
baseURL: baseURL,
responseType: "json",
headers: {
headers:
localStorage.getItem("disableAuth") === "true"
? {}
: {
Authorization: `Bearer ${JSON.parse(localStorage.getItem("token"))}`,
},
});