More work on reg services

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-04-06 19:42:47 +01:00
parent 214658443d
commit 833be34333
2 changed files with 25 additions and 10 deletions

View File

@ -29,9 +29,10 @@ async function createRegKey(body){
} }
async function regUser(req) { 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 // 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} return {status: 201, message: "User added", api_key: apiKey}
} else { } else {
return {status: 401, message: "Unautorised"} return {status: 401, message: "Unautorised"}

View File

@ -4,15 +4,28 @@ const db = require('../services/dbAccess.services');
const fs = require('fs/promises') const fs = require('fs/promises')
// Checks users registration key against issued keys // Checks users registration key against issued keys
async function isAuthed(key) { // Needs testing async function isAuthed(uuid) { // Needs testing
return false; q = {uuid: uuid};
q = {uuid: key}; res = db.query("users", q);
res = db.query("registrations", q);
log.out(`authUtils.checkUser: DB Query answer: ${await res}`) log.out(`authUtils.checkUser: DB Query answer: ${await res}`)
// 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) {
return 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 // Creates an API key for a user
@ -39,5 +52,6 @@ async function generateConfirmationEmail(eml, uuid) {
module.exports = { module.exports = {
isAuthed, isAuthed,
generateKey, generateKey,
generateConfirmationEmail generateConfirmationEmail,
checkRequest
} }