zero-ui/backend/routes/auth.js

31 lines
752 B
JavaScript
Raw Normal View History

2023-10-07 04:11:40 +08:00
import express from "express";
2021-03-22 03:25:13 +08:00
const router = express.Router();
import * as auth from "../services/auth.js";
2021-03-22 03:25:13 +08:00
2022-05-24 10:09:30 +08:00
router.get("/login", async function (req, res) {
if (process.env.ZU_DISABLE_AUTH === "true") {
res.send({ enabled: false });
} else {
res.send({ enabled: true });
}
});
2021-03-22 03:25:13 +08:00
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) {
2021-03-22 03:25:13 +08:00
if (user) {
res.send({ token: user["token"] });
} else {
res.status(401).send({
error: err.message,
});
}
});
} else {
res.status(400).send({ error: "Specify username and password" });
}
});
2023-10-07 04:11:40 +08:00
export default router;