@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
49 lines • 2.27 kB
JavaScript
;
/*
"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(require("@cocalc/database/pool"));
const misc_1 = require("@cocalc/util/misc");
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