]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/reset-password.ts
Don't send undefined with HTML input
[github/Chocobozzz/PeerTube.git] / scripts / reset-password.ts
CommitLineData
8cc61201 1import { program } from 'commander'
f8360396 2import { isUserPasswordValid } from '../server/helpers/custom-validators/users'
80fdaf06 3import { initDatabaseModels } from '../server/initializers/database'
7d9ba5c0 4import { UserModel } from '../server/models/user/user'
7df5e5e4
C
5
6program
7 .option('-u, --user [user]', 'User')
7df5e5e4
C
8 .parse(process.argv)
9
ba5a8d89
C
10const options = program.opts()
11
12if (options.user === undefined) {
7df5e5e4
C
13 console.error('All parameters are mandatory.')
14 process.exit(-1)
15}
16
91fea9fc 17initDatabaseModels(true)
6fcd19ba 18 .then(() => {
ba5a8d89 19 return UserModel.loadByUsername(options.user)
6fcd19ba
C
20 })
21 .then(user => {
7df5e5e4 22 if (!user) {
f4659d73 23 console.error('Unknown user.')
fdbda9e3 24 process.exit(-1)
7df5e5e4
C
25 }
26
c129e2a1
C
27 const readline = require('readline')
28 const Writable = require('stream').Writable
29 const mutableStdout = new Writable({
ba5a8d89 30 write: function (_chunk, _encoding, callback) {
c129e2a1 31 callback()
7df5e5e4 32 }
c129e2a1
C
33 })
34 const rl = readline.createInterface({
35 input: process.stdin,
36 output: mutableStdout,
37 terminal: true
38 })
39
40 console.log('New password?')
41 rl.on('line', function (password) {
285e04f6
C
42 if (!isUserPasswordValid(password)) {
43 console.error('New password is invalid.')
44 process.exit(-1)
45 }
46
c129e2a1
C
47 user.password = password
48
6fcd19ba
C
49 user.save()
50 .then(() => console.log('User password updated.'))
51 .catch(err => console.error(err))
52 .finally(() => process.exit(0))
7df5e5e4
C
53 })
54 })
f0af38e6
C
55 .catch(err => {
56 console.error(err)
57 process.exit(-1)
58 })