mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:39:28 +00:00
Add middleware to prevent unauthorised user from accessing private pages
This commit is contained in:
21
app.js
21
app.js
@@ -82,6 +82,27 @@ async function main() {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Authentication middleware that redirects unauthenticated users
|
||||||
|
// back to the login page if they request a page they don't have access
|
||||||
|
// to
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
const allowed = [
|
||||||
|
'/login',
|
||||||
|
'/register',
|
||||||
|
'/password-reset',
|
||||||
|
'/change-password',
|
||||||
|
'/'
|
||||||
|
];
|
||||||
|
|
||||||
|
// Extract the first component of the path from the request
|
||||||
|
const path = `/${req.path.split('/')?.[1] ?? ''}`;
|
||||||
|
|
||||||
|
if (!(allowed.includes(path) || req.session.authenticated))
|
||||||
|
return res.redirect('/login');
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
app.get('*', (req, res, next) => {
|
app.get('*', (req, res, next) => {
|
||||||
req.app.locals.layout = 'main';
|
req.app.locals.layout = 'main';
|
||||||
next();
|
next();
|
||||||
|
|||||||
Reference in New Issue
Block a user