diff --git a/src/services/registration.services.js b/src/services/registration.services.js index b390f5d..b43bb96 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -29,9 +29,10 @@ async function createRegKey(body){ } async function regUser(req) { - if (util.checkRequest(req.key) == true) { + let res = auth.checkRequest(req.key) + if (res.status) { // Run DB Query to get the email domain to store alongside the api_key - apiKey = await db.addUser(await util.generateKey()) + apiKey = await db.addUser(await util.generateKey(), res.domain) return {status: 201, message: "User added", api_key: apiKey} } else { return {status: 401, message: "Unautorised"} diff --git a/src/utils/auth.utils.js b/src/utils/auth.utils.js index b88a075..4903b2f 100644 --- a/src/utils/auth.utils.js +++ b/src/utils/auth.utils.js @@ -4,15 +4,28 @@ const db = require('../services/dbAccess.services'); const fs = require('fs/promises') // Checks users registration key against issued keys -async function isAuthed(key) { // Needs testing - return false; - q = {uuid: key}; - res = db.query("registrations", q); +async function isAuthed(uuid) { // Needs testing + q = {uuid: uuid}; + res = db.query("users", q); log.out(`authUtils.checkUser: DB Query answer: ${await res}`) - // Do something here to determine if authorised or not and simply return a BOOL - - return + 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} } // Creates an API key for a user @@ -39,5 +52,6 @@ async function generateConfirmationEmail(eml, uuid) { module.exports = { isAuthed, generateKey, - generateConfirmationEmail + generateConfirmationEmail, + checkRequest } \ No newline at end of file