Debug request/register flows

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-04-06 20:17:30 +01:00
parent 833be34333
commit 9cba7e7399
4 changed files with 20 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 = {

View File

@ -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}
}