async'ify avatar and apppassword code

This commit is contained in:
Girish Ramakrishnan
2021-06-25 22:11:17 -07:00
parent 31d742fa67
commit 147c8df6e3
11 changed files with 385 additions and 354 deletions
+29 -31
View File
@@ -13,7 +13,7 @@ exports = module.exports = {
disableTwoFactorAuthentication,
};
var assert = require('assert'),
const assert = require('assert'),
auditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
@@ -35,24 +35,24 @@ function authorize(req, res, next) {
});
}
function get(req, res, next) {
async function get(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.getAvatarUrl(req.user, function (error, avatarUrl) {
if (error) return next(BoxError.toHttpError(error));
const [error, avatarUrl] = await safe(users.getAvatarUrl(req.user));
if (error) return next(BoxError.toHttpError(error));
if (!avatarUrl) return next(new HttpError(404, 'User not found'));
next(new HttpSuccess(200, {
id: req.user.id,
username: req.user.username,
email: req.user.email,
fallbackEmail: req.user.fallbackEmail,
displayName: req.user.displayName,
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
role: req.user.role,
source: req.user.source,
avatarUrl
}));
});
next(new HttpSuccess(200, {
id: req.user.id,
username: req.user.username,
email: req.user.email,
fallbackEmail: req.user.fallbackEmail,
displayName: req.user.displayName,
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
role: req.user.role,
source: req.user.source,
avatarUrl
}));
}
function update(req, res, next) {
@@ -72,7 +72,7 @@ function update(req, res, next) {
});
}
function setAvatar(req, res, next) {
async function setAvatar(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
if (!req.files.avatar) return next(new HttpError(400, 'avatar is missing'));
@@ -80,31 +80,29 @@ function setAvatar(req, res, next) {
const avatar = safe.fs.readFileSync(req.files.avatar.path);
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
users.setAvatar(req.user.id, avatar, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(users.setAvatar(req.user.id, avatar));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
next(new HttpSuccess(202, {}));
}
function clearAvatar(req, res, next) {
async function clearAvatar(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.setAvatar(req.user.id, null, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(users.setAvatar(req.user.id, null));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
next(new HttpSuccess(202, {}));
}
function getAvatar(req, res, next) {
async function getAvatar(req, res, next) {
assert.strictEqual(typeof req.params.identifier, 'string');
users.getAvatar(req.params.identifier, function (error, avatar) {
if (error) return next(BoxError.toHttpError(error));
const [error, avatar] = await safe(users.getAvatar(req.params.identifier));
if (error) return next(BoxError.toHttpError(error));
if (!avatar) return next(new HttpError(404, 'User not found'));
res.send(avatar);
});
res.send(avatar);
}
function changePassword(req, res, next) {