create-user.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import minimist from 'minimist'
  2. import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
  3. import UserRegistrationHandler from '../../../app/src/Features/User/UserRegistrationHandler.js'
  4. async function main() {
  5. await waitForDb()
  6. const argv = minimist(process.argv.slice(2), {
  7. string: ['email'],
  8. boolean: ['admin'],
  9. })
  10. const { admin, email } = argv
  11. if (!email) {
  12. console.error(`Usage: node ${__filename} [--admin] --email=joe@example.com`)
  13. process.exit(1)
  14. }
  15. await new Promise((resolve, reject) => {
  16. UserRegistrationHandler.registerNewUserAndSendActivationEmail(
  17. email,
  18. (error, user, setNewPasswordUrl) => {
  19. if (error) {
  20. return reject(error)
  21. }
  22. db.users.updateOne(
  23. { _id: user._id },
  24. { $set: { isAdmin: admin } },
  25. error => {
  26. if (error) {
  27. return reject(error)
  28. }
  29. console.log('')
  30. console.log(`\
  31. Successfully created ${email} as ${admin ? 'an admin' : 'a'} user.
  32. Please visit the following URL to set a password for ${email} and log in:
  33. ${setNewPasswordUrl}
  34. `)
  35. resolve()
  36. }
  37. )
  38. }
  39. )
  40. })
  41. }
  42. main()
  43. .then(() => {
  44. console.error('Done.')
  45. process.exit(0)
  46. })
  47. .catch(err => {
  48. console.error(err)
  49. process.exit(1)
  50. })