aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/scripts/add-user.ts
blob: bad34fdd06218d7c34e34cd0a89499c33f924a60 (plain)
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
import * as readline from "node:readline/promises";
import * as argon2 from "argon2";
import { userRepository } from "../repositories/index.js";

async function main() {
	const rl = readline.createInterface({
		input: process.stdin,
		output: process.stdout,
	});

	const username = await rl.question("Username: ");
	const password = await rl.question("Password: ");
	rl.close();

	if (!username || !password) {
		console.error("Error: Username and password are required");
		process.exit(1);
	}

	if (password.length < 15) {
		console.error("Error: Password must be at least 15 characters");
		process.exit(1);
	}

	// Check if username already exists
	const exists = await userRepository.existsByUsername(username);
	if (exists) {
		console.error(`Error: Username "${username}" already exists`);
		process.exit(1);
	}

	// Hash password with Argon2
	const passwordHash = await argon2.hash(password);

	// Create user
	const newUser = await userRepository.create({ username, passwordHash });

	console.log(`User created successfully:`);
	console.log(`  ID: ${newUser.id}`);
	console.log(`  Username: ${newUser.username}`);

	process.exit(0);
}

main().catch((error) => {
	console.error("Error:", error.message);
	process.exit(1);
});