82 lines
1.9 KiB
JavaScript
82 lines
1.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(ensureLoggedIn("/connexion"), (req, res) =>
|
|
res.redirect("/ma-collection")
|
|
);
|
|
|
|
router
|
|
.route("/connexion")
|
|
.get((req, res, next) => {
|
|
try {
|
|
const page = new Pages(req, "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");
|
|
|
|
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("/se-deconnecter").get((req, res) => {
|
|
req.logout();
|
|
req.session.destroy(() => {
|
|
res.redirect("/");
|
|
});
|
|
});
|
|
|
|
export default router;
|