diff --git a/actions/auth.js b/actions/auth.js new file mode 100644 index 0000000..bf0fd3b --- /dev/null +++ b/actions/auth.js @@ -0,0 +1,19 @@ +import { queryOne } from "@/lib/db"; +import bcrypt from "bcryptjs"; + +export async function validateCredentials(username, password) { + const user = await queryOne("SELECT * FROM users WHERE username = ?", [ + username, + ]); + + if (user && bcrypt.compareSync(password, user.password)) { + return { + id: user.id, + username: user.username, + role: user.role, + teamId: user.team_id, + }; + } + + return null; +} diff --git a/database.sqlite b/database.sqlite new file mode 100644 index 0000000..a1ad0f1 Binary files /dev/null and b/database.sqlite differ diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..9c33383 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "@/*": ["./*"] + } + } +} diff --git a/lib/init-db.js b/lib/init-db.js new file mode 100644 index 0000000..c253a76 --- /dev/null +++ b/lib/init-db.js @@ -0,0 +1,33 @@ +import { run } from "./db.js"; + +async function initializeDatabase() { + try { + // Create users table + await run(` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL, + role TEXT DEFAULT 'user', + team_id INTEGER, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + `); + + // Create teams table + await run(` + CREATE TABLE IF NOT EXISTS teams ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + code TEXT UNIQUE NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + `); + + console.log("Database initialized successfully"); + } catch (error) { + console.error("Database initialization error:", error); + } +} + +initializeDatabase();