diff options
author | Akshay <[email protected]> | 2024-11-15 21:25:27 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2024-11-15 21:25:27 +0000 |
commit | c5cd65eba355e66589298cbe88fe3489e0fcebae (patch) | |
tree | 4050c946c4611e88c51efbb61cc44903fca5fd82 | |
parent | d4a83cb44dc98fe78f9061408137a43049344b1d (diff) |
invalidate registered tokens properly
-rw-r--r-- | scripts/gen-invite.js | 4 | ||||
-rw-r--r-- | src/invite.js | 2 | ||||
-rw-r--r-- | src/public/styles.css | 1 | ||||
-rw-r--r-- | src/routes/index.js | 6 |
4 files changed, 5 insertions, 8 deletions
diff --git a/scripts/gen-invite.js b/scripts/gen-invite.js index 0c6a808..3336a83 100644 --- a/scripts/gen-invite.js +++ b/scripts/gen-invite.js | |||
@@ -4,7 +4,6 @@ const db = new Database("readit.db", { | |||
4 | strict: true, | 4 | strict: true, |
5 | }); | 5 | }); |
6 | 6 | ||
7 | // Create the invites table if it doesn't exist | ||
8 | db.run(` | 7 | db.run(` |
9 | CREATE TABLE IF NOT EXISTS invites ( | 8 | CREATE TABLE IF NOT EXISTS invites ( |
10 | id INTEGER PRIMARY KEY AUTOINCREMENT, | 9 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
@@ -14,20 +13,17 @@ db.run(` | |||
14 | ) | 13 | ) |
15 | `); | 14 | `); |
16 | 15 | ||
17 | // Generate a new invite token | ||
18 | function generateInviteToken() { | 16 | function generateInviteToken() { |
19 | const hasher = new Bun.CryptoHasher("sha256", "super-secret-invite-key"); | 17 | const hasher = new Bun.CryptoHasher("sha256", "super-secret-invite-key"); |
20 | return hasher.update(Math.random().toString()).digest("hex"); | 18 | return hasher.update(Math.random().toString()).digest("hex"); |
21 | } | 19 | } |
22 | 20 | ||
23 | // Store the token in the database | ||
24 | function createInvite() { | 21 | function createInvite() { |
25 | const token = generateInviteToken(); | 22 | const token = generateInviteToken(); |
26 | db.run("INSERT INTO invites (token) VALUES ($token)", { token }); | 23 | db.run("INSERT INTO invites (token) VALUES ($token)", { token }); |
27 | console.log(`Invite token created: ${token}`); | 24 | console.log(`Invite token created: ${token}`); |
28 | } | 25 | } |
29 | 26 | ||
30 | // CLI usage | ||
31 | const command = process.argv[2]; | 27 | const command = process.argv[2]; |
32 | const arg = process.argv[3]; | 28 | const arg = process.argv[3]; |
33 | 29 | ||
diff --git a/src/invite.js b/src/invite.js index 0f6624c..7e357ac 100644 --- a/src/invite.js +++ b/src/invite.js | |||
@@ -11,7 +11,7 @@ const validateInviteToken = async (req, res, next) => { | |||
11 | } | 11 | } |
12 | 12 | ||
13 | const invite = db | 13 | const invite = db |
14 | .query("SELECT * FROM invites WHERE token = $token AND usedAt IS null") | 14 | .query("SELECT * FROM invites WHERE token = $token") |
15 | .get({ token }); | 15 | .get({ token }); |
16 | 16 | ||
17 | if (!invite) { | 17 | if (!invite) { |
diff --git a/src/public/styles.css b/src/public/styles.css index 523e81b..2f39234 100644 --- a/src/public/styles.css +++ b/src/public/styles.css | |||
@@ -499,6 +499,7 @@ form input[type="submit"]:hover { | |||
499 | } | 499 | } |
500 | 500 | ||
501 | .register-error-message { | 501 | .register-error-message { |
502 | margin-bottom: 1rem; | ||
502 | flex-flow: row wrap; | 503 | flex-flow: row wrap; |
503 | color: var(--error-text-color); | 504 | color: var(--error-text-color); |
504 | } | 505 | } |
diff --git a/src/routes/index.js b/src/routes/index.js index 8529595..6efeb79 100644 --- a/src/routes/index.js +++ b/src/routes/index.js | |||
@@ -20,7 +20,6 @@ router.get("/", authenticateToken, async (req, res) => { | |||
20 | res.redirect("/r/all"); | 20 | res.redirect("/r/all"); |
21 | } else { | 21 | } else { |
22 | const p = subs.map((s) => s.subreddit).join("+"); | 22 | const p = subs.map((s) => s.subreddit).join("+"); |
23 | console.log(p); | ||
24 | res.redirect(`/r/${p}`); | 23 | res.redirect(`/r/${p}`); |
25 | } | 24 | } |
26 | }); | 25 | }); |
@@ -143,7 +142,9 @@ router.post("/register", validateInviteToken, async (req, res) => { | |||
143 | try { | 142 | try { |
144 | const hashedPassword = await Bun.password.hash(password); | 143 | const hashedPassword = await Bun.password.hash(password); |
145 | 144 | ||
146 | db.query("UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id", { | 145 | db.query( |
146 | "UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id", | ||
147 | ).run({ | ||
147 | id: req.invite.id, | 148 | id: req.invite.id, |
148 | }); | 149 | }); |
149 | 150 | ||
@@ -165,7 +166,6 @@ router.post("/register", validateInviteToken, async (req, res) => { | |||
165 | }) | 166 | }) |
166 | .redirect("/"); | 167 | .redirect("/"); |
167 | } catch (err) { | 168 | } catch (err) { |
168 | console.log(err); | ||
169 | return res.render("register", { | 169 | return res.render("register", { |
170 | message: "error registering user, try again later", | 170 | message: "error registering user, try again later", |
171 | }); | 171 | }); |