1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ValTupleStorage } from "https://esm.town/v/chet/ValTupleStorage";
const db = new ValTupleStorage("email");
export async function generateEmailKey() {
const key = Math.random().toString().slice(3);
const SecondMs = 1000;
const MinuteMs = 60 * SecondMs;
const HourMs = 60 * MinuteMs;
const DayMs = 24 * HourMs;
const expires = Date.now() + 2 * 365 * DayMs;
await db.write({ set: [{ key: [key], value: expires }] });
console.log("Generated a new key", key);
}
export async function listEmailKeys() {
const emailKeys = await db.scan();
console.log("Email keys:", emailKeys);
}
export async function expireEmailKey(key: string) {
const expires = Date.now() - 1;
await db.write({ set: [{ key: [key], value: expires }] });
}
export async function authenticateEmailKey(key: string) {
const result = await db.scan({ gte: [key], lte: [key], limit: 1 });
if (result.length === 0) return "Invalid key.";
const expires = result[0].value;
if (Date.now() >= expires) return "Key has expired.";
}
export async function resetEmailKeys() {
await db.destroy();
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.