Organized server imports

This commit is contained in:
Ceferino Patino 2022-08-11 21:06:54 -05:00
commit 96d165c962
4 changed files with 8 additions and 28 deletions

View file

@ -37,9 +37,7 @@ mongoose.connect(process.env.DB_CONN_STRING, {
dbName: process.env.DB_NAME,
});
mongoose.connection.once('open', () =>
console.log('[INFO]: MongoDB connected')
);
mongoose.connection.once('open', () => console.log('[INFO]: MongoDB connected'));
mongoose.connection.on(
'error',
console.error.bind(console, '[ERROR]: MongoDB connection error - ')
@ -90,7 +88,6 @@ function onError(error) {
function onListening() {
const addr = server.address();
const bind =
typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
}

View file

@ -1,15 +1,13 @@
const deriveKeyFromPassword = require('server/utils/auth');
const createRequest = require('server/utils/api');
const config = require('server/config.json');
const User = require('server/db/models/User');
async function basicAuth(req, res, next) {
const authHeader = req.get('Authorization');
const b64Encoded = authHeader.split(' ')[1];
if (!b64Encoded)
return res.status(401).send(config.errorIncomplete);
if (!b64Encoded) return res.status(401).send(config.errorIncomplete);
// Isolate username and password
const [username, password] = Buffer.from(b64Encoded, 'base64').toString().split(':');
@ -20,18 +18,12 @@ async function basicAuth(req, res, next) {
if (cacheUser) {
const { key } = await deriveKeyFromPassword(password, cacheUser.salt);
const checkCacheAuth = await createRequest(
'post',
`/users/${username}/auth`,
{ key }
);
const checkCacheAuth = await createRequest('post', `/users/${username}/auth`, { key });
if (checkCacheAuth.success) {
req.user = cacheUser;
return next();
}
else
return res.status(403).send(config.errorFailed);
} else return res.status(403).send(config.errorFailed);
}
// If login failed with cached salt, refresh user and try again
@ -42,17 +34,12 @@ async function basicAuth(req, res, next) {
const dbUser = await User.create({ ...user, id: user.user_id });
const { key } = await deriveKeyFromPassword(password, user.salt);
const checkAuth = await createRequest(
'post',
`/users/${username}/auth`,
{ key }
);
const checkAuth = await createRequest('post', `/users/${username}/auth`, { key });
if (checkAuth.success) {
req.user = dbUser;
return next();
} else
return res.status(403).send(config.errorFailed);
} else return res.status(403).send(config.errorFailed);
}
module.exports = basicAuth;

View file

@ -16,10 +16,7 @@ async function tokenAuth(req, res, next) {
const date = new Date();
if (!result) {
return res.status(401).send(config.errorUnauthed);
} else if (
result.expires &&
date.setDate(date.getDate + 1) > result.createdAt
) {
} else if (result.expires && date.setDate(date.getDate + 1) > result.createdAt) {
await Token.findOneAndDelete({ token });
return res.status(401).send(config.errorUnauthed);
}

View file

@ -1,6 +1,5 @@
const config = require('server/config.json');
const Mail = require('server/db/models/Mail');
const User = require('server/db/models/User');
const { getAllMail } = require('server/utils/getData');
async function Get(req, res) {