-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-test-user.js
More file actions
60 lines (48 loc) · 1.81 KB
/
create-test-user.js
File metadata and controls
60 lines (48 loc) · 1.81 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
// create-test-user.js
const { MongoClient } = require('mongodb');
const bcrypt = require('bcryptjs');
async function createTestUser() {
const uri = process.env.MONGODB_URI || 'your-mongodb-connection-string';
const client = new MongoClient(uri);
try {
await client.connect();
console.log('✅ Connected to MongoDB');
const db = client.db('your-database-name'); // Replace with your actual database name
const usersCollection = db.collection('users');
// Test user credentials
const testUser = {
email: 'test@example.com',
password: 'password123',
username: 'testuser',
createdAt: new Date()
};
// Check if user already exists
const existingUser = await usersCollection.findOne({ email: testUser.email });
if (existingUser) {
console.log('❌ User already exists with email:', testUser.email);
return;
}
// Hash the password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(testUser.password, saltRounds);
console.log('🔐 Original password:', testUser.password);
console.log('🔐 Hashed password:', hashedPassword);
// Insert user with hashed password
const result = await usersCollection.insertOne({
...testUser,
password: hashedPassword
});
console.log('✅ Test user created successfully!');
console.log('📧 Email:', testUser.email);
console.log('🔑 Password:', testUser.password);
console.log('🆔 User ID:', result.insertedId);
// Verify the user was created
const createdUser = await usersCollection.findOne({ email: testUser.email });
console.log('✅ Verification - User found:', createdUser ? 'YES' : 'NO');
} catch (error) {
console.error('❌ Error creating test user:', error);
} finally {
await client.close();
}
}
createTestUser();