Fixed API issues and added a test file
This commit is contained in:
@@ -1,44 +1,45 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import { random } from './random';
|
||||
import { log } from './logManager';
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP,
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env.MAIL,
|
||||
pass: process.env.MAIL_PASS,
|
||||
user: `${process.env.MAIL}`,
|
||||
pass: `${process.env.PASS}`,
|
||||
},
|
||||
tls: { rejectUnauthorized: false },
|
||||
});
|
||||
|
||||
function sendMail(email, head, body) {
|
||||
try {
|
||||
// setup email data
|
||||
const mailOptions = {
|
||||
from: `"AirJet" <${process.env.MAIL}>`,
|
||||
from: `"HSP-GDH" <${process.env.MAIL}>`,
|
||||
to: email,
|
||||
subject: head,
|
||||
text: body,
|
||||
};
|
||||
// send mail with defined transport object
|
||||
transporter.sendMail(mailOptions, (error, info) => {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
log('Retrying connection to SMTP');
|
||||
if (sendMail(email, head, body)) return true;
|
||||
}
|
||||
else {
|
||||
console.log('Email sent: ' + info.response);
|
||||
log('Email sent: ' + info.response);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function sendVerification(email, userId, type = 'register', code = null) {
|
||||
async function sendVerification(email, userId, type = 'register', code = null) {
|
||||
try {
|
||||
code ? code : random(100000, 999999);
|
||||
code ? code : code = random(100000, 999999);
|
||||
let title, body;
|
||||
switch (type) {
|
||||
case 'email':
|
||||
@@ -56,8 +57,9 @@ function sendVerification(email, userId, type = 'register', code = null) {
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (sendMail(email, title, body)) return code;
|
||||
return false;
|
||||
await sendMail(email, title, body);
|
||||
return code;
|
||||
|
||||
}
|
||||
catch (err) {
|
||||
return false;
|
||||
|
||||
@@ -18,7 +18,7 @@ export async function userExists(userId) {
|
||||
export async function isBanned(userId) {
|
||||
try {
|
||||
const [bannedUser] = await pool.execute('SELECT * FROM bans WHERE user_id = ? LIMIT 1', [userId]);
|
||||
if (bannedUser.length > 0) return true;
|
||||
if (bannedUser.length) return true;
|
||||
return false;
|
||||
}
|
||||
catch (err) {
|
||||
@@ -60,24 +60,24 @@ export async function checkIfUserEmailIsVerified(userId) {
|
||||
|
||||
export async function checkUserExists(req, res, next) {
|
||||
const userId = req.userId;
|
||||
if (!userExists(userId)) return await respondWithStatus(res, 404, 'User not found');
|
||||
if (!await userExists(userId)) return await respondWithStatus(res, 404, 'User not found');
|
||||
next();
|
||||
}
|
||||
|
||||
export async function checkBanned(req, res, next) {
|
||||
const userId = req.userId;
|
||||
if (isBanned(userId)) return await respondWithStatus(res, 403, 'User is banned');
|
||||
if (await isBanned(userId)) return await respondWithStatus(res, 403, 'User is banned');
|
||||
next();
|
||||
}
|
||||
|
||||
export const checkPermissions = (permissionName, permissionType) => async (req, res, next) => {
|
||||
const userId = req.userId;
|
||||
if (!verifyPermissions(userId, permissionName, permissionType)) return await respondWithStatus(res, 403, 'Missing permission');
|
||||
if (!await verifyPermissions(userId, permissionName, permissionType)) return await respondWithStatus(res, 403, 'Missing permission');
|
||||
next();
|
||||
};
|
||||
|
||||
export const checkEmailVerified = async (req, res, next) => {
|
||||
const userId = req.userId;
|
||||
if (!checkIfUserEmailIsVerified(userId)) return await respondWithStatus(res, 403, 'Email not verified');
|
||||
if (!await checkIfUserEmailIsVerified(userId)) return await respondWithStatus(res, 403, 'Email not verified');
|
||||
next();
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Level } from 'level';
|
||||
import { pool } from './databaseManager';
|
||||
import { error } from '../modules/logManager';
|
||||
import { respondWithStatus } from './requestHandler';
|
||||
|
||||
const db = new Level('tokens', { valueEncoding: 'json' });
|
||||
@@ -31,7 +32,8 @@ export async function verifyToken(req, res, next) {
|
||||
}
|
||||
next();
|
||||
}
|
||||
catch (error) {
|
||||
catch (err) {
|
||||
error(err);
|
||||
return await respondWithStatus(res, 401, 'Invalid user');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user