"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authenticateToken = void 0;
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const index_1 = require("../index");
const authenticateToken = async (req, res, next) => {
    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];
    if (!token) {
        return res.status(401).json({
            success: false,
            message: 'Access token required',
        });
    }
    try {
        const jwtSecret = process.env['JWT_SECRET'];
        if (!jwtSecret) {
            return res.status(500).json({
                success: false,
                message: 'Server configuration error',
            });
        }
        const decoded = jsonwebtoken_1.default.verify(token, jwtSecret);
        const user = await index_1.prisma.adminUser.findUnique({
            where: { id: decoded.userId },
        });
        if (!user) {
            return res.status(401).json({
                success: false,
                message: 'Invalid token - user not found',
            });
        }
        req.user = {
            userId: decoded.userId,
            username: decoded.username,
        };
        next();
    }
    catch (error) {
        return res.status(403).json({
            success: false,
            message: 'Invalid or expired token',
        });
    }
};
exports.authenticateToken = authenticateToken;
//# sourceMappingURL=auth.js.map