reset-password.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. program
  8. .option('-u, --user [user]', 'User')
  9. .parse(process.argv)
  10. const options = program.opts()
  11. if (options.user === undefined) {
  12. console.error('All parameters are mandatory.')
  13. process.exit(-1)
  14. }
  15. initDatabaseModels(true)
  16. .then(() => {
  17. return UserModel.loadByUsername(options.user)
  18. })
  19. .then(user => {
  20. if (!user) {
  21. console.error('Unknown user.')
  22. process.exit(-1)
  23. }
  24. const mutableStdout = new Writable({
  25. write: function (_chunk, _encoding, callback) {
  26. callback()
  27. }
  28. })
  29. const rl = readline.createInterface({
  30. input: process.stdin,
  31. output: mutableStdout,
  32. terminal: true
  33. })
  34. console.log('New password?')
  35. rl.on('line', function (password) {
  36. if (!isUserPasswordValid(password)) {
  37. console.error('New password is invalid.')
  38. process.exit(-1)
  39. }
  40. user.password = password
  41. user.save()
  42. .then(() => console.log('User password updated.'))
  43. .catch(err => console.error(err))
  44. .finally(() => process.exit(0))
  45. })
  46. })
  47. .catch(err => {
  48. console.error(err)
  49. process.exit(-1)
  50. })