JaBa/dashboard/routes/profile.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-01-04 02:18:28 +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("profile", {
2022-01-04 02:18:28 +05:00
user: req.userInfos,
translate: req.translate,
printDate: req.printDate,
currentURL: `${req.client.config.dashboard.baseURL}/${req.originalUrl}`
});
});
router.post("/", CheckAuth, async function(req, res) {
const user = await req.client.findOrCreateUser({ id: req.user.id });
const data = req.body;
if (data.bio) user.bio = data.bio;
if (data.birthdate) {
if (checkDate(data.birthdate)) {
user.birthdate = checkDate(data.birthdate);
user.markModified("birthdate");
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
await user.save();
res.redirect(303, "/profile");
2022-01-04 02:18:28 +05:00
});
module.exports = router;
/**
* @returns {Boolean}
*/
function checkDate(birthdate) {
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];
if (tyear < 100) tyear += tyear < 50 ? 2000 : 1900;
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;
return d;
2022-01-13 00:26:23 +05:00
}