"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PhoneAuthProvider = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("typeorm");
const user_entity_1 = require("../../user/entities/user.entity");
const identity_entity_1 = require("../../user/entities/identity.entity");
const base_auth_provider_1 = require("./base-auth.provider");
const auth_constants_1 = require("../../auth.constants");
let PhoneAuthProvider = class PhoneAuthProvider extends base_auth_provider_1.BaseAuthProvider {
    dataSource;
    providerName = auth_constants_1.PHONE_AUTH_PROVIDER;
    constructor(dataSource) {
        const userRepository = dataSource.getRepository(user_entity_1.NestAuthUser);
        const authIdentityRepository = dataSource.getRepository(identity_entity_1.NestAuthIdentity);
        super(userRepository, authIdentityRepository);
        this.dataSource = dataSource;
        this.enabled = this.options.phoneAuth?.enabled;
    }
    async validate(credentials) {
        const identity = await this.findIdentity(credentials.phone);
        if (!identity?.user || !(await identity.user.validatePassword(credentials.password))) {
            throw new common_1.UnauthorizedException('Invalid credentials');
        }
        return {
            userId: identity.user?.phone,
            phone: identity.user?.phone || '',
            metadata: identity.user,
        };
    }
    getRequiredFields() {
        return ['phone', 'password'];
    }
    linkUserWith() {
        return 'phone';
    }
};
exports.PhoneAuthProvider = PhoneAuthProvider;
exports.PhoneAuthProvider = PhoneAuthProvider = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [typeorm_1.DataSource])
], PhoneAuthProvider);
//# sourceMappingURL=phone-auth.provider.js.map