-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-test-admin.js
More file actions
54 lines (48 loc) · 1.62 KB
/
create-test-admin.js
File metadata and controls
54 lines (48 loc) · 1.62 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
// Script to create a test admin for login testing
const mongoose = require('mongoose');
const Admin = require('./src/models/admin');
const bcrypt = require('bcrypt');
require('dotenv').config();
async function createTestAdmin() {
try {
console.log('MongoDB URI:', process.env.MONGO_DB_URI);
// Connect to database
await mongoose.connect(process.env.MONGO_DB_URI);
console.log('Connected to MongoDB');
// Define test admin credentials
const email = 'testadmin@quickshift.com';
const password = 'TestAdmin123!';
const firstName = 'Test';
const lastName = 'Admin';
// Check if admin with this email already exists
const existingAdmin = await Admin.findOne({ email });
if (existingAdmin) {
console.log('Admin with this email already exists:', email);
console.log('Updating password...');
// Save plain password so pre-save hook hashes it
existingAdmin.password = password;
await existingAdmin.save();
console.log('Password updated for admin:', email);
} else {
// Create admin user with plain password
const admin = new Admin({
email,
password, // Save plain password
firstName,
lastName,
role: 'admin',
isActive: true
});
await admin.save();
console.log('Test admin created successfully!');
console.log('Email:', email);
console.log('Password:', password);
}
} catch (error) {
console.error('Error creating/updating test admin:', error);
} finally {
await mongoose.connection.close();
console.log('MongoDB connection closed');
}
}
createTestAdmin();