mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-06 21:11:41 -07:00
fix: disable authentication properly
This commit is contained in:
parent
5096a99f75
commit
75933d7e59
4 changed files with 74 additions and 65 deletions
|
@ -18,11 +18,13 @@ const app = express();
|
||||||
app.use(logger("dev"));
|
app.use(logger("dev"));
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: false }));
|
app.use(express.urlencoded({ extended: false }));
|
||||||
app.use(
|
if (process.env.ZU_DISABLE_AUTH === "true") {
|
||||||
bearerToken({
|
app.use(
|
||||||
headerKey: "Bearer",
|
bearerToken({
|
||||||
})
|
headerKey: "Bearer",
|
||||||
);
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
process.env.NODE_ENV === "production" &&
|
process.env.NODE_ENV === "production" &&
|
||||||
|
|
|
@ -21,6 +21,7 @@ import LogIn from "components/LogIn";
|
||||||
|
|
||||||
function Bar() {
|
function Bar() {
|
||||||
const [loggedIn, setLoggedIn] = useLocalStorage("loggedIn", false);
|
const [loggedIn, setLoggedIn] = useLocalStorage("loggedIn", false);
|
||||||
|
const [disabledAuth] = useLocalStorage("disableAuth", false);
|
||||||
const [anchorEl, setAnchorEl] = useState(null);
|
const [anchorEl, setAnchorEl] = useState(null);
|
||||||
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
@ -46,7 +47,7 @@ function Bar() {
|
||||||
// name: "Settings",
|
// name: "Settings",
|
||||||
// to: "/settings",
|
// to: "/settings",
|
||||||
// },
|
// },
|
||||||
{
|
!disabledAuth && {
|
||||||
name: "Log out",
|
name: "Log out",
|
||||||
divide: true,
|
divide: true,
|
||||||
onClick: onLogOutClick,
|
onClick: onLogOutClick,
|
||||||
|
@ -72,69 +73,70 @@ function Bar() {
|
||||||
</Link>
|
</Link>
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
{/* 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>
|
||||||
|
</Button>
|
||||||
|
|
||||||
{loggedIn && (
|
<Menu
|
||||||
<>
|
anchorEl={anchorEl}
|
||||||
<Button color="inherit" onClick={openMenu}>
|
open={Boolean(anchorEl)}
|
||||||
<MenuIcon></MenuIcon>
|
onClose={closeMenu}
|
||||||
</Button>
|
>
|
||||||
|
{menuItems.map((menuItem, index) => {
|
||||||
|
if (
|
||||||
|
menuItem.hasOwnProperty("condition") &&
|
||||||
|
!menuItem.condition
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
<Menu
|
let component = null;
|
||||||
anchorEl={anchorEl}
|
|
||||||
open={Boolean(anchorEl)}
|
|
||||||
onClose={closeMenu}
|
|
||||||
>
|
|
||||||
{menuItems.map((menuItem, index) => {
|
|
||||||
if (
|
|
||||||
menuItem.hasOwnProperty("condition") &&
|
|
||||||
!menuItem.condition
|
|
||||||
) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let component = null;
|
if (menuItem.to) {
|
||||||
|
component = (
|
||||||
|
<MenuItem
|
||||||
|
key={index}
|
||||||
|
component={RouterLink}
|
||||||
|
to={menuItem.to}
|
||||||
|
onClick={closeMenu}
|
||||||
|
>
|
||||||
|
{menuItem.name}
|
||||||
|
</MenuItem>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
component = (
|
||||||
|
<MenuItem
|
||||||
|
key={index}
|
||||||
|
onClick={() => {
|
||||||
|
closeMenu();
|
||||||
|
|
||||||
if (menuItem.to) {
|
menuItem.onClick();
|
||||||
component = (
|
}}
|
||||||
<MenuItem
|
>
|
||||||
key={index}
|
{menuItem.name}
|
||||||
component={RouterLink}
|
</MenuItem>
|
||||||
to={menuItem.to}
|
);
|
||||||
onClick={closeMenu}
|
}
|
||||||
>
|
|
||||||
{menuItem.name}
|
|
||||||
</MenuItem>
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
component = (
|
|
||||||
<MenuItem
|
|
||||||
key={index}
|
|
||||||
onClick={() => {
|
|
||||||
closeMenu();
|
|
||||||
|
|
||||||
menuItem.onClick();
|
if (menuItem.divide) {
|
||||||
}}
|
return (
|
||||||
>
|
<span key={index}>
|
||||||
{menuItem.name}
|
<Divider />
|
||||||
</MenuItem>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (menuItem.divide) {
|
{component}
|
||||||
return (
|
</span>
|
||||||
<span key={index}>
|
);
|
||||||
<Divider />
|
}
|
||||||
|
|
||||||
{component}
|
return component;
|
||||||
</span>
|
})}
|
||||||
);
|
</Menu>
|
||||||
}
|
</>
|
||||||
|
)}
|
||||||
return component;
|
|
||||||
})}
|
|
||||||
</Menu>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
{!loggedIn && LogIn()}
|
{!loggedIn && LogIn()}
|
||||||
</Toolbar>
|
</Toolbar>
|
||||||
</AppBar>
|
</AppBar>
|
||||||
|
|
|
@ -6,10 +6,12 @@ import { useHistory } from "react-router-dom";
|
||||||
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 history = useHistory();
|
const history = useHistory();
|
||||||
axios.get("/auth/login").then(function (response) {
|
axios.get("/auth/login").then(function (response) {
|
||||||
if (!response.data.enabled) {
|
if (!response.data.enabled) {
|
||||||
setLoggedIn(true);
|
setLoggedIn(true);
|
||||||
|
setDisableAuth(true);
|
||||||
setToken("");
|
setToken("");
|
||||||
history.go(0);
|
history.go(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,10 @@ const baseURL = "/api/";
|
||||||
export default axios.create({
|
export default axios.create({
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
responseType: "json",
|
responseType: "json",
|
||||||
headers: {
|
headers:
|
||||||
Authorization: `Bearer ${JSON.parse(localStorage.getItem("token"))}`,
|
localStorage.getItem("disableAuth") === "true"
|
||||||
},
|
? {}
|
||||||
|
: {
|
||||||
|
Authorization: `Bearer ${JSON.parse(localStorage.getItem("token"))}`,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue