prismpanel 0.0.6 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prismpanel",
3
- "version": "0.0.6",
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 User from "../models/User.js";
6
- import { getEnvVariables } from "../utils/config.js";
4
+ import crypto from "crypto";
5
+ import { getDatabase, runQuery } from "../utils/database.js";
7
6
 
8
- export async function createUser() {
9
- const config = getEnvVariables();
10
-
11
- if (!config) {
12
- console.error(chalk.red("Error: Config file not found. Please run 'prismpanel init' first."));
13
- return;
14
- }
15
-
16
- const mongodbUri = config.mongodb_uri || config.MONGODB_URI || config.database_url;
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
- if (!mongodbUri) {
19
- console.error(chalk.red("Error: MongoDB URI not found in config. Please add 'mongodb_uri' to your @prism_panel/config.json."));
20
- return;
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 mongoose.connect(mongodbUri, {
27
- connectTimeoutMS: 5000, // Fail faster (5 seconds instead of 30)
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 connect to MongoDB."));
27
+ spinner.fail(chalk.red("Failed to open local database."));
33
28
  console.error(chalk.red("\nDetails: " + error.message));
34
- console.log(chalk.yellow("\nTroubleshooting tips:"));
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
- const newUser = new User({
61
- name: answers.name,
62
- password: answers.password,
63
- });
64
-
65
- await newUser.save();
66
- saveSpinner.succeed(chalk.green(`User '${answers.name}' created successfully.`));
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
- await mongoose.disconnect();
62
+ if (db) {
63
+ db.close();
64
+ }
72
65
  }
73
66
  }
@@ -39,7 +39,7 @@ export async function init() {
39
39
  setTimeout(resolve, 4000);
40
40
  });
41
41
 
42
- loader.succeed("Project created successfully!");
42
+ loader.succeed("Project created successfully!\nUse 'prismpanel create-user' to create a user.");
43
43
 
44
44
  // Save the config
45
45
  try {
@@ -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
+ }