akf-forum/routes/admin.js

39 lines
881 B
JavaScript
Raw Normal View History

const { UserModel } = require("../models")
2022-03-21 23:53:22 +03:00
const { Router } = require("express")
const error = require("../errors/error")
const app = Router();
2022-04-03 21:01:55 +03:00
app.use(require("../middlewares/login"));
2022-04-06 21:14:46 +03:00
app.get("/", async (req, res) => {
const user = req.user;
2022-03-21 23:53:22 +03:00
2022-03-31 01:01:32 +03:00
if (!user.admin) return error(res, 403, "You have not got permissions for view to this page.");
2022-03-21 23:53:22 +03:00
res.render("admin", { user, user2: false })
2022-04-03 21:01:55 +03:00
});
2022-03-21 23:53:22 +03:00
2022-04-06 21:14:46 +03:00
app.post("/", async (req, res) => {
2022-03-21 23:53:22 +03:00
2022-04-06 21:14:46 +03:00
const user = req.user;
2022-03-21 23:53:22 +03:00
2022-03-31 01:01:32 +03:00
if (!user.admin) return error(res, 403, "You have not got permissions for view to this page.");
const user2 = await UserModel.get(req.body.userid);
2022-03-21 23:53:22 +03:00
2022-04-06 21:14:46 +03:00
if (!user2)
return error(res, 404, "We have not got this user in all of the forum. Vesselam.");
2022-03-21 23:53:22 +03:00
else {
user2.admin = true;
await user2.save()
2022-03-21 23:53:22 +03:00
}
res.render("admin", { user, user2 })
});
2022-03-31 01:01:32 +03:00
module.exports = app;