|
| 1 | +require('dotenv').config(); |
| 2 | +const mongoose = require('mongoose'); |
| 3 | +const bcrypt = require('bcryptjs'); |
| 4 | +const crypto = require('crypto'); |
| 5 | + |
| 6 | +// Connect to MongoDB |
| 7 | +mongoose.connect(process.env.MONGODB_URI, { |
| 8 | + useNewUrlParser: true, |
| 9 | + useUnifiedTopology: true |
| 10 | +}); |
| 11 | + |
| 12 | +const userSchema = new mongoose.Schema({ |
| 13 | + username: { type: String, required: true, unique: true }, |
| 14 | + password: { type: String, required: true }, |
| 15 | + email: { type: String, required: true }, |
| 16 | + role: { type: String, enum: ['user', 'admin'], default: 'user' }, |
| 17 | + permissions: { |
| 18 | + canChat: { type: Boolean, default: true }, |
| 19 | + canViewAnalytics: { type: Boolean, default: false }, |
| 20 | + canAccessLearnedDocs: { type: Boolean, default: false } |
| 21 | + }, |
| 22 | + createdAt: { type: Date, default: Date.now } |
| 23 | +}); |
| 24 | + |
| 25 | +const User = mongoose.model('User', userSchema); |
| 26 | + |
| 27 | +// Generate secure random password |
| 28 | +function generateSecurePassword(length = 24) { |
| 29 | + const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*'; |
| 30 | + const password = crypto.randomBytes(length) |
| 31 | + .toString('base64') |
| 32 | + .slice(0, length) |
| 33 | + .split('') |
| 34 | + .map((char, i) => charset[crypto.randomBytes(1)[0] % charset.length]) |
| 35 | + .join(''); |
| 36 | + return password; |
| 37 | +} |
| 38 | + |
| 39 | +async function rotateAccounts() { |
| 40 | + try { |
| 41 | + console.log('π Starting account rotation...\n'); |
| 42 | + |
| 43 | + // Delete all existing users |
| 44 | + const deleteResult = await User.deleteMany({}); |
| 45 | + console.log(`β
Deleted ${deleteResult.deletedCount} existing accounts\n`); |
| 46 | + |
| 47 | + // Generate new secure passwords |
| 48 | + const adminPassword = generateSecurePassword(24); |
| 49 | + const userPassword = generateSecurePassword(24); |
| 50 | + |
| 51 | + // Hash passwords |
| 52 | + const adminHashedPassword = await bcrypt.hash(adminPassword, 10); |
| 53 | + const userHashedPassword = await bcrypt.hash(userPassword, 10); |
| 54 | + |
| 55 | + // Create new admin account |
| 56 | + const newAdmin = await User.create({ |
| 57 | + username: 'admin', |
| 58 | + password: adminHashedPassword, |
| 59 | + |
| 60 | + role: 'admin', |
| 61 | + permissions: { |
| 62 | + canChat: true, |
| 63 | + canViewAnalytics: true, |
| 64 | + canAccessLearnedDocs: true |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + // Create new demo user account |
| 69 | + const newUser = await User.create({ |
| 70 | + username: 'user', |
| 71 | + password: userHashedPassword, |
| 72 | + |
| 73 | + role: 'user', |
| 74 | + permissions: { |
| 75 | + canChat: true, |
| 76 | + canViewAnalytics: false, |
| 77 | + canAccessLearnedDocs: false |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + console.log('β
New accounts created successfully!\n'); |
| 82 | + console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ'); |
| 83 | + console.log('π NEW CREDENTIALS - SAVE THESE SECURELY!'); |
| 84 | + console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n'); |
| 85 | + console.log('ADMIN ACCOUNT:'); |
| 86 | + console.log(` Username: admin`); |
| 87 | + console.log(` Password: ${adminPassword}`); |
| 88 | + console.log(` Email: [email protected]`); |
| 89 | + console.log(` Role: admin\n`); |
| 90 | + console.log('USER ACCOUNT:'); |
| 91 | + console.log(` Username: user`); |
| 92 | + console.log(` Password: ${userPassword}`); |
| 93 | + console.log(` Email: [email protected]`); |
| 94 | + console.log(` Role: user\n`); |
| 95 | + console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ'); |
| 96 | + console.log('β οΈ IMPORTANT: Save these credentials immediately!'); |
| 97 | + console.log('β οΈ They will not be displayed again.'); |
| 98 | + console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n'); |
| 99 | + |
| 100 | + // Close connection |
| 101 | + await mongoose.connection.close(); |
| 102 | + console.log('β
Database connection closed'); |
| 103 | + process.exit(0); |
| 104 | + |
| 105 | + } catch (error) { |
| 106 | + console.error('β Error rotating accounts:', error); |
| 107 | + await mongoose.connection.close(); |
| 108 | + process.exit(1); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Run the rotation |
| 113 | +rotateAccounts(); |
0 commit comments