76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
|
|
import { VariableReplacer, Variables } from "../../../../browser/text/replacing/VariableReplacer";
|
||
|
|
import { CryptIO } from "../../../crypt/CryptIO";
|
||
|
|
import { RJLog } from "../../../log/RJLog";
|
||
|
|
import { RequestHandler, RequestType } from "../../RequestHandler";
|
||
|
|
import { FastifyRequest, FastifyReply } from 'fastify';
|
||
|
|
|
||
|
|
export class RequestPasswordChangeHandler extends RequestHandler
|
||
|
|
{
|
||
|
|
static url = "/request-password-change";
|
||
|
|
constructor(){ super( RequestType.POST, RequestPasswordChangeHandler.url ); }
|
||
|
|
|
||
|
|
static USER_NAME = "USER_NAME";
|
||
|
|
static CHANGE_PASSWORD_LINK = "CONFIRMATION_LINK";
|
||
|
|
|
||
|
|
static EMAIL_TITLE = "Password Change Request";
|
||
|
|
static EMAIL_MESSAGE =
|
||
|
|
`
|
||
|
|
<b>Hi {{${RequestPasswordChangeHandler.USER_NAME}}}!</b>
|
||
|
|
<br>
|
||
|
|
You requested to change your password, here is your link:
|
||
|
|
<br>
|
||
|
|
<a href="{{${RequestPasswordChangeHandler.CHANGE_PASSWORD_LINK}}}">CHANGE PASSWORD</a>
|
||
|
|
<br>
|
||
|
|
<br>
|
||
|
|
Cheers!
|
||
|
|
|
||
|
|
`
|
||
|
|
|
||
|
|
async _handle( request:FastifyRequest, reply:FastifyReply )
|
||
|
|
{
|
||
|
|
let requestBody = request.body;
|
||
|
|
let { email } = requestBody as { email:string };
|
||
|
|
|
||
|
|
if ( ! email )
|
||
|
|
{
|
||
|
|
return this.sendError( "Missing email" );
|
||
|
|
}
|
||
|
|
|
||
|
|
let userData = await this.userDB.byEmail( email );
|
||
|
|
|
||
|
|
if ( ! userData )
|
||
|
|
{
|
||
|
|
return this.sendError( "Email not found" );
|
||
|
|
}
|
||
|
|
|
||
|
|
let token = await this.userDB.createPasswordChange( userData, this.ip );
|
||
|
|
|
||
|
|
let changePasswordURL = this._ums._settings.changePasswordURL;
|
||
|
|
|
||
|
|
let id = userData.id;
|
||
|
|
|
||
|
|
let link = `${changePasswordURL}?id=${id}&token=${token.id}`;
|
||
|
|
|
||
|
|
await this.sendPasswordChangeEmail( email, userData.name, link );
|
||
|
|
|
||
|
|
return this.sendInfo( "Send password change email" );
|
||
|
|
}
|
||
|
|
|
||
|
|
async sendPasswordChangeEmail( email:string, userName:string, link:string ):Promise<void>
|
||
|
|
{
|
||
|
|
|
||
|
|
let variables:Variables = {
|
||
|
|
[ RequestPasswordChangeHandler.USER_NAME ] : userName,
|
||
|
|
[ RequestPasswordChangeHandler.CHANGE_PASSWORD_LINK ] : link
|
||
|
|
};
|
||
|
|
|
||
|
|
let message = VariableReplacer.replace( RequestPasswordChangeHandler.EMAIL_MESSAGE, variables, "{{", "}}" );
|
||
|
|
let title = VariableReplacer.replace( RequestPasswordChangeHandler.EMAIL_TITLE, variables, "{{", "}}" );
|
||
|
|
|
||
|
|
await this._ums.sendEmail( email, title, message );
|
||
|
|
|
||
|
|
return Promise.resolve();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|