"use strict";
(() => {
var exports = {};
exports.id = 7004;
exports.ids = [7004];
exports.modules = {

/***/ 439491:
/***/ ((module) => {

module.exports = require("assert");

/***/ }),

/***/ 714300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 706113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 809523:
/***/ ((module) => {

module.exports = require("dns");

/***/ }),

/***/ 582361:
/***/ ((module) => {

module.exports = require("events");

/***/ }),

/***/ 657147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 241808:
/***/ ((module) => {

module.exports = require("net");

/***/ }),

/***/ 822037:
/***/ ((module) => {

module.exports = require("os");

/***/ }),

/***/ 371017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 677282:
/***/ ((module) => {

module.exports = require("process");

/***/ }),

/***/ 12781:
/***/ ((module) => {

module.exports = require("stream");

/***/ }),

/***/ 371576:
/***/ ((module) => {

module.exports = require("string_decoder");

/***/ }),

/***/ 924404:
/***/ ((module) => {

module.exports = require("tls");

/***/ }),

/***/ 76224:
/***/ ((module) => {

module.exports = require("tty");

/***/ }),

/***/ 257310:
/***/ ((module) => {

module.exports = require("url");

/***/ }),

/***/ 473837:
/***/ ((module) => {

module.exports = require("util");

/***/ }),

/***/ 26438:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Z": () => (/* binding */ getParams)
/* harmony export */ });
function getParams(req) {
    if (req?.method == "POST") {
        return new Proxy({}, {
            get (_, key) {
                return req.body?.[key];
            }
        });
    /*
    // Disabled, since this could lead to a sneaky click on a link attack.
    // Should only be enabled for dev purposes.
    } else if (req?.method == "GET") {
    return new Proxy(
      {},
      {
        get(_, key) {
          if (typeof key != "string") {
            return undefined;
          }
          return req.query?.[key];
        },
      }
    );
*/ } else {
        // only support params for POST requests.
        return {};
    }
}


/***/ }),

/***/ 75177:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ redeemVerifyEmailAPICall)
/* harmony export */ });
/* harmony import */ var _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(35012);
/* harmony import */ var _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lib_api_get_params__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(26438);
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */ 

async function redeemVerifyEmailAPICall(req, res) {
    const { email_address , token  } = (0,lib_api_get_params__WEBPACK_IMPORTED_MODULE_1__/* ["default"] */ .Z)(req);
    try {
        await _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0___default()(email_address, token);
    } catch (err) {
        res.json({
            error: `${err.message}`
        });
        return;
    }
    res.json({});
    return;
}


/***/ }),

/***/ 35012:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


/*
"Redeem" an email verification token.  This checks everything is valid, then sets that the email
has been verified.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isEmailVerified = void 0;
const pool_1 = __importDefault(__webpack_require__(541808));
const misc_1 = __webpack_require__(854006);
async function redeemVerifyEmail(email_address, token) {
    if (token.length < 16) {
        throw Error("token is too short");
    }
    if (!(0, misc_1.is_valid_email_address)(email_address)) {
        throw Error("email_address is not valid");
    }
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT account_id, email_address_challenge, email_address_verified FROM accounts WHERE email_address=$1", [email_address]);
    if (rows.length == 0) {
        throw Error(`no account with email address ${email_address}`);
    }
    const { account_id, email_address_challenge, email_address_verified } = rows[0];
    if (!email_address_challenge?.email) {
        if (email_address_verified?.[email_address]) {
            // nothing to do.
            return;
        }
        throw Error(`no email verification configured for ${email_address}`);
    }
    if (email_address_challenge.token != token) {
        throw Error("tokens do not match");
    }
    // we're good, save this in the email_address_verified JSONB record and also delete the challenge
    await pool.query("UPDATE accounts SET email_address_challenge=NULL, email_address_verified=$1::JSONB WHERE account_id=$2", [{ ...email_address_verified, [email_address]: new Date() }, account_id]);
}
exports["default"] = redeemVerifyEmail;
async function isEmailVerified(email_address) {
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT email_address_verified FROM accounts WHERE email_address=$1::TEXT", [email_address]);
    if (rows.length == 0)
        return false;
    const { email_address_verified } = rows[0];
    return !!email_address_verified[email_address];
}
exports.isEmailVerified = isEmailVerified;
//# sourceMappingURL=redeem-verify-email.js.map

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [8180,2921,1808,4006], () => (__webpack_exec__(75177)));
module.exports = __webpack_exports__;

})();