forked from harikrishna0315/uni_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin_script.js
More file actions
130 lines (112 loc) Β· 5.74 KB
/
create_admin_script.js
File metadata and controls
130 lines (112 loc) Β· 5.74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const bcrypt = require('bcrypt');
const mysql = require('mysql2/promise');
// UPDATE THESE WITH YOUR DATABASE CREDENTIALS
const DB_CONFIG = {
host: 'localhost',
user: 'univ_user', // Change to your MySQL username
password: '98652006', // Change to your MySQL password
database: 'university_map'
};
async function createAdminUser() {
let connection;
try {
console.log('π Connecting to database...');
connection = await mysql.createConnection(DB_CONFIG);
console.log('β
Connected to database\n');
// Define admin credentials
const adminUser = {
username: 'admin',
password: 'admin123', // Plain text password
email: 'admin@university.edu',
full_name: 'System Administrator',
role: 'admin'
};
console.log('π Hashing password...');
const password_hash = await bcrypt.hash(adminUser.password, 10);
console.log('β
Password hashed successfully');
console.log('Hash:', password_hash, '\n');
// Check if admin already exists
console.log('π Checking for existing admin user...');
const [existingUsers] = await connection.query(
'SELECT user_id, username FROM users WHERE username = ?',
[adminUser.username]
);
if (existingUsers.length > 0) {
console.log('β οΈ Admin user already exists. Updating password...');
await connection.query(
'UPDATE users SET password_hash = ?, email = ?, full_name = ?, role = ?, is_active = TRUE WHERE username = ?',
[password_hash, adminUser.email, adminUser.full_name, adminUser.role, adminUser.username]
);
console.log('β
Admin password updated successfully!');
} else {
console.log('β Creating new admin user...');
await connection.query(
'INSERT INTO users (username, password_hash, email, full_name, role, is_active) VALUES (?, ?, ?, ?, ?, TRUE)',
[adminUser.username, password_hash, adminUser.email, adminUser.full_name, adminUser.role]
);
console.log('β
Admin user created successfully!');
}
// Verify the user was created/updated
console.log('\nπ Verifying admin user...');
const [users] = await connection.query(
'SELECT user_id, username, email, full_name, role, is_active FROM users WHERE username = ?',
[adminUser.username]
);
if (users.length > 0) {
console.log('β
Admin user verified:');
console.log('βββββββββββββββββββββββββββββββββββββ');
console.log('User ID :', users[0].user_id);
console.log('Username :', users[0].username);
console.log('Email :', users[0].email);
console.log('Full Name :', users[0].full_name);
console.log('Role :', users[0].role);
console.log('Active :', users[0].is_active ? 'Yes' : 'No');
console.log('βββββββββββββββββββββββββββββββββββββ');
}
// Test the password
console.log('\nπ§ͺ Testing password verification...');
const [testUsers] = await connection.query(
'SELECT password_hash FROM users WHERE username = ?',
[adminUser.username]
);
const isValid = await bcrypt.compare(adminUser.password, testUsers[0].password_hash);
if (isValid) {
console.log('β
Password verification test PASSED!');
} else {
console.log('β Password verification test FAILED!');
throw new Error('Password verification failed');
}
console.log('\nπ SUCCESS! You can now login with:');
console.log('βββββββββββββββββββββββββββββββββββββ');
console.log('Username: admin');
console.log('Password: admin123');
console.log('βββββββββββββββββββββββββββββββββββββ');
console.log('\nπ‘ TIP: Change this password after first login!');
} catch (error) {
console.error('\nβ ERROR:', error.message);
if (error.code === 'ER_ACCESS_DENIED_ERROR') {
console.log('\nπ§ Fix: Update DB_CONFIG in this file with correct MySQL credentials');
} else if (error.code === 'ER_BAD_DB_ERROR') {
console.log('\nπ§ Fix: Create the database first:');
console.log(' mysql -u root -p');
console.log(' CREATE DATABASE university_map;');
} else if (error.code === 'ECONNREFUSED') {
console.log('\nπ§ Fix: Start MySQL service');
} else if (error.code === 'ER_NO_SUCH_TABLE') {
console.log('\nπ§ Fix: Import the database schema:');
console.log(' mysql -u root -p university_map < university_map.sql');
}
process.exit(1);
} finally {
if (connection) {
await connection.end();
console.log('\nπ Database connection closed');
}
}
}
// Run the script
console.log('ββββββββββββββββββββββββββββββββββββββββββ');
console.log('β Admin User Creation Script β');
console.log('β University Map System β');
console.log('ββββββββββββββββββββββββββββββββββββββββββ\n');
createAdminUser();