source: Klonkt/scripts/reset-admin.mjs@ 834bcc3

main
Last change on this file since 834bcc3 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 1.8 KB
Line 
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
12import 'dotenv/config';
13import crypto from 'crypto';
14import bcrypt from 'bcryptjs';
15import db from '../src/config/database.js';
16
17const arg = process.argv[2];
18const pwArg = process.argv[3];
19
20let user;
21if (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
30if (!user) {
31 console.error(arg ? `Geen user gevonden voor "${arg}".` : 'Geen god-user gevonden.');
32 process.exit(1);
33}
34
35if (pwArg && pwArg.length < 8) {
36 console.error('Wachtwoord moet minstens 8 tekens zijn.');
37 process.exit(1);
38}
39
40const newPw = pwArg || crypto.randomBytes(9).toString('base64url');
41db.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
46console.log(`Wachtwoord gereset voor ${user.username} <${user.email}> (rol: ${user.role}).`);
47if (!pwArg) console.log(`Nieuw wachtwoord: ${newPw}`);
48console.log('Log nu in via /auth/login en wijzig het eventueel in je account.');
49process.exit(0);
Note: See TracBrowser for help on using the repository browser.