@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
58 lines • 2.46 kB
JavaScript
;
/*
Set or change the password of an account.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const misc_1 = require("@cocalc/util/misc");
const password_hash_1 = __importStar(require("@cocalc/backend/auth/password-hash"));
async function setPassword(account_id, current_password, new_password) {
if (!(0, misc_1.isValidUUID)(account_id)) {
throw Error("account_id is not valid");
}
const pool = (0, pool_1.default)();
const { rows } = await pool.query("SELECT password_hash FROM accounts WHERE account_id=$1", [account_id]);
if (rows.length == 0) {
throw Error("No such account");
}
const { password_hash } = rows[0];
if (password_hash) {
// user had a password set before, so it needs to match
if (!(0, password_hash_1.verifyPassword)(current_password, password_hash)) {
throw Error("Current password is incorrect.");
}
}
// save the hash (only!) of the new password.
await pool.query("UPDATE accounts SET password_hash=$1 WHERE account_id=$2", [
(0, password_hash_1.default)(new_password),
account_id,
]);
}
exports.default = setPassword;
//# sourceMappingURL=set-password.js.map