forked from harikrishna0315/uni_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_admin.js
More file actions
47 lines (39 loc) · 1.53 KB
/
Copy pathfix_admin.js
File metadata and controls
47 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const mysql = require('mysql2/promise');
const bcrypt = require('bcrypt');
require('dotenv').config();
async function resetAdminPassword() {
const config = {
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'uni'
};
let connection;
try {
connection = await mysql.createConnection(config);
const password = 'admin123';
const hash = await bcrypt.hash(password, 10);
console.log(`🔒 Hashing password '${password}'...`);
// Check if admin exists
const [rows] = await connection.query('SELECT * FROM users WHERE username = ?', ['admin']);
if (rows.length === 0) {
console.log('👤 Admin user not found. Creating one...');
await connection.query(
'INSERT INTO users (username, password_hash, email, full_name, role) VALUES (?, ?, ?, ?, ?)',
['admin', hash, 'admin@university.edu', 'System Administrator', 'admin']
);
} else {
console.log('👤 Admin user found. Updating password...');
await connection.query(
'UPDATE users SET password_hash = ? WHERE username = ?',
[hash, 'admin']
);
}
console.log('✅ Admin password set to: admin123');
} catch (err) {
console.error('❌ Error:', err.message);
} finally {
if (connection) await connection.end();
}
}
resetAdminPassword();