// ... import { createRoutes } from './routes'; import createRedux from './redux/createRedux'; import fetchComponentsData from './core/fetchComponentsData'; import renderTemplate from './core/renderTemplate'; import { NeedRedirect } from './core/auth-helpers.js'; import bindCheckAuth from './core/bindCheckAuth'; // ... // server configuration // ... server.get('*', (req, res, next) => { try { const redux = createRedux(); const requireAccess = bindCheckAuth(redux, (nextState) => { /* Current version need this hack to avoid infinite /login redirect Need to fetch error states: 1) "user not authorized" => [401] => redirect to login 2) "access denied" => [403] => redirect to 403 page */ if (nextState.location.pathname === '/login') { return; } throw new NeedRedirect('/login?next=' + nextState.location.pathname); }); const routes = createRoutes(requireAccess); const location = new Location(req.path, req.query); Router.run(routes, location, async (error, initialState) => { try { const state = await fetchComponentsData(initialState.components, redux); const html = renderTemplate(redux, state, initialState, location); res.send(html).end(); } catch (err) { res.status(500).send(err.stack); next(err); } }); } catch (err) { if (err instanceof NeedRedirect) { // Redirect res.set('Content-Type', 'text/html'); res.status(401).send(''); } else { res.status(500).send(err.stack); next(err); } } });