function readCookie(cookieHeader, cookieName) {
    if (!cookieHeader) {
        return null;
    }
    for (const cookiePair of cookieHeader.split(';')) {
        const [name, ...valueParts] = cookiePair.trim().split('=');
        if (name === cookieName && valueParts.length > 0) {
            return valueParts.join('=').trim();
        }
    }
    return null;
}
function writeUnauthorized(res) {
    res.status(401).json({
        error: 'Unauthorized',
        message: 'Admin dashboard session is missing or expired.',
        timestamp: new Date().toISOString()
    });
}
export function createSessionAuthMiddleware(config) {
    return (req, res, next) => {
        const sessionId = readCookie(req.headers.cookie, config.cookieName);
        if (!sessionId) {
            writeUnauthorized(res);
            return;
        }
        const session = config.store.touch(sessionId);
        if (!session) {
            writeUnauthorized(res);
            return;
        }
        next();
    };
}
//# sourceMappingURL=session-auth.middleware.js.map