aboutsummaryrefslogtreecommitdiff
path: root/scripts/gen-invite.js
blob: 0c6a808db3f042fba90157acfcb747d8d1ae711a (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
35
36
37
38
import { Database } from "bun:sqlite";

const db = new Database("readit.db", {
	strict: true,
});

// Create the invites table if it doesn't exist
db.run(`
	CREATE TABLE IF NOT EXISTS invites (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		token TEXT NOT NULL,
		createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
		usedAt TIMESTAMP
	)
`);

// Generate a new invite token
function generateInviteToken() {
	const hasher = new Bun.CryptoHasher("sha256", "super-secret-invite-key");
	return hasher.update(Math.random().toString()).digest("hex");
}

// Store the token in the database
function createInvite() {
	const token = generateInviteToken();
	db.run("INSERT INTO invites (token) VALUES ($token)", { token });
	console.log(`Invite token created: ${token}`);
}

// CLI usage
const command = process.argv[2];
const arg = process.argv[3];

if (command === "create") {
	createInvite();
} else {
	console.log("requires an arg");
}