First part of backend rework

- Added the base data structure for the new database
- Added the new routes for the new database
- Reworked the users endpoints
This commit is contained in:
2024-02-26 10:20:29 +01:00
parent 902bab14c7
commit 0ddbc437b9
40 changed files with 1237 additions and 1911 deletions

View File

@@ -1,9 +1,6 @@
/* eslint-disable no-undef */
import nodemailer from 'nodemailer';
import { random } from './random';
import { createPool } from './database';
const pool = createPool('localhost', 'root', '', 'postfixadmin');
const transporter = nodemailer.createTransport({
host: process.env.SMTP,
@@ -15,29 +12,6 @@ const transporter = nodemailer.createTransport({
},
});
async function createAddress(username, domain, password, name = '', backup_email = '', phone = '') {
try {
const hashedPassword = await Bun.password.hash(password, {
type: Bun.password.argon2i,
memoryCost: 2 ** 15,
hashLength: 32,
timeCost: 5,
});
const [result] = await pool.execute(
'INSERT INTO `mailbox` (`username`, `password`, `name`, `maildir`, `quota`, `local_part`, `domain`, `created`, `modified`, `active`, `phone`, `email_other`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[ username, hashedPassword, name, `${domain}/${username}`, 0, username, domain, Date.now(), Date.now(), '1', phone, backup_email],
);
if (result.affectedRows === 0) {
return false;
}
return true;
}
catch (error) {
console.error(error);
return false;
}
}
function sendMail(email, head, body) {
try {
// setup email data
@@ -63,30 +37,29 @@ function sendMail(email, head, body) {
}
}
function sendVerification(email, userId) {
function sendVerification(email, userId, type = 'register', code = null) {
try {
const code = random(100000, 999999);
if (sendMail(email, 'Your verification code for AirJet', `Verification code: ${code}\nLink: https://aostia.me/api/users/verify?u=${userId}&c=${code}`)) {
return code;
}
else {
code ? code : random(100000, 999999);
let title, body;
switch (type) {
case 'email':
title = 'Your verification code for HSP-GDH';
body = `Verification code: ${code}\nLink: ${process.env.DOMAIN}/email/verify?code=${code}`;
break;
case 'password':
title = 'Your password reset code for HSP-GDH';
body = `Verification code: ${code}\nLink: ${process.env.DOMAIN}/password/reset?u=${userId}&c=${code}`;
break;
case '2fa':
title = 'Your 2FA code for HSP-GDH';
body = `Verification code: ${code}`;
break;
default:
return false;
}
}
catch (err) {
if (sendMail(email, title, body)) return code;
return false;
}
}
function sendResetVerification(email, code = random(100000, 999999)) {
try {
if (sendMail(email, 'Your reset verification code for AirJet', `Verification code: ${code}`)) {
return code;
}
else {
return false;
}
}
catch (err) {
return false;
}
@@ -95,6 +68,4 @@ function sendResetVerification(email, code = random(100000, 999999)) {
export {
sendMail,
sendVerification,
sendResetVerification,
createAddress,
};