"use strict";
/*
Set email address for an existing account.

You must include both the email address and password.  If there is no password
currently set for the account, you have to set one as part of this request.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const set_email_address_1 = __importDefault(require("@cocalc/server/accounts/set-email-address"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const get_params_1 = __importDefault(require("lib/api/get-params"));
async function handle(req, res) {
    const account_id = await (0, get_account_1.default)(req);
    if (account_id == null) {
        res.json({ error: "must be signed in" });
        return;
    }
    const { email_address, password } = (0, get_params_1.default)(req);
    try {
        await (0, set_email_address_1.default)(account_id, email_address, password);
        res.json({});
    }
    catch (err) {
        if (err.message.includes("duplicate key")) {
            err = Error(`The email address "${email_address}" is already in use by another account.`);
        }
        res.json({ error: err.message });
    }
}
exports.default = handle;
//# sourceMappingURL=set-email-address.js.map