Add Email Service

This commit is contained in:
Josef 2025-11-11 14:13:39 +01:00
parent d496c6bc70
commit 5833d3d08d
10 changed files with 91 additions and 8 deletions

View File

@ -10,7 +10,7 @@ export class Request
console.log( "post", url, ">>", input); console.log( "post", url, ">>", input);
xhr.open( "POST", url, true ); xhr.open( "POST", url, true );
xhr.setRequestHeader( "Content-Type", "application/json");
xhr.responseType = "text"; xhr.responseType = "text";
xhr.onload= xhr.onload=

View File

@ -150,7 +150,7 @@ export abstract class RequestHandler
getUser():Promise<UserData> getUser():Promise<UserData>
{ {
let request = this._currentRequest; let request = this._currentRequest;
let requestBody = JSON.parse( request.body as string ); let requestBody = request.body;
let tokenData = requestBody as { token:string }; let tokenData = requestBody as { token:string };
let tokenID = tokenData.token; let tokenID = tokenData.token;

View File

@ -10,6 +10,7 @@ export class UserManagementServerSettings
isDebugMode:boolean = false; isDebugMode:boolean = false;
httpsKeyPath:string; httpsKeyPath:string;
httpsCertPath:string; httpsCertPath:string;
emailKey:string;
// Paths // Paths
rootPath:string; rootPath:string;

View File

@ -0,0 +1,42 @@
import { Message } from "../../../browser/messages/Message";
import { RJLog } from "../../log/RJLog";
import { Request } from "../../web/Request";
import { EmailService } from "./EmailService";
export class EmailData
{
to:string;
subject:string;
message:string;
reply:string;
key:string;
}
export class RokojoriEmail extends EmailService
{
_key:string;
constructor( key:string )
{
super();
this._key = key;
}
async send( reply:string, to:string, subject:string, message:string ):Promise<void>
{
let emailData =
{
to: to,
subject: subject,
message: message,
reply: reply,
key: this._key
};
let url = "https://rokojori.com/_php/backend/email.php";
let result = await Request.postJSON<EmailData,Message>( url, emailData );
RJLog.log( "Email", to, subject, ">>", result );
}
}

View File

@ -32,7 +32,7 @@ export class InfoHandler extends RequestHandler
async _handle( request:FastifyRequest, reply:FastifyReply ) async _handle( request:FastifyRequest, reply:FastifyReply )
{ {
let requestBody = JSON.parse( request.body as string ); let requestBody = request.body;
let tokenData = requestBody as { token:string }; let tokenData = requestBody as { token:string };
let tokenID = tokenData.token; let tokenID = tokenData.token;

View File

@ -17,7 +17,7 @@ export class LoginHandler extends RequestHandler
async _handle( request:FastifyRequest, reply:FastifyReply ) async _handle( request:FastifyRequest, reply:FastifyReply )
{ {
let requestBody = JSON.parse( request.body as string ); let requestBody = request.body;
let { email, password, userName } = requestBody as { email: string; password: string; userName: string }; let { email, password, userName } = requestBody as { email: string; password: string; userName: string };
if ( ! email || ! password ) if ( ! email || ! password )

View File

@ -18,7 +18,7 @@ export class LogoutHandler extends RequestHandler
async _handle( request:FastifyRequest, reply:FastifyReply ) async _handle( request:FastifyRequest, reply:FastifyReply )
{ {
let requestBody = JSON.parse( request.body as string ); let requestBody = request.body;
let tokenData = requestBody as { token:string }; let tokenData = requestBody as { token:string };
let tokenID = tokenData.token; let tokenID = tokenData.token;

View File

@ -30,8 +30,7 @@ export class SignUpHandler extends RequestHandler
async _handle( request:FastifyRequest, reply:FastifyReply ) async _handle( request:FastifyRequest, reply:FastifyReply )
{ {
RJLog.log( "Body:", request.body ); let requestBody = request.body;
let requestBody = JSON.parse( request.body as string );
let { email, password, userName } = requestBody as { email: string; password: string; userName: string }; let { email, password, userName } = requestBody as { email: string; password: string; userName: string };

View File

@ -7,7 +7,7 @@ export class UserIsLoggedIn extends RequestRequirement
async handle( request:FastifyRequest, reply:FastifyReply ):Promise<boolean> async handle( request:FastifyRequest, reply:FastifyReply ):Promise<boolean>
{ {
let requestBody = JSON.parse( request.body as string ); let requestBody = request.body;
let tokenData = requestBody as { token:string }; let tokenData = requestBody as { token:string };
if ( ! tokenData ) if ( ! tokenData )

41
node/web/Request.ts Normal file
View File

@ -0,0 +1,41 @@
export class Request
{
static async getJSON<O>(url: string): Promise<O>
{
let result = await fetch( url,
{
method: 'GET',
headers: { 'Accept': 'application/json' }
}
);
if ( ! result.ok )
{
throw new Error( `GET ${url} failed: ${result.status} ${result.statusText}` );
}
return result.json() as Promise<O>;
}
static async postJSON<I, O>(url: string, input: I): Promise<O>
{
let result = await fetch(
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify( input )
});
if ( ! result.ok )
{
throw new Error( `POST ${url} failed: ${result.status} ${result.statusText}` );
}
return result.json() as Promise<O>;
}
}