backend/src/utils/auth.utils.js

57 lines
1.8 KiB
JavaScript
Raw Normal View History

const log = require('../utils/log.utils');
const crypto = require('crypto')
const db = require('../services/dbAccess.services');
const fs = require('fs/promises')
// Checks users registration key against issued keys
async function isAuthed(uuid) { // Needs testing
q = {uuid: uuid};
res = await db.query("users", q);
log.out(`authUtils.checkUser: DB Query answer: ${JSON.stringify(res[0])}`)
// Do something here to determine if authorised or not and simply return a BOOL
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: ${JSON.stringify(res)}`, "info")
if (res[0].time) {
return {result: true, domain: res[0].domain}
}
return {result: false}
}
// Creates an API key for a user
async function generateKey() { // Needs testing & moving to 'register.utils'
return crypto.randomUUID()
};
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;
}
}
module.exports = {
isAuthed,
generateKey,
generateConfirmationEmail,
checkRequest
}