@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
59 lines (55 loc) • 2.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVerifyEmail = void 0;
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const random_key_1 = require("random-key");
const site_url_1 = __importDefault(require("@cocalc/server/settings/site-url"));
async function getVerifyEmail(email_address) {
const token = await getToken(email_address);
const site_url = await (0, site_url_1.default)();
const url = `${site_url}/auth/verify/${token}?email=${encodeURIComponent(email_address)}`;
return body(url);
}
exports.getVerifyEmail = getVerifyEmail;
async function getToken(email_address) {
const pool = (0, pool_1.default)();
const { rows } = await pool.query("SELECT account_id, email_address_challenge 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 } = rows[0];
if (email_address_challenge?.token &&
email_address_challenge.email == email_address) {
// return the same token if there is one for the same email
return email_address_challenge.token;
}
const token = (0, random_key_1.generate)(16).toLowerCase();
const data = { email: email_address, token, time: new Date() };
await pool.query("UPDATE accounts SET email_address_challenge = $1::JSONB WHERE account_id = $2::UUID", [data, account_id]);
return token;
}
function body(url) {
const html = `
<p style="margin-top:0;margin-bottom:20px;">
<strong>
Please <a href="${url}">verify your email address</a>!
</strong>
</p>
<p style="margin-top:0;margin-bottom:20px;">
If the above link does not work, please copy and paste the following
URL into a new browser tab:
</p>
<pre style="margin-top:10px;margin-bottom:10px;font-size:11px;">
${url}
</pre>
`;
const text = `
Please verify your email address by visiting the following URL in your web browser:
${url}
`;
return { html, text };
}
//# sourceMappingURL=verify.js.map