@accounts/rest-express
Version:
Server side REST express middleware for accounts
62 lines • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.twoFactorUnset = exports.twoFactorSet = exports.twoFactorSecret = void 0;
const send_error_1 = require("../../utils/send-error");
const express_validator_1 = require("express-validator");
const matchOrTrow_1 = require("../../utils/matchOrTrow");
const twoFactorSecret = (accountsServer) => async (req, res) => {
try {
const accountsPassword = accountsServer.getServices().password;
const secret = accountsPassword.twoFactor.getNewAuthSecret();
res.json({ secret });
}
catch (err) {
(0, send_error_1.sendError)(res, err);
}
};
exports.twoFactorSecret = twoFactorSecret;
const twoFactorSet = (accountsServer) => [
(0, express_validator_1.body)('secret').isObject().notEmpty(),
(0, express_validator_1.body)('secret.base32').isString().notEmpty(),
(0, express_validator_1.body)('code').isString().notEmpty(),
async (req, res) => {
try {
const userId = req.userId;
if (!userId) {
res.status(401);
res.json({ message: 'Unauthorized' });
return;
}
const accountsPassword = accountsServer.getServices().password;
const { secret, code } = (0, matchOrTrow_1.matchOrThrow)(req);
await accountsPassword.twoFactor.set(userId, secret, code);
res.json({});
}
catch (err) {
(0, send_error_1.sendError)(res, err);
}
},
];
exports.twoFactorSet = twoFactorSet;
const twoFactorUnset = (accountsServer) => [
(0, express_validator_1.body)('code').isString().notEmpty(),
async (req, res) => {
try {
const userId = req.userId;
if (!userId) {
res.status(401);
res.json({ message: 'Unauthorized' });
return;
}
const { code } = (0, matchOrTrow_1.matchOrThrow)(req);
const accountsPassword = accountsServer.getServices().password;
await accountsPassword.twoFactor.unset(userId, code);
res.json({});
}
catch (err) {
(0, send_error_1.sendError)(res, err);
}
},
];
exports.twoFactorUnset = twoFactorUnset;
//# sourceMappingURL=two-factor.js.map