@brewww/authentication-service
Version:
Authenticator service for Brew projects.
80 lines • 4.25 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 __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.SignUpOtpEmailController = void 0;
const openapi = require("@nestjs/swagger");
const swagger_1 = require("@nestjs/swagger");
const common_1 = require("@nestjs/common");
const otp_service_1 = require("../otp/otp.service");
const user_service_1 = require("../user/user.service");
const token_service_1 = require("../token/token.service");
const error_1 = require("../error");
const nestjs_1 = require("@automapper/nestjs");
const entities_1 = require("../entities");
const dto_1 = require("./dto");
let SignUpOtpEmailController = class SignUpOtpEmailController {
constructor(mapper, otpService, tokenService, userService) {
this.mapper = mapper;
this.otpService = otpService;
this.tokenService = tokenService;
this.userService = userService;
}
async signUpAsync(signUpOtpEmailRequest) {
try {
const haveUser = await this.userService.getUserAsync({
email: signUpOtpEmailRequest.email,
phone: signUpOtpEmailRequest.phone,
});
if (haveUser)
throw new error_1.UserAlreadyExistsError();
const isOtpValid = await this.otpService.validateEmailOtpAsync(signUpOtpEmailRequest.email, signUpOtpEmailRequest.otpValue);
if (!isOtpValid)
throw new error_1.InvalidCredentialsError();
this.otpService.expireOtpAsync({ email: signUpOtpEmailRequest.email });
const userCandidate = await this.mapper.mapAsync(signUpOtpEmailRequest, dto_1.SignUpOtpEmailRequest, entities_1.User);
userCandidate.emailVerified = true;
const user = await this.userService.createUserAsync(userCandidate, signUpOtpEmailRequest.appData);
return await this.tokenService.createTokensAsync(user);
}
catch (error) {
if (error instanceof error_1.InvalidCredentialsError)
throw new common_1.UnauthorizedException(null, { cause: error });
if (error instanceof error_1.UserAlreadyExistsError)
throw new common_1.UnauthorizedException(null, { cause: error });
console.log("sign-up-otp-email", error);
throw new common_1.InternalServerErrorException();
}
}
};
__decorate([
(0, common_1.Post)("sign-up-otp-email"),
openapi.ApiResponse({ status: 201, type: require("./dto/sign-up-response.dto").SignUpResponse }),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [dto_1.SignUpOtpEmailRequest]),
__metadata("design:returntype", Promise)
], SignUpOtpEmailController.prototype, "signUpAsync", null);
SignUpOtpEmailController = __decorate([
(0, swagger_1.ApiTags)("authentication"),
(0, swagger_1.ApiSecurity)("ApiKey"),
(0, common_1.Controller)(),
__param(0, (0, nestjs_1.InjectMapper)()),
__param(2, (0, common_1.Inject)("TokenService")),
__param(3, (0, common_1.Inject)("UserService")),
__metadata("design:paramtypes", [Object, otp_service_1.OtpService,
token_service_1.TokenService,
user_service_1.UserService])
], SignUpOtpEmailController);
exports.SignUpOtpEmailController = SignUpOtpEmailController;
//# sourceMappingURL=sign-up-otp-email.controller.js.map