reset-password.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { initDatabaseModels } from '../server/initializers/database'
  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('Unknown user.')
  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. })
  48. .catch(err => {
  49. console.error(err)
  50. process.exit(-1)
  51. })