reset-password.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { initDatabaseModels } from '../server/initializers'
  5. import { UserModel } from '../server/models/account/user'
  6. import { isUserPasswordValid } from '../server/helpers/custom-validators/users'
  7. program
  8. .option('-u, --user [user]', 'User')
  9. .parse(process.argv)
  10. if (program['user'] === undefined) {
  11. console.error('All parameters are mandatory.')
  12. process.exit(-1)
  13. }
  14. initDatabaseModels(true)
  15. .then(() => {
  16. return UserModel.loadByUsername(program['user'])
  17. })
  18. .then(user => {
  19. if (!user) {
  20. console.error('User unknown.')
  21. process.exit(-1)
  22. }
  23. const readline = require('readline')
  24. const Writable = require('stream').Writable
  25. const mutableStdout = new Writable({
  26. write: function (chunk, encoding, callback) {
  27. callback()
  28. }
  29. })
  30. const rl = readline.createInterface({
  31. input: process.stdin,
  32. output: mutableStdout,
  33. terminal: true
  34. })
  35. console.log('New password?')
  36. rl.on('line', function (password) {
  37. if (!isUserPasswordValid(password)) {
  38. console.error('New password is invalid.')
  39. process.exit(-1)
  40. }
  41. user.password = password
  42. user.save()
  43. .then(() => console.log('User password updated.'))
  44. .catch(err => console.error(err))
  45. .finally(() => process.exit(0))
  46. })
  47. })