import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import Inject from "@entity-access/entity-access/dist/di/di.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import SocialMailContext from "../../../../../../server/model/SocialMailContext.js"; import CryptoService from "../../../../../../server/services/CryptoService.js"; import EmailAddressService from "../../../../../../server/services/EmailAddressService.js"; import CaptchaService from "../../../../../../server/services/captcha/CaptchaService.js"; import { IVerification } from "../../../../../../server/services/captcha/IVerification.js"; import OtpToUserService from "../../../../../../server/services/user/otp-to-user/OtpToUserService.js"; export interface ISignup { username: string; userID: number; phoneNumber: string; captcha: IVerification; phone: IVerification; password: string; } @Prepare.parseJsonBody export default class extends Page { @Inject captchaService: CaptchaService; @Inject cryptoService: CryptoService; @Inject emailAddressService: EmailAddressService; // @Inject // ois: ExternalOtpInService; @Inject otpService: OtpToUserService; @Inject db: SocialMailContext; async run() { const { db } = this; db.verifyFilters = false; db.raiseEvents = false; const model = this.body as ISignup; model.username = model.username?.trim(); if (!model.username) { throw new EntityAccessError("Username is required"); } const [username, domain] = model.username.split("@"); model.username = username?.trim()?.toLocaleLowerCase()?.replace(/[\.\-]/g, "") + "@" + domain; model.phoneNumber = `+${model.phoneNumber.replace(/[^0-9]/g, "")}`; // console.log(model); // verify captcha... await this.captchaService.verify(model.captcha as any); const user = await db.users.where(model, (x, p) => x.userName === p.username && x.storeID === null) .first(); if (!user) { throw new EntityAccessError(`Username ${model.username} not found, make sure it is complete email address.`); } if (!model.phone.id) { // check if phone exists... const phone = await db.userAuthFactors .where(model, (x, p) => x.user.userName === p.username && x.user.storeID === null && x.phoneNumber === p.phoneNumber ) .first(); if (!phone) { throw new EntityAccessError(`No phone number is associated with this user, please contact us at support@neurospeech.com .`); } if (!model.phone.id) { const v = await this.otpService.create({ phone: model.phoneNumber }); model.phone.id = v.id as any; model.phone.code = v.accessCode; return this.json(model); } } if(model.phone.id) { // verify... const v = await db.verificationTokens.statements.select({} , { verificationID: model.phone.id as any }); if (!v.verified) { return this.json(model); } model.phone.verified = "true"; } if (!model.password) { return this.json(model); } if (model.password.length < 7) { throw new EntityAccessError("Password is cannot be less than 7 characters"); } if (/\s/i.test(model.password)) { throw new EntityAccessError("Password must not any whitespace characters"); } const passwordSalt = this.cryptoService.generateSalt(); const passwordHash = this.cryptoService.hash(passwordSalt, model.password); model.userID = user.userID; await db.users.where(user, (x, p) => x.userID === p.userID) .update({ passwordSalt, passwordHash}, (x, p) => ({ passwordHash: p.passwordHash, passwordSalt: p.passwordSalt })); return this.json(model); } }