@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
47 lines (39 loc) • 1.15 kB
JavaScript
import express from 'express';
import { v4 as uuid } from 'uuid';
/** @typedef {import('../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */
/**
* Scope middleware for checking the query parameter if it contains ?scope.
*
* @param {DevCmsConfig} config
*/
export default (config) => {
// eslint-disable-next-line new-cap
const router = express.Router();
router.route('/').get((req, res, next) => {
let scope = config.scope;
if (req.query.scope) {
scope = JSON.parse(req.query.scope);
if (
req.hasFontoSession(scope) &&
config.alwaysRegenerateSessionToken
) {
// We must re-generate a session
scope = Object.assign(scope, { editSessionToken: uuid() });
res.redirect(
`?scope=${encodeURIComponent(JSON.stringify(scope))}`
);
// Actually create the session after the redirect
return;
}
if (scope.editSessionToken) {
req.ensureFontoSession(scope);
next();
return;
}
}
// No edit session token, start a new session
scope = { editSessionToken: uuid(), ...scope };
res.redirect(`?scope=${encodeURIComponent(JSON.stringify(scope))}`);
});
return router;
};