fix: prefer admin token on admin routes (#23 #28)
Test and Lint / backend-test (push) Successful in 2m9s
Test and Lint / frontend-test (push) Successful in 2m31s

This commit is contained in:
2025-09-21 22:37:30 +02:00
parent 8611206396
commit d4404e39bd
+25 -5
View File
@@ -75,17 +75,37 @@ if (enableHsts) {
app.use(cookieParser()); app.use(cookieParser());
app.use((req, res, next) => { app.use((req, res, next) => {
if (!req.headers.authorization) { if (req.headers.authorization) {
const slugMatch = req.path.match(/\/api\/(?:gallery|secure-images)\/([^\/]+)/); return next();
const slug = slugMatch ? slugMatch[1] : req.requestedSlug; }
const galleryToken = getGalleryTokenFromRequest(req, slug);
const adminToken = getAdminTokenFromRequest(req);
const path = req.path || '';
const slugMatch = path.match(/\/api\/(?:gallery|secure-images)\/([^\/]+)/);
const slug = slugMatch ? slugMatch[1] : req.requestedSlug;
const adminToken = getAdminTokenFromRequest(req);
const galleryToken = getGalleryTokenFromRequest(req, slug);
const isAdminRequest = path.startsWith('/api/admin') || path.startsWith('/admin');
const isGalleryRequest = Boolean(slugMatch)
|| path.startsWith('/api/gallery')
|| path.startsWith('/gallery')
|| path.startsWith('/api/secure-images');
// Prefer admin credentials on admin routes so gallery sessions cannot override them.
if (isAdminRequest) {
if (adminToken) {
req.headers.authorization = `Bearer ${adminToken}`;
}
} else if (isGalleryRequest) {
if (galleryToken) { if (galleryToken) {
req.headers.authorization = `Bearer ${galleryToken}`; req.headers.authorization = `Bearer ${galleryToken}`;
} else if (adminToken) { } else if (adminToken) {
req.headers.authorization = `Bearer ${adminToken}`; req.headers.authorization = `Bearer ${adminToken}`;
} }
} else if (adminToken) {
req.headers.authorization = `Bearer ${adminToken}`;
} else if (galleryToken) {
req.headers.authorization = `Bearer ${galleryToken}`;
} }
next(); next();