fix minor log entry issue
[websub-hub] / bin / authUserAdd.js
1 'use strict';
2
3 const argon2 = require('argon2');
4 const readline = require('readline');
5 const stream = require('stream');
6 const DB = require('../src/db');
7 const Logger = require('../src/logger');
8 const Config = require('../config');
9 const config = new Config(process.env.NODE_ENV);
10
11 const logger = new Logger(config);
12 const db = new DB(logger, config);
13
14 const identifier = process.argv[2];
15
16 if (!identifier) {
17 console.log('missing user to add');
18 throw new Error('missing argument');
19 }
20
21 async function readPassword(prompt) {
22 const input = process.stdin;
23 const output = new stream.Writable({
24 write: function (chunk, encoding, callback) {
25 if (!this.muted) {
26 process.stdout.write(chunk, encoding);
27 }
28 callback();
29 },
30 });
31 const rl = readline.createInterface({ input, output, terminal: !!process.stdin.isTTY });
32 rl.setPrompt(prompt);
33 rl.prompt();
34 output.muted = true;
35
36 return new Promise((resolve) => {
37 rl.question('', (answer) => {
38 output.muted = false;
39 rl.close();
40 output.write('\n');
41 resolve(answer);
42 });
43 });
44 }
45
46 (async () => {
47 await db.initialize();
48 const password = await readPassword('password: ');
49 const credential = await argon2.hash(password, { type: argon2.argon2id });
50 console.log(`\t${identifier}:${credential}`);
51 await db.context(async (dbCtx) => {
52 const result = await db.authenticationUpsert(dbCtx, identifier, credential);
53 console.log(result);
54 });
55 console.log('done');
56 await db._closeConnection();
57 })();