feat: initial brainstorming app — Nuxt 3 + SQLite + admin auth

Nuxt 3 app with:
- SQLite (better-sqlite3) for persistence
- Anonymous idea submission and voting
- Admin auth with session cookies
- AI analysis via Gemini API
- Nuxt UI components + Tailwind

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alejandro Martinez
2026-04-07 14:15:45 +02:00
commit e7de636cf2
25 changed files with 9114 additions and 0 deletions

41
server/utils/auth.ts Normal file
View File

@@ -0,0 +1,41 @@
import { randomBytes, timingSafeEqual } from 'crypto'
import type { H3Event } from 'h3'
import db from './db'
export function createSession(): string {
const token = randomBytes(32).toString('hex')
const expires = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString()
db.prepare('INSERT INTO sessions (token, expires_at) VALUES (?, ?)').run(token, expires)
return token
}
export function validateSession(token: string): boolean {
const row = db.prepare('SELECT expires_at FROM sessions WHERE token = ?').get(token) as any
if (!row) return false
if (new Date(row.expires_at) < new Date()) {
db.prepare('DELETE FROM sessions WHERE token = ?').run(token)
return false
}
return true
}
export function destroySession(token: string) {
db.prepare('DELETE FROM sessions WHERE token = ?').run(token)
}
export function checkPassword(input: string): boolean {
const config = useRuntimeConfig()
const expected = config.adminPassword as string
if (input.length !== expected.length) return false
return timingSafeEqual(Buffer.from(input), Buffer.from(expected))
}
export function isAdmin(event: H3Event): boolean {
return event.context.isAdmin === true
}
export function requireAdmin(event: H3Event) {
if (!isAdmin(event)) {
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
}
}

39
server/utils/db.ts Normal file
View File

@@ -0,0 +1,39 @@
import Database from 'better-sqlite3'
import { mkdirSync } from 'fs'
import { dirname } from 'path'
const config = useRuntimeConfig()
const dbPath = config.dbPath as string
mkdirSync(dirname(dbPath), { recursive: true })
const db = new Database(dbPath)
db.pragma('journal_mode = WAL')
db.pragma('foreign_keys = ON')
db.exec(`
CREATE TABLE IF NOT EXISTS ideas (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
category TEXT NOT NULL DEFAULT 'General',
votes INTEGER NOT NULL DEFAULT 0,
hidden INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS vote_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
idea_id INTEGER NOT NULL REFERENCES ideas(id) ON DELETE CASCADE,
voter_hash TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
UNIQUE(idea_id, voter_hash)
);
CREATE TABLE IF NOT EXISTS sessions (
token TEXT PRIMARY KEY,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
expires_at TEXT NOT NULL
);
`)
export default db