2023-04-05 11:27:01 +01:00
|
|
|
const log = require('../utils/log.utils');
|
2023-04-04 21:28:46 +01:00
|
|
|
const crypto = require('crypto')
|
2023-04-05 11:27:01 +01:00
|
|
|
const db = require('../services/dbAccess.services');
|
|
|
|
const fs = require('fs/promises')
|
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
|
|
|
|
q = {uuid: uuid};
|
|
|
|
res = db.query("users", q);
|
2023-04-04 21:28:46 +01:00
|
|
|
log.out(`authUtils.checkUser: DB Query answer: ${await res}`)
|
2023-04-05 00:58:48 +01:00
|
|
|
// Do something here to determine if authorised or not and simply return a BOOL
|
2023-04-06 19:42:47 +01:00
|
|
|
if (res[0].domain) {
|
|
|
|
db.userAtime(uuid)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether a registration request key is valid
|
|
|
|
async function checkRequest(key) {
|
|
|
|
let collection = "registrations"
|
|
|
|
let query = {uuid: key}
|
|
|
|
const res = await db.query(collection, query)
|
|
|
|
log.out(`authUtils.checkRequest: DB Query result: ${res}`, "info")
|
|
|
|
if (res[0].time) {
|
|
|
|
return {result: true, domain: res[0].date}
|
|
|
|
}
|
|
|
|
return {result: false}
|
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-04-04 22:12:38 +01:00
|
|
|
return crypto.randomUUID()
|
2023-04-04 21:28:46 +01:00
|
|
|
};
|
|
|
|
|
2023-04-05 11:27:01 +01:00
|
|
|
async function generateConfirmationEmail(eml, uuid) {
|
|
|
|
try {
|
|
|
|
let htmlTpl = fs.readFile('mail-templates/register.html', 'utf-8');
|
|
|
|
let txtTpl = fs.readFile('mail-templates/register.txt', 'utf-8');
|
|
|
|
return msg = {
|
|
|
|
to: eml,
|
|
|
|
subject: "OwlBoard Registration",
|
|
|
|
text: (await txtTpl).replace(/>>ACCESSCODE<</g, uuid),
|
|
|
|
html: (await htmlTpl).replace(/>>ACCESSCODE<</g, uuid)
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
log.out(`mailServices.generateConfirmationEmail: Error reading templates, $(err)`, "err");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 00:58:48 +01:00
|
|
|
module.exports = {
|
|
|
|
isAuthed,
|
2023-04-05 11:27:01 +01:00
|
|
|
generateKey,
|
2023-04-06 19:42:47 +01:00
|
|
|
generateConfirmationEmail,
|
|
|
|
checkRequest
|
2023-04-04 21:28:46 +01:00
|
|
|
}
|