bb-inspired
Version:
Core library for BB-inspired NestJS backend
63 lines • 3.5 kB
JavaScript
;
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 CredentialsValidationPipe_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialsValidationPipe = void 0;
const common_1 = require("@nestjs/common");
const logger_1 = require("../../../utils/logger");
let CredentialsValidationPipe = CredentialsValidationPipe_1 = class CredentialsValidationPipe {
constructor() {
this.logger = new logger_1.AppLogger(CredentialsValidationPipe_1.name);
}
transform(value) {
if (!value || typeof value !== 'object') {
this.logger.warn('Invalid credentials format: not an object');
throw new common_1.BadRequestException('Invalid credentials format');
}
const { username, password, tenantId, rememberMe } = value;
if (!username || typeof username !== 'string') {
this.logger.warn('Invalid credentials: missing or invalid username');
throw new common_1.BadRequestException('Username is required and must be a string');
}
if (!password || typeof password !== 'string') {
this.logger.warn('Invalid credentials: missing or invalid password');
throw new common_1.BadRequestException('Password is required and must be a string');
}
const isValidUsername = /^[a-zA-Z0-9_-]{3,30}$/.test(username);
const isValidEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(username);
if (!isValidUsername && !isValidEmail) {
this.logger.warn('Invalid credentials: username format invalid');
throw new common_1.BadRequestException('Invalid username or email format');
}
if (password.length < 6) {
this.logger.warn('Invalid credentials: password too short');
throw new common_1.BadRequestException('Password must be at least 6 characters long');
}
if (tenantId !== undefined) {
if (typeof tenantId !== 'string' && typeof tenantId !== 'number') {
this.logger.warn('Invalid credentials: invalid tenant ID format');
throw new common_1.BadRequestException('Tenant ID must be a string or number');
}
}
if (rememberMe !== undefined && typeof rememberMe !== 'boolean') {
this.logger.warn('Invalid credentials: invalid rememberMe format');
throw new common_1.BadRequestException('Remember me flag must be a boolean');
}
return {
username,
password,
...(tenantId !== undefined && { tenantId }),
...(rememberMe !== undefined && { rememberMe }),
};
}
};
exports.CredentialsValidationPipe = CredentialsValidationPipe;
exports.CredentialsValidationPipe = CredentialsValidationPipe = CredentialsValidationPipe_1 = __decorate([
(0, common_1.Injectable)()
], CredentialsValidationPipe);
//# sourceMappingURL=credentials-validation.pipe.js.map