cs2-rcon-panel/db.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-09-28 07:23:33 +02:00
const Database = require('better-sqlite3');
const bcrypt = require('bcrypt');
2024-11-16 12:31:54 +01:00
const better_sqlite_client = new Database('./db/cspanel.db');
2023-09-28 07:23:33 +02:00
better_sqlite_client.exec(`
CREATE TABLE IF NOT EXISTS servers (
id INTEGER PRIMARY KEY,
serverIP TEXT NOT NULL,
serverPort INTEGER NOT NULL,
rconPassword TEXT NOT NULL
)
`);
better_sqlite_client.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
)
`);
const default_username = 'cspanel';
const default_password = 'v67ic55x4ghvjfj';
// Hash the default password
const hashed_password = bcrypt.hashSync(default_password, 10);
// Check if the default user already exists
const existing_user = better_sqlite_client.prepare(`
SELECT * FROM users WHERE username = ?
`).get(default_username);
if (existing_user) {
console.log('Default user already exists');
} else {
// Insert the default user into the users table
const insert_query = better_sqlite_client.prepare(`
INSERT INTO users (username, password)
VALUES (?, ?)
`);
insert_query.run(default_username, hashed_password);
console.log('Default user created successfully.');
}
module.exports = {
better_sqlite_client
};