feat: implement authentication check endpoint and login page redirection

This commit is contained in:
Greg 2025-05-27 00:17:24 +02:00
parent 7a789e7060
commit 7ed0bca7c2
3 changed files with 52 additions and 5 deletions

View File

@ -71,10 +71,21 @@ function initAuth(app) {
res.redirect('/login'); res.redirect('/login');
}); });
// Authentication check endpoint for Nginx auth_request
app.get('/auth/check', (req, res) => {
if (req.session.authenticated) {
return res.status(200).send('OK');
}
return res.status(401).send('Unauthorized');
});
// Authentication middleware for all other routes // Authentication middleware for all other routes
app.use((req, res, next) => { app.use((req, res, next) => {
// Skip auth for login-related routes // Skip auth for login-related routes and static assets
if (req.path === '/login' || req.path === '/auth/login') { if (req.path === '/login' ||
req.path === '/auth/login' ||
req.path === '/auth/check' ||
req.path.match(/\.(css|js|png|jpg|jpeg|gif|ico|svg)$/)) {
return next(); return next();
} }

View File

@ -25,6 +25,20 @@ app.use(bodyParser.json({ limit: '5mb' }));
app.use(bodyParser.urlencoded({ extended: true })); // For parsing form data app.use(bodyParser.urlencoded({ extended: true })); // For parsing form data
app.use(express.static('public')); // Serve static files app.use(express.static('public')); // Serve static files
// Copy login.html to the correct location for serving
const loginHtmlPath = path.join(__dirname, 'login.html');
if (fs.existsSync(loginHtmlPath)) {
// Ensure public directory exists
const publicDir = path.join(__dirname, 'public');
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true });
}
// Copy login.html to public directory
fs.copyFileSync(loginHtmlPath, path.join(publicDir, 'login.html'));
console.log('Login page copied to public directory');
}
// Initialize authentication middleware // Initialize authentication middleware
initAuth(app); initAuth(app);

View File

@ -8,15 +8,22 @@ server {
gzip on; gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Serve static files directly # Authentication check - redirect to login if not authenticated
location / { location = / {
# First try to use the API to check authentication
auth_request /auth/check;
# If auth passes, serve the main page
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
# CORS headers for main location # CORS headers
add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always; add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
# Error indicates not authenticated, redirect to login
error_page 401 = @error401;
# Handle preflight requests # Handle preflight requests
if ($request_method = 'OPTIONS') { if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Origin' '*';
@ -28,6 +35,21 @@ server {
return 204; return 204;
} }
} }
# Serve static files directly
location / {
try_files $uri $uri/ /index.html;
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
}
# Handle 401 unauthorized by redirecting to login
location @error401 {
return 302 /login;
}
# Proxy requests to the data API # Proxy requests to the data API
location /data/ { location /data/ {