/ opencodehub / scripts / create-admin.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
79 lines · 2.5 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
70
71
72
73
74
75
76
77
78
79
/**
 * Create Admin Account Script
 * Run with: bun run scripts/create-admin.ts
 */

import { createClient } from "@libsql/client";
import * as bcrypt from "bcryptjs";

// Load from .env
const DATABASE_URL = process.env.DATABASE_URL || "libsql://opencodehub-swadhinbiswas.aws-ap-south-1.turso.io";
const DATABASE_AUTH_TOKEN = process.env.DATABASE_AUTH_TOKEN;

if (!DATABASE_AUTH_TOKEN) {
    console.error("❌ DATABASE_AUTH_TOKEN is required. Run with: bun run scripts/create-admin.ts");
    console.error("   Make sure your .env file has DATABASE_AUTH_TOKEN set.");
    process.exit(1);
}

// Admin account details
const ADMIN_USERNAME = "admin";
const ADMIN_EMAIL = "[email protected]";
const ADMIN_PASSWORD = "Admin@123"; // Change this!

async function main() {
    console.log("🔌 Connecting to database...");

    const client = createClient({
        url: DATABASE_URL,
        authToken: DATABASE_AUTH_TOKEN,
    });

    // Check if admin already exists
    const existing = await client.execute({
        sql: "SELECT id, username, is_admin FROM users WHERE username = ?",
        args: [ADMIN_USERNAME]
    });

    if (existing.rows.length > 0) {
        console.log("⚠️  Admin user already exists. Updating to admin role...");
        await client.execute({
            sql: "UPDATE users SET is_admin = 1 WHERE username = ?",
            args: [ADMIN_USERNAME]
        });
        console.log("✅ Admin role granted to existing user:", existing.rows[0].username);
        return;
    }

    // Hash password
    console.log("🔐 Hashing password...");
    const passwordHash = await bcrypt.hash(ADMIN_PASSWORD, 10);

    // Generate unique ID
    const id = crypto.randomUUID();
    const now = new Date().toISOString();

    // Create admin user
    console.log("👤 Creating admin user...");

    await client.execute({
        sql: `INSERT INTO users (id, username, email, password_hash, display_name, is_admin, is_active, email_verified, created_at, updated_at) 
          VALUES (?, ?, ?, ?, ?, 1, 1, 1, ?, ?)`,
        args: [id, ADMIN_USERNAME, ADMIN_EMAIL, passwordHash, "Administrator", now, now]
    });

    console.log("✅ Admin account created successfully!");
    console.log("");
    console.log("📋 Admin Credentials:");
    console.log(`   Username: ${ADMIN_USERNAME}`);
    console.log(`   Email:    ${ADMIN_EMAIL}`);
    console.log(`   Password: ${ADMIN_PASSWORD}`);
    console.log("");
    console.log("⚠️  Please change the password after first login!");
}

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