blob: 6b1e93da1f15f4ebe1b0c73e809952f92701dcd2 (
plain)
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
|
import { Database } from "bun:sqlite";
const command = process.argv[2];
const dbPath = process.argv[3] ? process.argv[3] : "readit.db";
const db = new Database(dbPath, {
strict: true,
});
if (command === "create") {
createInvite();
} else {
console.log("requires an arg");
}
db.run(`
CREATE TABLE IF NOT EXISTS invites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
usedAt TIMESTAMP
)
`);
function generateInviteToken() {
const hasher = new Bun.CryptoHasher("sha256", "super-secret-invite-key");
return hasher.update(Math.random().toString()).digest("hex").slice(0, 10);
}
function createInvite() {
const token = generateInviteToken();
db.run("INSERT INTO invites (token) VALUES ($token)", { token });
console.log(`Invite token created: ${token}`);
}
|