backend/src/services/registration.services.js

51 lines
2.0 KiB
JavaScript
Raw Normal View History

const log = require('../utils/log.utils');
const auth = require('../utils/auth.utils');
const db = require('./dbAccess.services');
const mail = require('./mail.services');
const clean = require('../utils/sanitizer.utils');
const domains = require('../configs/domains.configs');
const errors = require('../configs/errorCodes.configs');
async function createRegKey(body) {
log.out('registerServices.createRegKey: Incoming request', 'INFO');
const domain = await clean.getDomainFromEmail(body.email); // The function should validate the email
log.out(`registerServices: New registration request from domain: ${domain}`, 'info');
if (domains.includes(domain)) {
log.out(`registerServices.createRegKey: Key from valid domain: ${domain}`);
const uuid = await auth.generateKey();
db.addRegReq(uuid, domain);
const message = await auth.generateConfirmationEmail(body.email, uuid);
if (!message) {
const err = new Error('Message generation error');
log.out('registerServices.createRegKey: Error generating registration email', 'err');
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]};
}
return {status: 403, errorCode: 702, errorMsg: errors[702]};
}
async function regUser(req) { // Add input validation
log.out(`Read UUID: ${req.uuid}`, 'dbug');
log.out(`registrationServices.regUser: Checking validity of : ${req.uuid}`, 'info');
const res = await auth.checkRequest(req.uuid);
log.out(`registrationServices.regUser: checkRequest returned: ${JSON.stringify(res)}`, 'info');
if (res.result) {
const uuid = await auth.generateKey();
const apiKey = await db.addUser(uuid, res.domain);
if (apiKey) {
db.delRegReq(req.uuid);
return {status: 201, message: 'User added', api_key: uuid};
}
}
return {status: 401, errorCode: 703, errorMsg: errors[703]};
}
module.exports = {
regUser,
createRegKey
};