-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-db.js
More file actions
75 lines (59 loc) · 2.13 KB
/
seed-db.js
File metadata and controls
75 lines (59 loc) · 2.13 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
// seed-db.js - Script to create initial admin user and sample data
import { MongoClient } from 'mongodb';
import crypto from 'crypto';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
const MONGODB_URI = process.env.VITE_MONGODB_URI || 'mongodb://localhost:27017/like-us-tshirts';
// Match the hashing method used in the application
async function hashPassword(password) {
// Using Node.js crypto since we can't use Web Crypto API in Node
return crypto.createHash('sha256').update(password).digest('hex');
}
async function seedDatabase() {
const client = new MongoClient(MONGODB_URI);
try {
await client.connect();
console.log(`Connected to MongoDB at ${MONGODB_URI}`);
const db = client.db();
// Check if admin user already exists
const existingAdmin = await db.collection('users').findOne({ email: 'admin@example.com' });
if (!existingAdmin) {
// Create admin user
const hashedPassword = await hashPassword('admin123');
await db.collection('users').insertOne({
email: 'admin@example.com',
password: hashedPassword,
name: 'Admin User',
role: 'admin',
createdAt: new Date()
});
console.log('Admin user created successfully');
} else {
console.log('Admin user already exists');
}
// Check if regular user already exists
const existingUser = await db.collection('users').findOne({ email: 'user@example.com' });
if (!existingUser) {
// Create regular user
const hashedPassword = await hashPassword('user123');
await db.collection('users').insertOne({
email: 'user@example.com',
password: hashedPassword,
name: 'Regular User',
role: 'user',
createdAt: new Date()
});
console.log('Regular user created successfully');
} else {
console.log('Regular user already exists');
}
console.log('Database seeding completed');
} catch (error) {
console.error('Error seeding database:', error);
} finally {
await client.close();
}
}
// Run the seed function
seedDatabase();