]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/reset-password.ts
863537500a55fb61dbbeb91d15d3204ab5a2323e
[github/Chocobozzz/PeerTube.git] / scripts / reset-password.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import * as program from 'commander'
5 import { initDatabaseModels } from '../server/initializers/database'
6 import { UserModel } from '../server/models/account/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 if (program['user'] === undefined) {
14 console.error('All parameters are mandatory.')
15 process.exit(-1)
16 }
17
18 initDatabaseModels(true)
19 .then(() => {
20 return UserModel.loadByUsername(program['user'])
21 })
22 .then(user => {
23 if (!user) {
24 console.error('User unknown.')
25 process.exit(-1)
26 }
27
28 const readline = require('readline')
29 const Writable = require('stream').Writable
30 const mutableStdout = new Writable({
31 write: function (chunk, encoding, callback) {
32 callback()
33 }
34 })
35 const rl = readline.createInterface({
36 input: process.stdin,
37 output: mutableStdout,
38 terminal: true
39 })
40
41 console.log('New password?')
42 rl.on('line', function (password) {
43 if (!isUserPasswordValid(password)) {
44 console.error('New password is invalid.')
45 process.exit(-1)
46 }
47
48 user.password = password
49
50 user.save()
51 .then(() => console.log('User password updated.'))
52 .catch(err => console.error(err))
53 .finally(() => process.exit(0))
54 })
55 })