API, Backend & Frontend

This commit is contained in:
Ichitux
2026-04-01 01:18:21 +02:00
parent 331c04fbef
commit 0fe8ec9bc0
44 changed files with 10060 additions and 0 deletions

88
backend/create-admin.js Normal file
View File

@@ -0,0 +1,88 @@
import sqlite3 from 'sqlite3';
import { promisify } from 'util';
import bcrypt from 'bcrypt';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dbPath = path.join(__dirname, 'database.sqlite');
const db = new sqlite3.Database(dbPath);
// Custom wrapper to get lastID from db.run
function dbRun(sql, params = []) {
return new Promise((resolve, reject) => {
db.run(sql, params, function(err) {
if (err) reject(err);
else resolve({ lastID: this.lastID, changes: this.changes });
});
});
}
const dbGet = promisify(db.get.bind(db));
// Initialize users table
async function initDatabase() {
try {
await dbRun(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Database table initialized');
} catch (error) {
console.error('Error initializing database:', error);
throw error;
}
}
async function createAdmin() {
try {
// Initialize database tables first
await initDatabase();
// Default admin credentials
const username = process.env.ADMIN_USERNAME || 'admin';
const password = process.env.ADMIN_PASSWORD || 'admin123';
// Check if admin already exists
const existing = await dbGet(
'SELECT * FROM users WHERE username = ?',
[username]
);
if (existing) {
console.log(`Admin user '${username}' already exists.`);
console.log('To change the password, delete the user first and run this script again.');
db.close();
return;
}
// Hash password
const saltRounds = 10;
const passwordHash = await bcrypt.hash(password, saltRounds);
// Create admin user
await dbRun(
'INSERT INTO users (username, password_hash) VALUES (?, ?)',
[username, passwordHash]
);
console.log('✅ Admin user created successfully!');
console.log(`Username: ${username}`);
console.log(`Password: ${password}`);
console.log('\n⚠ IMPORTANT: Change the default password after first login!');
console.log(' You can set ADMIN_USERNAME and ADMIN_PASSWORD environment variables to customize.');
} catch (error) {
console.error('Error creating admin user:', error);
} finally {
db.close();
}
}
createAdmin();