-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
59 lines (53 loc) · 1.59 KB
/
Copy pathcontroller.js
File metadata and controls
59 lines (53 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @module setup/controller
* @description Express routes for the initial owner account setup wizard.
*/
import { Router } from "express";
import { registerOwner, isOwnerSetupComplete } from "./service.js";
import { registerSchema } from "../auth/validation.js";
import { validate } from "../../middleware/validate.js";
import { COOKIE_NAMES } from "../../config/constants.js";
import { env } from "../../config/env.js";
import { csrfMiddleware } from "../../middleware/csrf.js";
const router = Router();
const cookieOptions = {
httpOnly: true,
secure: env.NODE_ENV === "production",
sameSite: "strict",
path: "/",
maxAge: 7 * 24 * 60 * 60 * 1000,
};
router.use((_req, res, next) => {
if (isOwnerSetupComplete()) {
return res.redirect("/auth/login");
}
next();
});
router.get("/", csrfMiddleware, (_req, res) => {
res.render("admin/setup", {
layout: false,
title: "Setup",
error: null,
csrfToken: res.locals.csrfToken,
siteName: env.SITE_NAME,
});
});
router.post("/", csrfMiddleware, validate(registerSchema), async (req, res, next) => {
try {
const { token } = await registerOwner(req.validatedBody, req.requestId);
res.cookie(COOKIE_NAMES.AUTH_TOKEN, token, cookieOptions);
res.redirect("/admin");
} catch (err) {
if (err.statusCode === 409 || err.statusCode === 422) {
return res.status(err.statusCode).render("admin/setup", {
layout: false,
title: "Setup",
error: err.message,
csrfToken: res.locals.csrfToken,
siteName: env.SITE_NAME,
});
}
next(err);
}
});
export default router;