"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSocketIOAuthenticator = createSocketIOAuthenticator;
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const config_1 = require("../modules/config");
/**
 * Build a Socket.IO handshake authenticator from the startup config.
 *
 * Uses the same JWT secret as `requireAuth` — either the explicit
 * secret from startConfig.auth.secret or the JWT_SECRET env var.
 *
 * Returns `undefined` when no secret is configured (e.g. tests or
 * deployments that intentionally skip auth), which leaves Socket.IO
 * open as before.
 */
function createSocketIOAuthenticator(startConfig) {
    const secret = startConfig.auth?.secret ?? config_1.config.JWT_SECRET;
    if (!secret)
        return undefined;
    return (token) => {
        try {
            const payload = jsonwebtoken_1.default.verify(token, secret);
            return !!(payload && typeof payload === 'object' && payload.userId);
        }
        catch {
            return false;
        }
    };
}
