-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-admin.js
More file actions
53 lines (44 loc) · 1.54 KB
/
seed-admin.js
File metadata and controls
53 lines (44 loc) · 1.54 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
const bcrypt = require('bcryptjs');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const path = require('path');
// Load env from .env.local
dotenv.config({ path: path.join(__dirname, '.env.local') });
const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI) {
console.error('Please define MONGODB_URI in .env.local');
process.exit(1);
}
const AdminSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
createdAt: { type: Date, default: Date.now }
});
const Admin = mongoose.models.Admin || mongoose.model('Admin', AdminSchema);
async function seed() {
try {
await mongoose.connect(MONGODB_URI);
console.log('Connected to MongoDB');
const email = process.env.ADMIN_EMAIL || 'akash@neupane.com';
const password = process.env.ADMIN_PASSWORD || 'Admin@1234';
const existingAdmin = await Admin.findOne({ email });
if (existingAdmin) {
console.log('Admin already exists');
} else {
const hashedPassword = await bcrypt.hash(password, 10);
await Admin.create({
email,
password: hashedPassword
});
console.log('Admin created successfully');
console.log('Email:', email);
console.log('Password:', password);
}
} catch (err) {
console.error('Seed error:', err);
} finally {
await mongoose.disconnect();
process.exit(0);
}
}
seed();