76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
const logs = require("../utils/logs.utils");
|
|
const crypt = require("crypto");
|
|
const db = require("../services/dbAccess.services");
|
|
const fs = require("fs/promises");
|
|
|
|
import { minifyMail } from "./minify.utils";
|
|
|
|
// Checks users registration key against issued keys
|
|
async function isAuthed(uuid: string): Promise<boolean> {
|
|
// Needs testing
|
|
const q = {
|
|
uuid: uuid
|
|
};
|
|
const res = await db.query("users", q);
|
|
logs.out(
|
|
"authUtils.checkUser: DB Query answer: " + JSON.stringify(res[0]),
|
|
"dbug"
|
|
);
|
|
const authorized = res && res[0] && res[0].domain;
|
|
if (authorized) db.userAtime(uuid);
|
|
return authorized;
|
|
}
|
|
|
|
// Checks whether a registration request key is valid
|
|
async function checkRequest(key: string) { // For some reason db.query seems to return correctly, but the second logs.out statement prints []??? so registration fails!!
|
|
const collection = "registrations";
|
|
const query = { uuid: key };
|
|
const res = await db.query(collection, query);
|
|
logs.out("authUtils.checkRequest: Raw Result: " + res, "dbug")
|
|
logs.out(
|
|
"authUtils.checkRequest: DB Query result: " + JSON.stringify(res),
|
|
"dbug"
|
|
);
|
|
const result =
|
|
res.length > 0 && res[0].time
|
|
? { result: true, domain: res[0].domain }
|
|
: { result: false };
|
|
return result;
|
|
}
|
|
|
|
// Creates an API key for a user
|
|
async function generateKey() {
|
|
// Needs testing & moving to 'register.utils' ??? Why does it need moving?
|
|
return crypt.randomUUID();
|
|
}
|
|
|
|
async function generateConfirmationEmail(eml: string, uuid: string) {
|
|
try {
|
|
const htmlTpl = await fs.readFile("mail-templates/register.html", "utf-8");
|
|
const htmlStr = htmlTpl.replace(/>>ACCESSCODE<</g, uuid);
|
|
const htmlMin = await minifyMail(htmlStr);
|
|
const txtTpl = fs.readFile("mail-templates/register.txt", "utf-8");
|
|
return {
|
|
to: eml,
|
|
subject: "OwlBoard Registration",
|
|
text: (await txtTpl).replace(/>>ACCESSCODE<</g, uuid),
|
|
html: htmlMin,
|
|
};
|
|
} catch (err) {
|
|
logs.out(
|
|
"mailServices.generateConfirmationEmail: Error reading template, " + err,
|
|
"err"
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
isAuthed,
|
|
generateKey,
|
|
generateConfirmationEmail,
|
|
checkRequest,
|
|
};
|
|
|
|
export { isAuthed, generateKey, generateConfirmationEmail, checkRequest };
|