tududi/backend/scripts/user-create.js
Chris 3c1209a5a9
Express migration (#80)
* Initial migration

* Cleanup and create migration scripts

* Introduce test suite

* Fix test issues

* Correct CORS issue and update paths

* Update README
2025-06-16 21:50:44 +03:00

66 lines
No EOL
1.7 KiB
JavaScript
Executable file

#!/usr/bin/env node
/**
* User Creation Script
* Creates a new user with email and password
* Usage: node user-create.js <email> <password>
*/
require('dotenv').config();
const { User } = require('../models');
const bcrypt = require('bcrypt');
async function createUser() {
const [email, password] = process.argv.slice(2);
if (!email || !password) {
console.error('❌ Usage: npm run user:create <email> <password>');
console.error('Example: npm run user:create admin@example.com mypassword123');
process.exit(1);
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
console.error('❌ Invalid email format');
process.exit(1);
}
// Basic password validation
if (password.length < 6) {
console.error('❌ Password must be at least 6 characters long');
process.exit(1);
}
try {
console.log(`Creating user with email: ${email}`);
// Check if user already exists
const existingUser = await User.findOne({ where: { email } });
if (existingUser) {
console.error(`❌ User with email ${email} already exists`);
process.exit(1);
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
// Create the user
const user = await User.create({
email,
password: hashedPassword
});
console.log('✅ User created successfully');
console.log(`📧 Email: ${user.email}`);
console.log(`🆔 User ID: ${user.id}`);
console.log(`📅 Created: ${user.created_at}`);
process.exit(0);
} catch (error) {
console.error('❌ Error creating user:', error.message);
process.exit(1);
}
}
createUser();