aboutsummaryrefslogtreecommitdiff
path: root/src/routes/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/index.js')
-rw-r--r--src/routes/index.js24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index 9a415be..e585d3d 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -136,15 +136,6 @@ router.get("/create-invite", authenticateAdmin, async (req, res) => {
136 } 136 }
137 137
138 try { 138 try {
139 db.run(`
140 CREATE TABLE IF NOT EXISTS invites (
141 id INTEGER PRIMARY KEY AUTOINCREMENT,
142 token TEXT NOT NULL,
143 createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
144 usedAt TIMESTAMP
145 )
146 `);
147
148 createInvite(); 139 createInvite();
149 return res.redirect("/dashboard"); 140 return res.redirect("/dashboard");
150 } catch (err) { 141 } catch (err) {
@@ -201,19 +192,22 @@ router.post("/register", validateInviteToken, async (req, res) => {
201 try { 192 try {
202 const hashedPassword = await Bun.password.hash(password); 193 const hashedPassword = await Bun.password.hash(password);
203 194
204 db.query( 195 if (!req.isFirstUser) {
205 "UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id", 196 db.query(
206 ).run({ 197 "UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id",
207 id: req.invite.id, 198 ).run({
208 }); 199 id: req.invite.id,
200 });
201 }
209 202
210 const insertedRecord = db 203 const insertedRecord = db
211 .query( 204 .query(
212 "INSERT INTO users (username, password_hash) VALUES ($username, $hashedPassword)", 205 "INSERT INTO users (username, password_hash, isAdmin) VALUES ($username, $hashedPassword, $isAdmin)",
213 ) 206 )
214 .run({ 207 .run({
215 username, 208 username,
216 hashedPassword, 209 hashedPassword,
210 isAdmin: req.isFirstUser ? 1 : 0,
217 }); 211 });
218 const id = insertedRecord.lastInsertRowid; 212 const id = insertedRecord.lastInsertRowid;
219 const token = jwt.sign({ username, id }, JWT_KEY, { expiresIn: "5d" }); 213 const token = jwt.sign({ username, id }, JWT_KEY, { expiresIn: "5d" });