v2 user routes complete

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-06-20 21:47:32 +01:00
parent eb80d7386e
commit 72444e9fc9
4 changed files with 45 additions and 23 deletions

View File

@ -1,4 +1,5 @@
const statusCodes = { const statusCodes = {
400: 'data not found',
700: 'no authentication attempt', 700: 'no authentication attempt',
701: 'invalid credentials', 701: 'invalid credentials',
702: 'domain not whitelisted', 702: 'domain not whitelisted',
@ -6,7 +7,9 @@ const statusCodes = {
800: 'location code not found', 800: 'location code not found',
801: 'unable to fetch location data', 801: 'unable to fetch location data',
900: 'invalid request format', 900: 'invalid request format',
901: 'email not provided',
950: 'upstream server error', 950: 'upstream server error',
951: 'unknown server error'
}; };
module.exports = statusCodes; module.exports = statusCodes;

View File

@ -23,7 +23,12 @@ async function request(req, res, next) {
async function getUser(req, res, next) { async function getUser(req, res, next) {
try { try {
let uuid = req.params.uuid; let uuid = req.params.uuid;
res.json(await reg.getUser(uuid)); let data = await reg.getUser(uuid);
if (data.status) {
res.status(data.status).json(data);
} else {
res.json(data);
}
} catch (err) { } catch (err) {
console.error(err); console.error(err);
next(err); next(err);

View File

@ -4,6 +4,7 @@ const regCtr = require('../controllers/registration.controllers');
// User // User
router.get('/:uuid', regCtr.getUser); router.get('/:uuid', regCtr.getUser);
// Not Implemented router.delete('/:uuid', regCtr.deleteUser);
router.post('/request', regCtr.request); router.post('/request', regCtr.request);
router.post('/register', regCtr.register); router.post('/register', regCtr.register);

View File

@ -8,25 +8,29 @@ const errors = require('../configs/errorCodes.configs');
async function createRegKey(body) { async function createRegKey(body) {
log.out('registerServices.createRegKey: Incoming request', 'INFO'); log.out('registerServices.createRegKey: Incoming request', 'INFO');
const domain = await clean.getDomainFromEmail(body.email); if (body.email) {
log.out(`registerServices: Registration request from: ${domain}`, 'info'); const domain = await clean.getDomainFromEmail(body.email);
if (domains.includes(domain)) { log.out(`registerServices: Registration request from: ${domain}`, 'info');
log.out(`registerServices.createRegKey: Key from valid domain: ${domain}`); if (domains.includes(domain)) {
const uuid = await auth.generateKey(); log.out(`registerServices.createRegKey: Key from valid: ${domain}`,'info');
db.addRegReq(uuid, domain); const uuid = await auth.generateKey();
const message = await auth.generateConfirmationEmail(body.email, uuid); db.addRegReq(uuid, domain);
if (!message) { const message = await auth.generateConfirmationEmail(body.email, uuid);
const err = new Error('Message generation error'); if (!message) {
log.out('registerServices.createRegKey: Error generating email', 'err'); const err = new Error('Message generation error');
log.out(err, 'err'); log.out('registerServices.createRegKey: Error generating email', 'err');
return 500; log.out(err, 'err');
return 500;
}
if (await mail.send(message) == true) {
return {status: 201, message: 'email sent'};
}
return {status: 500, errorCode: 950, errorMsg: errors[950]};
} }
if (await mail.send(message) == true) { return {status: 403, errorCode: 702, errorMsg: errors[702]};
return {status: 201, message: 'email sent'}; } else {
} return {status: 400, errorCode: 901, errorMsg: errors[902]};
return {status: 500, errorCode: 950, errorMsg: errors[950]};
} }
return {status: 403, errorCode: 702, errorMsg: errors[702]};
} }
async function regUser(req) { // Add input validation async function regUser(req) { // Add input validation
@ -47,11 +51,20 @@ async function regUser(req) { // Add input validation
async function getUser(uuid) { async function getUser(uuid) {
log.out('registrationServices: Finding user for given UUID', 'dbug'); log.out('registrationServices: Finding user for given UUID', 'dbug');
const filter = { try {
uuid: uuid const filter = {
}; uuid: uuid
const res = await db.query('users', filter, false); };
return res; const res = await db.query('users', filter, false);
if (res.length) {
return res;
} else {
return {status: 404, errorCode:400, errorMsg: errors[400]};
}
} catch (err) {
console.log(err);
return {status: 500, errorCode: 951, errorMsg: errors[951]};
}
} }
module.exports = { module.exports = {