70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
// add-user.ts
|
|
// Management command to create users from the command line
|
|
|
|
import { hashPassword } from "../auth/password";
|
|
import { PostgresAuthStore, pool } from "../database";
|
|
|
|
async function main(): Promise<void> {
|
|
const args = process.argv.slice(2);
|
|
|
|
if (args.length < 2) {
|
|
console.error(
|
|
"Usage: ./mgmt add-user <email> <password> [--display-name <name>] [--active]",
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const email = args[0];
|
|
const password = args[1];
|
|
|
|
// Parse optional flags
|
|
let displayName: string | undefined;
|
|
let makeActive = false;
|
|
|
|
for (let i = 2; i < args.length; i++) {
|
|
if (args[i] === "--display-name" && args[i + 1]) {
|
|
displayName = args[i + 1];
|
|
i++;
|
|
} else if (args[i] === "--active") {
|
|
makeActive = true;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const store = new PostgresAuthStore();
|
|
|
|
// Check if user already exists
|
|
const existing = await store.getUserByEmail(email);
|
|
if (existing) {
|
|
console.error(`Error: User with email '${email}' already exists`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Hash password and create user
|
|
const passwordHash = await hashPassword(password);
|
|
const user = await store.createUser({
|
|
email,
|
|
passwordHash,
|
|
displayName,
|
|
});
|
|
|
|
// Optionally activate user immediately
|
|
if (makeActive) {
|
|
await store.updateUserEmailVerified(user.id);
|
|
console.log(
|
|
`Created and activated user: ${user.email} (${user.id})`,
|
|
);
|
|
} else {
|
|
console.log(`Created user: ${user.email} (${user.id})`);
|
|
console.log(" Status: pending (use --active to create as active)");
|
|
}
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("Failed to create user:", err.message);
|
|
process.exit(1);
|
|
});
|