I'm encountering an issue with custom middleware in ui_base.js not properly enforcing session validation before serving the dashboard page. Despite setting up the middleware to check for a valid user session, users are still able to access the dashboard page directly without being redirected to the login page if they don't have a valid session.
const cookieParser = require('cookie-parser'); // Parse the cookie from headers
// Cookie parsing Middleware
uiShared.app.use(cookieParser());
// Middleware to check logged in user session
function checkSession(req, res, next) {
const sessionId = req.cookies['sessionId']; // session ID is stored in a cookie
const userSession = global.get(sessionId); // Sessions from global context
// Validate the session
if (userSession && userSession[sessionId]) {
// If session is valid, continue to serve the dashboard
next();
} else {
// If no session or invalid session, redirect to login
res.redirect('http://localhost:1880/login/');
}
}
// Serve dashboard with session validation
uiShared.app.get(config.path, uiShared.httpMiddleware, checkSession, (req, res) => {
// Send the dashboard file only if session is valid
res.sendFile(path.join(__dirname, '../../dist/index.html'));
});
Issue
Even though checkSession
middleware is placed before the route handler for the dashboard page, users are able to access the dashboard page directly without being redirected if they don't have a valid session.
Expected Behavior:
Users without a valid session should be redirected to the login page, and the dashboard page should only be accessible if a valid session is present.
Questions:
- Is there an issue with the way middleware is set up or ordered?
- How can I ensure that the middleware correctly enforces session validation before serving the dashboard page?
Any insights or suggestions would be greatly appreciated!