Organized server imports
This commit is contained in:
parent
68c8f96ca5
commit
96d165c962
4 changed files with 8 additions and 28 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Reference in a new issue