diff --git a/backend/routes/auth.js b/backend/routes/auth.js index cda1e83..dea35f9 100644 --- a/backend/routes/auth.js +++ b/backend/routes/auth.js @@ -3,6 +3,14 @@ const router = express.Router(); const auth = require("../services/auth"); +router.get("/login", async function (req, res) { + if (process.env.ZU_DISABLE_AUTH === "true") { + res.send({ enabled: false }); + } else { + res.send({ enabled: true }); + } +}); + router.post("/login", async function (req, res) { if (req.body.username && req.body.password) { auth.authorize(req.body.username, req.body.password, function (err, user) { diff --git a/backend/services/auth.js b/backend/services/auth.js index f1b986c..9833857 100644 --- a/backend/services/auth.js +++ b/backend/services/auth.js @@ -20,14 +20,18 @@ async function authorize(username, password, callback) { exports.isAuthorized = isAuthorized; async function isAuthorized(req, res, next) { - if (req.token) { - const user = await db.get("users").find({ token: req.token }).value(); - if (user) { - next(); - } else { - res.status(403).send({ error: "Invalid token" }); - } + if (process.env.ZU_DISABLE_AUTH === "true") { + next(); } else { - res.status(401).send({ error: "Specify token" }); + if (req.token) { + const user = await db.get("users").find({ token: req.token }).value(); + if (user) { + next(); + } else { + res.status(403).send({ error: "Invalid token" }); + } + } else { + res.status(401).send({ error: "Specify token" }); + } } } diff --git a/frontend/src/components/HomeLoggedOut/HomeLoggedOut.jsx b/frontend/src/components/HomeLoggedOut/HomeLoggedOut.jsx index 4aea50d..2a57165 100644 --- a/frontend/src/components/HomeLoggedOut/HomeLoggedOut.jsx +++ b/frontend/src/components/HomeLoggedOut/HomeLoggedOut.jsx @@ -1,6 +1,19 @@ import { Grid, Typography } from "@material-ui/core"; +import { useLocalStorage } from "react-use"; +import axios from "axios"; +import { useHistory } from "react-router-dom"; function HomeLoggedOut() { + const [, setLoggedIn] = useLocalStorage("loggedIn", false); + const [, setToken] = useLocalStorage("token", null); + const history = useHistory(); + axios.get("/auth/login").then(function (response) { + if (!response.data.enabled) { + setLoggedIn(true); + setToken(""); + history.go(0); + } + }); return (