| 1 | #!/usr/bin/env node
|
|---|
| 2 | // Break-glass: reset (or set) the password of an admin account. Always works,
|
|---|
| 3 | // no email required — the self-hoster has shell/server access.
|
|---|
| 4 | //
|
|---|
| 5 | // Usage:
|
|---|
| 6 | // npm run reset-admin # reset the (first) god user, print new password
|
|---|
| 7 | // npm run reset-admin -- <user|email> # reset a specific user, print new password
|
|---|
| 8 | // npm run reset-admin -- <user|email> <pw> # set a chosen password
|
|---|
| 9 | //
|
|---|
| 10 | // Run from the project root so DATABASE_PATH/.env is loaded correctly.
|
|---|
| 11 |
|
|---|
| 12 | import 'dotenv/config';
|
|---|
| 13 | import crypto from 'crypto';
|
|---|
| 14 | import bcrypt from 'bcryptjs';
|
|---|
| 15 | import db from '../src/config/database.js';
|
|---|
| 16 |
|
|---|
| 17 | const arg = process.argv[2];
|
|---|
| 18 | const pwArg = process.argv[3];
|
|---|
| 19 |
|
|---|
| 20 | let user;
|
|---|
| 21 | if (arg) {
|
|---|
| 22 | user = db.prepare('SELECT * FROM users WHERE username = ? OR LOWER(email) = LOWER(?)').get(arg, arg);
|
|---|
| 23 | } else {
|
|---|
| 24 | // No arg: pick the admin (god or admin role), otherwise the very first user.
|
|---|
| 25 | user =
|
|---|
| 26 | db.prepare("SELECT * FROM users WHERE role IN ('god','admin') ORDER BY created_at LIMIT 1").get() ||
|
|---|
| 27 | db.prepare('SELECT * FROM users ORDER BY created_at LIMIT 1').get();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | if (!user) {
|
|---|
| 31 | console.error(arg ? `Geen user gevonden voor "${arg}".` : 'Geen god-user gevonden.');
|
|---|
| 32 | process.exit(1);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | if (pwArg && pwArg.length < 8) {
|
|---|
| 36 | console.error('Wachtwoord moet minstens 8 tekens zijn.');
|
|---|
| 37 | process.exit(1);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | const newPw = pwArg || crypto.randomBytes(9).toString('base64url');
|
|---|
| 41 | db.prepare(`
|
|---|
| 42 | UPDATE users SET password_hash = ?, reset_token = NULL, reset_token_expires = NULL,
|
|---|
| 43 | updated_at = CURRENT_TIMESTAMP WHERE id = ?
|
|---|
| 44 | `).run(bcrypt.hashSync(newPw, 10), user.id);
|
|---|
| 45 |
|
|---|
| 46 | console.log(`Wachtwoord gereset voor ${user.username} <${user.email}> (rol: ${user.role}).`);
|
|---|
| 47 | if (!pwArg) console.log(`Nieuw wachtwoord: ${newPw}`);
|
|---|
| 48 | console.log('Log nu in via /auth/login en wijzig het eventueel in je account.');
|
|---|
| 49 | process.exit(0);
|
|---|