mirror of
https://github.com/UNITRONIX/BetterDesk.git
synced 2026-09-10 17:45:42 +00:00
32e29723e4
Introduce a new Node.js-based web console (Express + EJS + better-sqlite3) under web-nodejs/ and add installer support to choose between Node.js and the legacy Flask console. Update interactive ALL-IN-ONE installers (betterdesk.sh, betterdesk.ps1) with flags/options for --nodejs/--flask, automatic Node.js installation, migration logic, enhanced service handling and diagnostics. Bump VERSION to 2.2.0 and update README and project docs (.github/copilot-instructions.md) to reflect the new console, usage examples, and Docker/docs changes. Many new web-nodejs files and supporting middleware/services/routes/views/static assets were added to support the new console.
111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
/**
|
|
* BetterDesk Console - Auth Middleware
|
|
* Protects routes that require authentication
|
|
*/
|
|
|
|
/**
|
|
* Require authentication middleware
|
|
* Redirects to login page for HTML requests, returns 401 for API requests
|
|
*/
|
|
function requireAuth(req, res, next) {
|
|
if (!req.session || !req.session.userId) {
|
|
// API request
|
|
if (req.path.startsWith('/api/')) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
error: 'Unauthorized. Please log in.'
|
|
});
|
|
}
|
|
|
|
// HTML request - redirect to login
|
|
return res.redirect('/login');
|
|
}
|
|
|
|
// Add user info to locals for templates
|
|
res.locals.user = req.session.user;
|
|
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* Require specific role
|
|
*/
|
|
function requireRole(role) {
|
|
return function(req, res, next) {
|
|
if (!req.session || !req.session.userId) {
|
|
if (req.path.startsWith('/api/')) {
|
|
return res.status(401).json({ success: false, error: 'Unauthorized' });
|
|
}
|
|
return res.redirect('/login');
|
|
}
|
|
|
|
if (req.session.user.role !== role && req.session.user.role !== 'admin') {
|
|
if (req.path.startsWith('/api/')) {
|
|
return res.status(403).json({ success: false, error: 'Forbidden' });
|
|
}
|
|
return res.status(403).render('error', {
|
|
title: 'Forbidden',
|
|
message: 'You do not have permission to access this resource'
|
|
});
|
|
}
|
|
|
|
res.locals.user = req.session.user;
|
|
next();
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Optional auth - add user to locals if logged in, but don't require it
|
|
*/
|
|
function optionalAuth(req, res, next) {
|
|
if (req.session && req.session.userId) {
|
|
res.locals.user = req.session.user;
|
|
} else {
|
|
res.locals.user = null;
|
|
}
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* Guest only - redirect to dashboard if already logged in
|
|
*/
|
|
function guestOnly(req, res, next) {
|
|
if (req.session && req.session.userId) {
|
|
return res.redirect('/');
|
|
}
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* Require admin role
|
|
*/
|
|
function requireAdmin(req, res, next) {
|
|
if (!req.session || !req.session.userId) {
|
|
if (req.path.startsWith('/api/')) {
|
|
return res.status(401).json({ success: false, error: 'Unauthorized' });
|
|
}
|
|
return res.redirect('/login');
|
|
}
|
|
|
|
if (req.session.user.role !== 'admin') {
|
|
if (req.path.startsWith('/api/')) {
|
|
return res.status(403).json({ success: false, error: 'Admin access required' });
|
|
}
|
|
return res.status(403).render('errors/403', {
|
|
title: 'Forbidden',
|
|
message: 'You do not have permission to access this resource'
|
|
});
|
|
}
|
|
|
|
res.locals.user = req.session.user;
|
|
next();
|
|
}
|
|
|
|
module.exports = {
|
|
requireAuth,
|
|
requireRole,
|
|
requireAdmin,
|
|
optionalAuth,
|
|
guestOnly
|
|
};
|