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