"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthModule = void 0;
const api_mail_1 = require("@hed-hog/api-mail");
const api_prisma_1 = require("@hed-hog/api-prisma");
const axios_1 = require("@nestjs/axios");
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const core_1 = require("@nestjs/core");
const jwt_1 = require("@nestjs/jwt");
const cookie_parser_1 = __importDefault(require("cookie-parser"));
const challenge_module_1 = require("../challenge/challenge.module");
const challenge_service_1 = require("../challenge/challenge.service");
const mail_sent_module_1 = require("../mail-sent/mail-sent.module");
const mail_module_1 = require("../mail/mail.module");
const security_module_1 = require("../security/security.module");
const session_module_1 = require("../session/session.module");
const setting_module_1 = require("../setting/setting.module");
const token_module_1 = require("../token/token.module");
const token_service_1 = require("../token/token.service");
const user_module_1 = require("../user/user.module");
const auth_controller_1 = require("./auth.controller");
const auth_service_1 = require("./auth.service");
const auth_guard_1 = require("./guards/auth.guard");
let AuthModule = class AuthModule {
    configure(consumer) {
        consumer
            .apply((0, cookie_parser_1.default)())
            .forRoutes('auth');
    }
};
exports.AuthModule = AuthModule;
exports.AuthModule = AuthModule = __decorate([
    (0, common_1.Global)(),
    (0, common_1.Module)({
        imports: [
            (0, common_1.forwardRef)(() => jwt_1.JwtModule.registerAsync({
                global: true,
                imports: [api_prisma_1.PrismaModule],
                inject: [api_prisma_1.PrismaService],
                useFactory: async (prisma) => {
                    try {
                        const authSettings = await prisma.setting.findMany({
                            where: {
                                slug: {
                                    in: [
                                        'access-token-expiration-minutes'
                                    ]
                                }
                            },
                            select: {
                                slug: true,
                                value: true,
                            }
                        });
                        const settings = new Map();
                        for (const setting of authSettings) {
                            settings.set(setting.slug, setting.value);
                        }
                        const expirationMinutes = settings.get('access-token-expiration-minutes');
                        const expiresIn = expirationMinutes ? parseInt(expirationMinutes, 10) * 60 : 15 * 60;
                        return {
                            secret: String(process.env.JWT_SECRET),
                            global: true,
                            signOptions: {
                                expiresIn, // in seconds
                            },
                        };
                    }
                    catch (error) {
                        console.error('Error loading JWT settings from database:', error);
                        throw error;
                    }
                },
            })),
            (0, common_1.forwardRef)(() => api_prisma_1.PrismaModule),
            (0, common_1.forwardRef)(() => mail_module_1.MailModule),
            (0, common_1.forwardRef)(() => setting_module_1.SettingModule),
            (0, common_1.forwardRef)(() => mail_sent_module_1.MailSentModule),
            (0, common_1.forwardRef)(() => api_mail_1.MailModule),
            (0, common_1.forwardRef)(() => user_module_1.UserModule),
            (0, common_1.forwardRef)(() => security_module_1.SecurityModule),
            (0, common_1.forwardRef)(() => token_module_1.TokenModule),
            (0, common_1.forwardRef)(() => challenge_module_1.ChallengeModule),
            (0, common_1.forwardRef)(() => session_module_1.SessionModule),
            config_1.ConfigModule,
            axios_1.HttpModule,
        ],
        controllers: [auth_controller_1.AuthController],
        providers: [
            core_1.Reflector,
            auth_service_1.AuthService,
            token_service_1.TokenService,
            challenge_service_1.ChallengeService,
            {
                provide: core_1.APP_GUARD,
                useClass: auth_guard_1.AuthGuard,
            },
        ],
        exports: [auth_service_1.AuthService],
    })
], AuthModule);
//# sourceMappingURL=auth.module.js.map