diff options
Diffstat (limited to 'scripts/gen-invite.js')
-rw-r--r-- | scripts/gen-invite.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/scripts/gen-invite.js b/scripts/gen-invite.js new file mode 100644 index 0000000..0c6a808 --- /dev/null +++ b/scripts/gen-invite.js | |||
@@ -0,0 +1,38 @@ | |||
1 | import { Database } from "bun:sqlite"; | ||
2 | |||
3 | const db = new Database("readit.db", { | ||
4 | strict: true, | ||
5 | }); | ||
6 | |||
7 | // Create the invites table if it doesn't exist | ||
8 | db.run(` | ||
9 | CREATE TABLE IF NOT EXISTS invites ( | ||
10 | id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
11 | token TEXT NOT NULL, | ||
12 | createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
13 | usedAt TIMESTAMP | ||
14 | ) | ||
15 | `); | ||
16 | |||
17 | // Generate a new invite token | ||
18 | function generateInviteToken() { | ||
19 | const hasher = new Bun.CryptoHasher("sha256", "super-secret-invite-key"); | ||
20 | return hasher.update(Math.random().toString()).digest("hex"); | ||
21 | } | ||
22 | |||
23 | // Store the token in the database | ||
24 | function createInvite() { | ||
25 | const token = generateInviteToken(); | ||
26 | db.run("INSERT INTO invites (token) VALUES ($token)", { token }); | ||
27 | console.log(`Invite token created: ${token}`); | ||
28 | } | ||
29 | |||
30 | // CLI usage | ||
31 | const command = process.argv[2]; | ||
32 | const arg = process.argv[3]; | ||
33 | |||
34 | if (command === "create") { | ||
35 | createInvite(); | ||
36 | } else { | ||
37 | console.log("requires an arg"); | ||
38 | } | ||