From 9cba7e73991c7a170c6c6030639d3064cdeab92f Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 6 Apr 2023 20:17:30 +0100 Subject: [PATCH] Debug request/register flows Signed-off-by: Fred Boniface --- src/controllers/registration.controllers.js | 2 +- src/services/dbAccess.services.js | 7 +++++-- src/services/registration.services.js | 17 ++++++++++------- src/utils/auth.utils.js | 8 ++++---- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/controllers/registration.controllers.js b/src/controllers/registration.controllers.js index f81706e..9253c3f 100644 --- a/src/controllers/registration.controllers.js +++ b/src/controllers/registration.controllers.js @@ -2,7 +2,7 @@ const reg = require('../services/registration.services'); async function register(req, res, next) { try { - let response = await reg.addUser(req.body) + let response = await reg.regUser(req.body) res.status(response.status).json(response) } catch (err) { console.error(`Controller Error`, err.message) diff --git a/src/services/dbAccess.services.js b/src/services/dbAccess.services.js index c8d6ce8..8dfbe19 100644 --- a/src/services/dbAccess.services.js +++ b/src/services/dbAccess.services.js @@ -38,8 +38,11 @@ async function addUser(uuid, domain) { // Needs testing let doc = {uuid: uuid, domain: domain, atime: new Date} await client.connect(); let col = db.collection("users"); - res = col.insertOne(doc); - return res; + res = await col.insertOne(doc); + if (res.insertedId) { + return true + } + return false } async function addRegReq(uuid, domain) { // Needs testing diff --git a/src/services/registration.services.js b/src/services/registration.services.js index b43bb96..f712083 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -29,14 +29,17 @@ async function createRegKey(body){ } async function regUser(req) { - 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(), res.domain) - return {status: 201, message: "User added", api_key: apiKey} - } else { - return {status: 401, message: "Unautorised"} + log.out(`registrationServices.regUser: Checking validity of : ${req.uuid}`) + let res = await auth.checkRequest(req.uuid) + log.out(`registrationServices.regUser: checkRequest returned: ${JSON.stringify(res)}`) + if (res.result) { + let uuid = await auth.generateKey() + let apiKey = await db.addUser(uuid, res.domain) + if (apiKey) { + return {status: 201, message: "User added", api_key: uuid} + } } + return {status: 401, message: "Unautorised"} } module.exports = { diff --git a/src/utils/auth.utils.js b/src/utils/auth.utils.js index 4903b2f..d9eeb5e 100644 --- a/src/utils/auth.utils.js +++ b/src/utils/auth.utils.js @@ -6,8 +6,8 @@ const fs = require('fs/promises') // Checks users registration key against issued keys async function isAuthed(uuid) { // Needs testing q = {uuid: uuid}; - res = db.query("users", q); - log.out(`authUtils.checkUser: DB Query answer: ${await res}`) + 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) @@ -21,9 +21,9 @@ 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") + log.out(`authUtils.checkRequest: DB Query result: ${JSON.stringify(res)}`, "info") if (res[0].time) { - return {result: true, domain: res[0].date} + return {result: true, domain: res[0].domain} } return {result: false} }