"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthController = void 0;
const api_1 = require("@hed-hog/api");
const api_locale_1 = require("@hed-hog/api-locale");
const common_1 = require("@nestjs/common");
const token_service_1 = require("../token/token.service");
const create_with_email_and_password_dto_1 = require("../user/dto/create-with-email-and-password.dto");
const user_service_1 = require("../user/user.service");
const auth_service_1 = require("./auth.service");
const forget_dto_1 = require("./dto/forget.dto");
const login_email_verification_resend_dto_1 = require("./dto/login-email-verification-resend.dto");
const login_email_verification_dto_1 = require("./dto/login-email-verification.dto");
const login_with_code_dto_1 = require("./dto/login-with-code.dto");
const login_with_recovery_code_1 = require("./dto/login-with-recovery-code");
const login_dto_1 = require("./dto/login.dto");
const resend_mfa_code_dto_1 = require("./dto/resend-mfa-code.dto");
const reset_dto_1 = require("./dto/reset.dto");
let AuthController = class AuthController {
    constructor(service, token, user) {
        this.service = service;
        this.token = token;
        this.user = user;
    }
    async verify({ id }, locale) {
        return this.service.verifyUser(locale, id);
    }
    async roles(user, locale) {
        const roles = !user || !user.id ? [] : await this.service.verifyRoles(locale, user.id);
        return { roles };
    }
    async refresh(locale, req, res, refreshTokenFromBody, userAgent, ipAddress) {
        const currentRefreshToken = req.cookies['rt'] || refreshTokenFromBody;
        const { session, refreshToken, accessToken } = await this.service.refreshAccessToken(locale, currentRefreshToken, ipAddress, userAgent);
        await this.token.setRefreshTokenCookie(locale, res, refreshToken, session.expires_at);
        return {
            accessToken,
            refreshToken,
        };
    }
    async login(locale, data, ipAddress, userAgent, res) {
        return this.service.loginWithEmailAndPassword(res, locale, ipAddress, userAgent, data);
    }
    async emailVerificationLogin(locale, { token, code }, ipAddress, userAgent, res) {
        return this.service.emailVerificationLogin(locale, token, code, ipAddress, userAgent, res);
    }
    async emailVerificationLoginResend(locale, { token }) {
        return this.service.emailVerificationLoginResend(locale, token);
    }
    async registerWithEmailAndPassword(locale, data) {
        return this.user.createWithEmailAndPassword(locale, data);
    }
    async loginCode(locale, { token, code, methodType }, ipAddress, userAgent, res) {
        const { accessToken, refreshToken, session, requiresPasswordReset } = await this.service.verifyMfaCode(locale, token, code, ipAddress, userAgent, methodType);
        await this.token.setRefreshTokenCookie(locale, res, refreshToken, session.expires_at);
        return { accessToken, refreshToken, requiresPasswordReset };
    }
    async loginRecoveryCode(locale, { token, code }, ipAddress, userAgent, res) {
        const { accessToken, refreshToken, session, requiresPasswordReset } = await this.service.verifyMfaRecoveryCode(locale, token, code, ipAddress, userAgent);
        await this.token.setRefreshTokenCookie(locale, res, refreshToken, session.expires_at);
        return { accessToken, refreshToken, requiresPasswordReset };
    }
    async resendMfaCode(locale, { token }) {
        return this.service.resendMfaCode(locale, token);
    }
    async generateWebAuthnOptions(locale, { mfaToken }) {
        return this.service.generateWebAuthnAuthenticationOptions(locale, mfaToken);
    }
    async verifyWebAuthn(locale, { mfaToken, assertionResponse }, ipAddress, userAgent, res) {
        const { accessToken, refreshToken, session, requiresPasswordReset } = await this.service.verifyWebAuthnAuthentication(locale, mfaToken, assertionResponse, ipAddress, userAgent);
        await this.token.setRefreshTokenCookie(locale, res, refreshToken, session.expires_at);
        return { accessToken, refreshToken, requiresPasswordReset };
    }
    async forgot(locale, data) {
        return this.service.forgotPassword(locale, data);
    }
    async logout(req, res, refreshTokenFromBody) {
        const refreshToken = req.cookies['rt'] || refreshTokenFromBody;
        if (!refreshToken) {
            throw new common_1.UnauthorizedException('Refresh token not provided');
        }
        await this.service.logout(res, req, refreshToken);
        return { success: true };
    }
    async forgotReset(locale, { password, code }, ipAddress, userAgent) {
        return this.service.forgotResetPassword(locale, password, code, ipAddress, userAgent);
    }
};
exports.AuthController = AuthController;
__decorate([
    (0, api_1.NoRole)(),
    (0, common_1.Get)('verify'),
    __param(0, (0, api_1.User)()),
    __param(1, (0, api_locale_1.Locale)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "verify", null);
__decorate([
    (0, api_1.NoRole)(),
    (0, common_1.Get)('roles'),
    __param(0, (0, api_1.UserOptional)()),
    __param(1, (0, api_locale_1.Locale)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "roles", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('refresh'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Req)()),
    __param(2, (0, common_1.Res)({ passthrough: true })),
    __param(3, (0, common_1.Body)('refreshToken')),
    __param(4, (0, common_1.Headers)('user-agent')),
    __param(5, (0, common_1.Ip)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object, String, String, String]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "refresh", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('login'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __param(4, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, login_dto_1.LoginDTO, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "login", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('login-email-verification'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __param(4, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, login_email_verification_dto_1.LoginEmailVerificationDTO, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "emailVerificationLogin", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('login-email-verification-resend'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, login_email_verification_resend_dto_1.LoginEmailVerificationResendDTO]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "emailVerificationLoginResend", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('signup'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, create_with_email_and_password_dto_1.CreateWithEmailAndPasswordDTO]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "registerWithEmailAndPassword", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('login-code'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __param(4, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, login_with_code_dto_1.LoginWithCodeDTO, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "loginCode", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('login-recovery-code'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __param(4, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, login_with_recovery_code_1.LoginWithRecoveryCodeDTO, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "loginRecoveryCode", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('resend-mfa-code'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, resend_mfa_code_dto_1.ResendMfaCodeDTO]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "resendMfaCode", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('webauthn/generate'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "generateWebAuthnOptions", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('webauthn/verify'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __param(4, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "verifyWebAuthn", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('forgot'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, forget_dto_1.ForgetDTO]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "forgot", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('logout'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Res)({ passthrough: true })),
    __param(2, (0, common_1.Body)('refreshToken')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, String]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "logout", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Post)('forgot-reset'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Ip)()),
    __param(3, (0, common_1.Headers)('user-agent')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, reset_dto_1.ResetDTO, String, String]),
    __metadata("design:returntype", Promise)
], AuthController.prototype, "forgotReset", null);
exports.AuthController = AuthController = __decorate([
    (0, common_1.Controller)('auth'),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => auth_service_1.AuthService))),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => token_service_1.TokenService))),
    __param(2, (0, common_1.Inject)((0, common_1.forwardRef)(() => user_service_1.UserService))),
    __metadata("design:paramtypes", [auth_service_1.AuthService,
        token_service_1.TokenService,
        user_service_1.UserService])
], AuthController);
//# sourceMappingURL=auth.controller.js.map