JaBa/dashboard/routes/settings.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const express = require("express"),
CheckAuth = require("../auth/CheckAuth"),
router = express.Router();
// Gets profile page
router.get("/", CheckAuth, async function(req, res) {
res.render("settings", {
user: req.userInfos,
translate: req.translate,
printDate: req.printDate,
currentURL: `${req.client.config.dashboard.baseURL}/${req.originalUrl}`
});
});
2021-12-16 23:42:58 +05:00
router.post("/", CheckAuth, async function(req, res) {
2021-12-10 21:39:54 +05:00
const user = await req.client.findOrCreateUser({ id: req.user.id });
const data = req.body;
2021-12-16 23:42:58 +05:00
if (data.bio) user.bio = data.bio;
2021-12-10 21:39:54 +05:00
if (data.birthdate) {
if (checkDate(data.birthdate)) {
user.birthdate = checkDate(data.birthdate);
user.markModified("birthdate");
};
};
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
await user.save();
res.redirect(303, "/settings");
});
module.exports = router;
/**
* @returns {Boolean}
*/
2021-12-16 23:42:58 +05:00
function checkDate(birthdate) {
2021-12-10 21:39:54 +05:00
const [day, month, year] = birthdate;
if (!day || !month || !year) return false;
const match = birthdate.match(/\d+/g);
if (!match) return false;
const tday = + match[0], tmonth = + match[1] - 1;
let tyear = + match[2];
2021-12-16 23:42:58 +05:00
if (tyear < 100) tyear += tyear < 50 ? 2000 : 1900;
2021-12-10 21:39:54 +05:00
const d = new Date(tyear, tmonth, tday);
if (!(tday == d.getDate() && tmonth == d.getMonth() && tyear == d.getFullYear())) return false;
if (d.getTime() > Date.now()) return false;
if (d.getTime() < (Date.now() - 2.523e+12)) return false;
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
return d;
};