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