2023-07-25 01:00:36 +01:00
|
|
|
const logs = require('../utils/logs.utils');
|
|
|
|
const crypt = require('crypt');
|
2023-05-06 21:54:49 +01:00
|
|
|
const db = require('../services/dbAccess.services');
|
|
|
|
const fs = require('fs/promises');
|
|
|
|
const minify = require('../utils/minify.utils');
|
|
|
|
|
|
|
|
// Checks users registration key against issued keys
|
2023-07-24 01:17:00 +01:00
|
|
|
async function isAuthed(uuid: string) { // Needs testing
|
2023-05-06 21:54:49 +01:00
|
|
|
const q = {uuid: uuid};
|
|
|
|
const res = await db.query('users', q);
|
2023-07-25 01:00:36 +01:00
|
|
|
logs.out('authUtils.checkUser: DB Query answer: ' +
|
2023-06-07 21:53:56 +01:00
|
|
|
JSON.stringify(res[0]), 'dbug');
|
2023-05-06 21:54:49 +01:00
|
|
|
const authorized = res && res[0] && res[0].domain;
|
|
|
|
if (authorized) db.userAtime(uuid);
|
|
|
|
return authorized;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether a registration request key is valid
|
2023-07-24 01:17:00 +01:00
|
|
|
async function checkRequest(key: string) {
|
2023-05-06 21:54:49 +01:00
|
|
|
const collection = 'registrations';
|
|
|
|
const query = {uuid: key};
|
|
|
|
const res = await db.query(collection, query);
|
2023-07-25 01:00:36 +01:00
|
|
|
logs.out('authUtils.checkRequest: DB Query result: ' +
|
2023-06-07 21:53:56 +01:00
|
|
|
JSON.stringify(res), 'dbug');
|
2023-05-06 21:54:49 +01:00
|
|
|
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'
|
2023-07-25 01:00:36 +01:00
|
|
|
return crypt.randomUUID();
|
2023-05-06 21:54:49 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 01:17:00 +01:00
|
|
|
async function generateConfirmationEmail(eml: string, uuid: string) {
|
2023-05-06 21:54:49 +01:00
|
|
|
try {
|
|
|
|
const htmlTpl = await fs.readFile('mail-templates/register.html', 'utf-8');
|
|
|
|
const htmlStr = htmlTpl.replace(/>>ACCESSCODE<</g, uuid);
|
|
|
|
const htmlMin = await minify(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) {
|
2023-07-25 01:00:36 +01:00
|
|
|
logs.out('mailServices.generateConfirmationEmail: Error reading template, ' +
|
2023-06-07 21:53:56 +01:00
|
|
|
err, 'err');
|
2023-05-06 21:54:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
isAuthed,
|
|
|
|
generateKey,
|
|
|
|
generateConfirmationEmail,
|
|
|
|
checkRequest
|
2023-07-25 01:00:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
isAuthed,
|
|
|
|
generateKey,
|
|
|
|
generateConfirmationEmail,
|
|
|
|
checkRequest
|
|
|
|
}
|