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) { async function register(req, res, next) {
try { try {
let response = await reg.addUser(req.body) let response = await reg.regUser(req.body)
res.status(response.status).json(response) res.status(response.status).json(response)
} catch (err) { } catch (err) {
console.error(`Controller Error`, err.message) 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} let doc = {uuid: uuid, domain: domain, atime: new Date}
await client.connect(); await client.connect();
let col = db.collection("users"); let col = db.collection("users");
res = col.insertOne(doc); res = await col.insertOne(doc);
return res; if (res.insertedId) {
return true
}
return false
} }
async function addRegReq(uuid, domain) { // Needs testing async function addRegReq(uuid, domain) { // Needs testing

View File

@ -29,14 +29,17 @@ async function createRegKey(body){
} }
async function regUser(req) { async function regUser(req) {
let res = auth.checkRequest(req.key) log.out(`registrationServices.regUser: Checking validity of : ${req.uuid}`)
if (res.status) { let res = await auth.checkRequest(req.uuid)
// Run DB Query to get the email domain to store alongside the api_key log.out(`registrationServices.regUser: checkRequest returned: ${JSON.stringify(res)}`)
apiKey = await db.addUser(await util.generateKey(), res.domain) if (res.result) {
return {status: 201, message: "User added", api_key: apiKey} let uuid = await auth.generateKey()
} else { let apiKey = await db.addUser(uuid, res.domain)
return {status: 401, message: "Unautorised"} if (apiKey) {
return {status: 201, message: "User added", api_key: uuid}
} }
}
return {status: 401, message: "Unautorised"}
} }
module.exports = { module.exports = {

View File

@ -6,8 +6,8 @@ const fs = require('fs/promises')
// Checks users registration key against issued keys // Checks users registration key against issued keys
async function isAuthed(uuid) { // Needs testing async function isAuthed(uuid) { // Needs testing
q = {uuid: uuid}; q = {uuid: uuid};
res = db.query("users", q); res = await db.query("users", q);
log.out(`authUtils.checkUser: DB Query answer: ${await res}`) 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 // Do something here to determine if authorised or not and simply return a BOOL
if (res[0].domain) { if (res[0].domain) {
db.userAtime(uuid) db.userAtime(uuid)
@ -21,9 +21,9 @@ async function checkRequest(key) {
let collection = "registrations" let collection = "registrations"
let query = {uuid: key} let query = {uuid: key}
const res = await db.query(collection, query) 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) { if (res[0].time) {
return {result: true, domain: res[0].date} return {result: true, domain: res[0].domain}
} }
return {result: false} return {result: false}
} }