diff options
Diffstat (limited to 'server/scripts/reset-password.ts')
-rwxr-xr-x | server/scripts/reset-password.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/server/scripts/reset-password.ts b/server/scripts/reset-password.ts new file mode 100755 index 000000000..96e301ba9 --- /dev/null +++ b/server/scripts/reset-password.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | import { program } from 'commander' | ||
2 | import readline from 'readline' | ||
3 | import { Writable } from 'stream' | ||
4 | import { isUserPasswordValid } from '@server/helpers/custom-validators/users.js' | ||
5 | import { initDatabaseModels } from '@server/initializers/database.js' | ||
6 | import { UserModel } from '@server/models/user/user.js' | ||
7 | |||
8 | program | ||
9 | .option('-u, --user [user]', 'User') | ||
10 | .parse(process.argv) | ||
11 | |||
12 | const options = program.opts() | ||
13 | |||
14 | if (options.user === undefined) { | ||
15 | console.error('All parameters are mandatory.') | ||
16 | process.exit(-1) | ||
17 | } | ||
18 | |||
19 | initDatabaseModels(true) | ||
20 | .then(() => { | ||
21 | return UserModel.loadByUsername(options.user) | ||
22 | }) | ||
23 | .then(user => { | ||
24 | if (!user) { | ||
25 | console.error('Unknown user.') | ||
26 | process.exit(-1) | ||
27 | } | ||
28 | |||
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 | }) | ||