prismpanel 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/commands/create-user.js +31 -38
- package/src/commands/init.js +1 -1
- package/src/utils/database.js +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prismpanel",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"chalk": "^5.6.2",
|
|
18
18
|
"inquirer": "^13.3.2",
|
|
19
19
|
"mongoose": "^9.4.1",
|
|
20
|
-
"ora": "^9.3.0"
|
|
20
|
+
"ora": "^9.3.0",
|
|
21
|
+
"sqlite3": "^6.0.1"
|
|
21
22
|
}
|
|
22
23
|
}
|
|
@@ -1,41 +1,32 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
1
|
import inquirer from "inquirer";
|
|
3
2
|
import chalk from "chalk";
|
|
4
3
|
import ora from "ora";
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
4
|
+
import crypto from "crypto";
|
|
5
|
+
import { getDatabase, runQuery } from "../utils/database.js";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
7
|
+
/**
|
|
8
|
+
* Hash a password using scrypt (Node.js native).
|
|
9
|
+
*
|
|
10
|
+
* @param {string} password
|
|
11
|
+
* @returns {string} - salt.hash format
|
|
12
|
+
*/
|
|
13
|
+
function hashPassword(password) {
|
|
14
|
+
const salt = crypto.randomBytes(16).toString("hex");
|
|
15
|
+
const hash = crypto.scryptSync(password, salt, 64).toString("hex");
|
|
16
|
+
return `${salt}.${hash}`;
|
|
17
|
+
}
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
19
|
+
export async function createUser() {
|
|
20
|
+
const spinner = ora("Opening local database...").start();
|
|
21
|
+
let db;
|
|
22
22
|
|
|
23
|
-
// Connect FIRST before asking questions
|
|
24
|
-
const spinner = ora("Connecting to MongoDB...").start();
|
|
25
23
|
try {
|
|
26
|
-
await
|
|
27
|
-
|
|
28
|
-
serverSelectionTimeoutMS: 5000,
|
|
29
|
-
});
|
|
30
|
-
spinner.succeed(chalk.green("Connected to MongoDB."));
|
|
24
|
+
db = await getDatabase();
|
|
25
|
+
spinner.succeed(chalk.green("Connected to SQLite database."));
|
|
31
26
|
} catch (error) {
|
|
32
|
-
spinner.fail(chalk.red("Failed to
|
|
27
|
+
spinner.fail(chalk.red("Failed to open local database."));
|
|
33
28
|
console.error(chalk.red("\nDetails: " + error.message));
|
|
34
|
-
|
|
35
|
-
console.log(chalk.gray(" 1. Check if your IP is whitelisted in Atlas (Network Access)."));
|
|
36
|
-
console.log(chalk.gray(" 2. Make sure your connection string is correct in config.json."));
|
|
37
|
-
console.log(chalk.gray(" 3. Try using a database name (e.g. ...mongodb.net/prism)."));
|
|
38
|
-
return; // Exit if connection fails
|
|
29
|
+
return;
|
|
39
30
|
}
|
|
40
31
|
|
|
41
32
|
const answers = await inquirer.prompt([
|
|
@@ -54,20 +45,22 @@ export async function createUser() {
|
|
|
54
45
|
},
|
|
55
46
|
]);
|
|
56
47
|
|
|
57
|
-
const saveSpinner = ora("Creating user...").start();
|
|
48
|
+
const saveSpinner = ora("Creating user and hashing password...").start();
|
|
58
49
|
|
|
59
50
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
saveSpinner.succeed(chalk.green(`User '${answers.name}' created
|
|
51
|
+
// POWERFUL HASHING LOGIC
|
|
52
|
+
const securePassword = hashPassword(answers.password);
|
|
53
|
+
|
|
54
|
+
const sql = "INSERT INTO users (name, password) VALUES (?, ?)";
|
|
55
|
+
await runQuery(db, sql, [answers.name, securePassword]);
|
|
56
|
+
|
|
57
|
+
saveSpinner.succeed(chalk.green(`User '${answers.name}' created with a powerful encrypted password.`));
|
|
67
58
|
} catch (error) {
|
|
68
59
|
saveSpinner.fail(chalk.red("Failed to save user."));
|
|
69
60
|
console.error(chalk.red(error.message));
|
|
70
61
|
} finally {
|
|
71
|
-
|
|
62
|
+
if (db) {
|
|
63
|
+
db.close();
|
|
64
|
+
}
|
|
72
65
|
}
|
|
73
66
|
}
|
package/src/commands/init.js
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import sqlite3 from "sqlite3";
|
|
2
|
+
import { getEnvVariables } from "./config.js";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get and initialize the SQLite database.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Promise<sqlite3.Database>}
|
|
10
|
+
*/
|
|
11
|
+
export async function getDatabase() {
|
|
12
|
+
const config = getEnvVariables();
|
|
13
|
+
|
|
14
|
+
if (!config || !config.db_path) {
|
|
15
|
+
throw new Error("SQLite database not configured in config.json");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const dbPath = path.resolve(process.cwd(), config.db_path);
|
|
19
|
+
|
|
20
|
+
// Create folder if it doesn't exist
|
|
21
|
+
const dbDir = path.dirname(dbPath);
|
|
22
|
+
if (!fs.existsSync(dbDir)) {
|
|
23
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const db = new sqlite3.Database(dbPath, (err) => {
|
|
28
|
+
if (err) return reject(err);
|
|
29
|
+
|
|
30
|
+
// Initialize tables
|
|
31
|
+
db.run(`
|
|
32
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
33
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
34
|
+
name TEXT NOT NULL,
|
|
35
|
+
password TEXT NOT NULL,
|
|
36
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
37
|
+
)
|
|
38
|
+
`, (err) => {
|
|
39
|
+
if (err) return reject(err);
|
|
40
|
+
resolve(db);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Execute a query that doesn't return rows (INSERT, UPDATE, DELETE).
|
|
48
|
+
*
|
|
49
|
+
* @param {sqlite3.Database} db
|
|
50
|
+
* @param {string} sql
|
|
51
|
+
* @param {any[]} params
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*/
|
|
54
|
+
export function runQuery(db, sql, params = []) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
db.run(sql, params, function(err) {
|
|
57
|
+
if (err) return reject(err);
|
|
58
|
+
resolve({ lastID: this.lastID, changes: this.changes });
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|