2020-11-02 13:43:12 +01:00
|
|
|
const express = require("express");
|
|
|
|
const router = express.Router();
|
|
|
|
const models = require("../models");
|
|
|
|
const error = require("./utils/error");
|
2020-11-02 17:04:20 +01:00
|
|
|
const sessionCheck = require("./utils/sessionCheck");
|
|
|
|
|
2020-11-02 13:43:12 +01:00
|
|
|
|
|
|
|
router.get("/check", async (req, res) => {
|
|
|
|
if (!req.query.token)
|
|
|
|
return error(req, res, "Missing argument", 400);
|
2020-12-12 18:28:44 +01:00
|
|
|
const user = await models.User.findOne({where: {emailToken: req.query.token}});
|
2020-11-02 13:43:12 +01:00
|
|
|
if (user) {
|
|
|
|
user.emailVerified = true;
|
|
|
|
if (user.email.endsWith("@etu.univ-lyon1.fr"))
|
|
|
|
user.permissions = 1;
|
|
|
|
else if (user.email.endsWith("@univ-lyon1.fr"))
|
|
|
|
user.permissions = 2;
|
|
|
|
await user.save();
|
|
|
|
res.redirect("/");
|
|
|
|
} else
|
2020-11-02 17:04:20 +01:00
|
|
|
return error(req, res, "Invalid token", 400);
|
2020-11-02 13:43:12 +01:00
|
|
|
});
|
|
|
|
|
2020-11-02 17:04:20 +01:00
|
|
|
router.get("/forget", sessionCheck(-1), async (req, res) => {
|
|
|
|
if (!req.query.token)
|
|
|
|
res.render("forget", {title: "L'ETU"});
|
|
|
|
else {
|
2020-12-12 18:28:44 +01:00
|
|
|
const user = await models.User.findOne({where: {passwordToken: data.token}});
|
2020-11-02 17:04:20 +01:00
|
|
|
if (!user)
|
|
|
|
return error(req, res, "Invalid token", 400);
|
|
|
|
else if (user.passwordTokenDate && ((new Date().getTime() - user.passwordTokenDate.getTime()) / 1000 > 3600))
|
|
|
|
return error(req, res, "Token expired", 400);
|
|
|
|
else
|
|
|
|
res.render("forget", {title: "L'ETU - Forget password"});
|
|
|
|
}
|
|
|
|
});
|
2020-11-02 13:43:12 +01:00
|
|
|
|
|
|
|
module.exports = router;
|