]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/reset-password.ts
Introduce accounts command
[github/Chocobozzz/PeerTube.git] / scripts / reset-password.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import { program } from 'commander'
5 import { initDatabaseModels } from '../server/initializers/database'
6 import { UserModel } from '../server/models/user/user'
7 import { isUserPasswordValid } from '../server/helpers/custom-validators/users'
8
9 program
10 .option('-u, --user [user]', 'User')
11 .parse(process.argv)
12
13 const options = program.opts()
14
15 if (options.user === undefined) {
16 console.error('All parameters are mandatory.')
17 process.exit(-1)
18 }
19
20 initDatabaseModels(true)
21 .then(() => {
22 return UserModel.loadByUsername(options.user)
23 })
24 .then(user => {
25 if (!user) {
26 console.error('Unknown user.')
27 process.exit(-1)
28 }
29
30 const readline = require('readline')
31 const Writable = require('stream').Writable
32 const mutableStdout = new Writable({
33 write: function (_chunk, _encoding, callback) {
34 callback()
35 }
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) {
45 if (!isUserPasswordValid(password)) {
46 console.error('New password is invalid.')
47 process.exit(-1)
48 }
49
50 user.password = password
51
52 user.save()
53 .then(() => console.log('User password updated.'))
54 .catch(err => console.error(err))
55 .finally(() => process.exit(0))
56 })
57 })
58 .catch(err => {
59 console.error(err)
60 process.exit(-1)
61 })