MusicTopus/src/routes/index.js
Damien Broqua 9a088b9b5f #29 - Avoir un titre distinct par page (#34)
Co-authored-by: dbroqua <contact@darkou.fr>
Reviewed-on: #34
2022-03-06 15:11:42 +01:00

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;