reset-password.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { program } from 'commander'
  4. import { initDatabaseModels } from '../server/initializers/database'
  5. import { UserModel } from '../server/models/user/user'
  6. import { isUserPasswordValid } from '../server/helpers/custom-validators/users'
  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 readline = require('readline')
  25. const Writable = require('stream').Writable
  26. const mutableStdout = new Writable({
  27. write: function (_chunk, _encoding, callback) {
  28. callback()
  29. }
  30. })
  31. const rl = readline.createInterface({
  32. input: process.stdin,
  33. output: mutableStdout,
  34. terminal: true
  35. })
  36. console.log('New password?')
  37. rl.on('line', function (password) {
  38. if (!isUserPasswordValid(password)) {
  39. console.error('New password is invalid.')
  40. process.exit(-1)
  41. }
  42. user.password = password
  43. user.save()
  44. .then(() => console.log('User password updated.'))
  45. .catch(err => console.error(err))
  46. .finally(() => process.exit(0))
  47. })
  48. })
  49. .catch(err => {
  50. console.error(err)
  51. process.exit(-1)
  52. })