130 lines
2.9 KiB
JavaScript
130 lines
2.9 KiB
JavaScript
import express from "express";
|
|
import passport from "passport";
|
|
import { ensureLoggedIn } from "connect-ensure-login";
|
|
|
|
import Pages from "../middleware/Pages";
|
|
import Auth from "../middleware/Auth";
|
|
|
|
import render from "../libs/format";
|
|
|
|
// eslint-disable-next-line new-cap
|
|
const router = express.Router();
|
|
|
|
router.route("/").get((req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "home");
|
|
|
|
page.setPageTitle("Présentation du projet");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
router
|
|
.route("/connexion")
|
|
.get((req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "connexion");
|
|
|
|
page.setPageTitle("Connexion");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
})
|
|
.post(
|
|
passport.authenticate("user", {
|
|
failureRedirect: "/connexion",
|
|
failureFlash: true,
|
|
}),
|
|
(req, res) => {
|
|
const { next, query } = req.body;
|
|
let url = `/${next}`;
|
|
|
|
if (next) {
|
|
if (query) {
|
|
const params = JSON.parse(query);
|
|
Object.keys(params).forEach((key) => {
|
|
url += `${url.indexOf("?") === -1 ? "?" : "&"}${key}=${
|
|
params[key]
|
|
}`;
|
|
});
|
|
}
|
|
return res.redirect(url);
|
|
}
|
|
return res.redirect("/");
|
|
}
|
|
);
|
|
|
|
router
|
|
.route("/inscription")
|
|
.get((req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "inscription");
|
|
|
|
page.setPageTitle("Inscription");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
})
|
|
.post(async (req, res) => {
|
|
try {
|
|
await Auth.register(req);
|
|
|
|
res.redirect("/");
|
|
} catch (err) {
|
|
res.redirect("/inscription");
|
|
}
|
|
});
|
|
|
|
router
|
|
.route("/ajouter-un-album")
|
|
.get(ensureLoggedIn("/connexion"), (req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "ajouter-un-album");
|
|
|
|
page.setPageTitle("Ajouter un album");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
router.route("/nous-contacter").get(async (req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "nous-contacter");
|
|
|
|
page.setPageTitle("Nous contacter");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
router.route("/composants").get(async (req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "composants");
|
|
|
|
page.setPageTitle("Les composants");
|
|
|
|
render(res, page);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
router.route("/se-deconnecter").get((req, res) => {
|
|
req.logout();
|
|
req.session.destroy(() => {
|
|
res.redirect("/");
|
|
});
|
|
});
|
|
|
|
export default router;
|