import { RegExpUtility } from "../../../browser/text/RegExpUtitlity"; import { CryptIO } from "../../crypt/CryptIO"; import { RJLog } from "../../log/RJLog"; import { RequestHandler, RequestType } from "../RequestHandler"; import { FastifyRequest, FastifyReply } from 'fastify'; import { VariableReplacer, Variables } from "../../../browser/text/replacing/VariableReplacer"; import { ConfirmSignUpHandler } from "./confirm-signup"; export class SignUpHandler extends RequestHandler { static url = "/signup"; constructor(){ super( RequestType.POST, SignUpHandler.url ); } static USER_NAME = "USER_NAME"; static CONFIRMATION_LINK = "CONFIRMATION_LINK"; static EMAIL_TITLE = "You signed up"; static EMAIL_MESSAGE = ` Hi {{${SignUpHandler.USER_NAME}}}!
Please confirm your signup by clicking the link:
SIGN UP CONFIRMATION

Cheers! ` async _handle( request:FastifyRequest, reply:FastifyReply ) { let requestBody = request.body; let { email, password, userName } = requestBody as { email: string; password: string; userName: string }; if ( ! email || ! password ) { return this.sendError( "Missing email or password:" + `"${requestBody}"` ); } let emailInUse = await this.userDB.hasUserWithEmail( email ); if ( emailInUse ) { return this.sendError( "User already exists" ); } let userData = await this.userDB.byEmail( email ); if ( userData == null ) { userData = await this.userDB.signUp( email, password, userName ); } let token = await this.userDB.createSignUpConfirmation( userData, this.ip ); let confirmationURL = this._ums._settings.url + ConfirmSignUpHandler.url; let id = userData.id; let link = `${confirmationURL}?id=${id}&token=${token.id}`; await this.sendEmail( email, userName, link ); return this.sendInfo( "User created, email sent" ); } async sendEmail( email:string, userName:string, link:string ):Promise { let variables:Variables = { [ SignUpHandler.USER_NAME ] : userName, [ SignUpHandler.CONFIRMATION_LINK ] : link }; let message = VariableReplacer.replace( SignUpHandler.EMAIL_MESSAGE, variables, "{{", "}}" ); let title = VariableReplacer.replace( SignUpHandler.EMAIL_TITLE, variables, "{{", "}}" ); await this._ums.sendEmail( email,title, message ); return Promise.resolve(); } }