Add auth bootstrap and database safeguards

This commit is contained in:
JB
2026-07-24 22:12:52 -04:00
parent 8a0bfd2187
commit b430a291fa
22 changed files with 380 additions and 62 deletions
+17
View File
@@ -0,0 +1,17 @@
import type { Database } from '@tursodatabase/database';
/**
* Atomically promotes the authenticated user when no administrator exists.
* The conditional UPDATE ensures concurrent first sign-ins cannot both win.
*/
export async function ensureBootstrapAdmin(database: Database, userId: string): Promise<void> {
const promote = await database.prepare(`
UPDATE users
SET role = 'admin'
WHERE id = ?
AND NOT EXISTS (
SELECT 1 FROM users WHERE role = 'admin'
)
`);
await promote.run(userId);
}
+1 -1
View File
@@ -11,7 +11,7 @@ let databasePromise: Promise<Database> | undefined;
* imported into browser bundles.
*/
export function getDatabase(): Promise<Database> {
const configuredPath = env.TDR_DATABASE_PATH || './data/shd-build.db';
const configuredPath = env.TDR_DATABASE_PATH || './data/shd-build-development.db';
if (!databasePromise) {
databasePromise = connect(resolve(configuredPath));