-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateInitialUser.js
More file actions
69 lines (57 loc) · 2.01 KB
/
createInitialUser.js
File metadata and controls
69 lines (57 loc) · 2.01 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
import pg from 'pg';
const { Pool } = pg;
dotenv.config();
if (!process.env.DATABASE_URL) {
console.error('DATABASE_URL environment variable is not set');
process.exit(1);
}
try {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: false
});
async function testConnection() {
try {
const result = await pool.query('SELECT NOW()');
console.log('Database connected successfully:', result.rows[0].now);
return true;
} catch (error) {
console.error('Connection test failed:', error.message);
return false;
}
}
async function createInitialUser() {
if (!(await testConnection())) {
process.exit(1);
}
const username = process.argv[2];
const password = process.argv[3];
if (!username || !password) {
console.error('Usage: node createInitialUser.js <username> <password>');
process.exit(1);
}
try {
const existingUsers = await pool.query('SELECT COUNT(*) FROM website.users');
if (parseInt(existingUsers.rows[0].count) > 0) {
console.error('Users already exist in database');
process.exit(1);
}
const hashedPassword = await bcrypt.hash(password, 10);
await pool.query(
'INSERT INTO website.users (username, password_hash, is_admin, totp_enabled) VALUES ($1, $2, true, false)',
[username, hashedPassword]
);
console.log('Initial admin user created successfully');
} catch (error) {
console.error('Error creating initial user:', error.message);
} finally {
await pool.end();
}
}
createInitialUser();
} catch (error) {
console.error('Failed to initialize database connection:', error.message);
process.exit(1);
}