2023-05-05 01:01:09 +01:00
|
|
|
const log = require('../utils/log.utils');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const db = require('../services/dbAccess.services');
|
|
|
|
const fs = require('fs/promises');
|
|
|
|
const minify = require('../utils/minify.utils');
|
2023-04-04 21:28:46 +01:00
|
|
|
|
|
|
|
// Checks users registration key against issued keys
|
2023-04-06 19:42:47 +01:00
|
|
|
async function isAuthed(uuid) { // Needs testing
|
2023-05-05 01:01:09 +01:00
|
|
|
const q = {uuid: uuid};
|
|
|
|
const res = await db.query('users', q);
|
|
|
|
log.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;
|
2023-04-06 19:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether a registration request key is valid
|
|
|
|
async function checkRequest(key) {
|
2023-05-05 01:01:09 +01:00
|
|
|
const collection = 'registrations';
|
|
|
|
const query = {uuid: key};
|
|
|
|
const res = await db.query(collection, query);
|
|
|
|
log.out(`authUtils.checkRequest: DB Query result: ${JSON.stringify(res)}`, 'dbug');
|
2023-04-30 22:07:36 +01:00
|
|
|
const result = res.length > 0 && res[0].time
|
|
|
|
? { result: true, domain: res[0].domain }
|
2023-05-05 01:01:09 +01:00
|
|
|
: { result: false };
|
|
|
|
return result;
|
2023-04-04 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an API key for a user
|
2023-04-05 01:09:50 +01:00
|
|
|
async function generateKey() { // Needs testing & moving to 'register.utils'
|
2023-05-05 01:01:09 +01:00
|
|
|
return crypto.randomUUID();
|
2023-04-30 22:07:36 +01:00
|
|
|
}
|
2023-04-04 21:28:46 +01:00
|
|
|
|
2023-04-05 11:27:01 +01:00
|
|
|
async function generateConfirmationEmail(eml, uuid) {
|
2023-04-30 22:07:36 +01:00
|
|
|
try {
|
2023-05-05 01:01:09 +01:00
|
|
|
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');
|
2023-05-02 22:49:51 +01:00
|
|
|
return {
|
2023-04-30 22:07:36 +01:00
|
|
|
to: eml,
|
|
|
|
subject: 'OwlBoard Registration',
|
|
|
|
text: (await txtTpl).replace(/>>ACCESSCODE<</g, uuid),
|
2023-05-02 22:49:51 +01:00
|
|
|
html: htmlMin
|
2023-05-05 01:01:09 +01:00
|
|
|
};
|
2023-04-30 22:07:36 +01:00
|
|
|
} catch(err) {
|
2023-05-05 01:01:09 +01:00
|
|
|
log.out('mailServices.generateConfirmationEmail: Error reading templates, $(err)', 'err');
|
|
|
|
return false;
|
2023-04-30 22:07:36 +01:00
|
|
|
}
|
2023-04-05 11:27:01 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 00:58:48 +01:00
|
|
|
module.exports = {
|
2023-04-30 22:07:36 +01:00
|
|
|
isAuthed,
|
|
|
|
generateKey,
|
|
|
|
generateConfirmationEmail,
|
|
|
|
checkRequest
|
2023-05-05 01:01:09 +01:00
|
|
|
};
|