signup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. module.exports = {
  2. friendlyName: 'Signup',
  3. description: 'Sign up for a new user account.',
  4. extendedDescription:
  5. `This creates a new user record in the database, signs in the requesting user agent
  6. by modifying its [session](https://sailsjs.com/documentation/concepts/sessions), and
  7. (if emailing with Mailgun is enabled) sends an account verification email.
  8. If a verification email is sent, the new user's account is put in an "unconfirmed" state
  9. until they confirm they are using a legitimate email address (by clicking the link in
  10. the account verification message.)`,
  11. inputs: {
  12. emailAddress: {
  13. required: true,
  14. type: 'string',
  15. isEmail: true,
  16. description: 'The email address for the new account, e.g. m@example.com.',
  17. extendedDescription: 'Must be a valid email address.',
  18. },
  19. password: {
  20. required: true,
  21. type: 'string',
  22. maxLength: 200,
  23. example: 'passwordlol',
  24. description: 'The unencrypted password to use for the new account.'
  25. },
  26. fullName: {
  27. required: true,
  28. type: 'string',
  29. example: 'Frida Kahlo de Rivera',
  30. description: 'The user\'s full name.',
  31. }
  32. },
  33. exits: {
  34. invalid: {
  35. responseType: 'badRequest',
  36. description: 'The provided fullName, password and/or email address are invalid.',
  37. extendedDescription: 'If this request was sent from a graphical user interface, the request '+
  38. 'parameters should have been validated/coerced _before_ they were sent.'
  39. },
  40. emailAlreadyInUse: {
  41. statusCode: 409,
  42. description: 'The provided email address is already in use.',
  43. },
  44. },
  45. fn: async function (inputs, exits) {
  46. var newEmailAddress = inputs.emailAddress.toLowerCase();
  47. // Build up data for the new user record and save it to the database.
  48. // (Also use `fetch` to retrieve the new ID so that we can use it below.)
  49. var newUserRecord = await User.create(Object.assign({
  50. emailAddress: newEmailAddress,
  51. password: await sails.helpers.passwords.hashPassword(inputs.password),
  52. fullName: inputs.fullName,
  53. tosAcceptedByIp: this.req.ip
  54. }, sails.config.custom.verifyEmailAddresses? {
  55. emailProofToken: await sails.helpers.strings.random('url-friendly'),
  56. emailProofTokenExpiresAt: Date.now() + sails.config.custom.emailProofTokenTTL,
  57. emailStatus: 'unconfirmed'
  58. }:{}))
  59. .intercept('E_UNIQUE', 'emailAlreadyInUse')
  60. .intercept({name: 'UsageError'}, 'invalid')
  61. .fetch();
  62. // If billing feaures are enabled, save a new customer entry in the Stripe API.
  63. // Then persist the Stripe customer id in the database.
  64. if (sails.config.custom.enableBillingFeatures) {
  65. let stripeCustomerId = await sails.helpers.stripe.saveBillingInfo.with({
  66. emailAddress: newEmailAddress
  67. });
  68. await User.update(newUserRecord.id).set({
  69. stripeCustomerId
  70. });
  71. }
  72. // Store the user's new id in their session.
  73. this.req.session.userId = newUserRecord.id;
  74. if (sails.config.custom.verifyEmailAddresses) {
  75. // Send "confirm account" email
  76. await sails.helpers.sendTemplateEmail.with({
  77. to: newEmailAddress,
  78. subject: 'Please confirm your account',
  79. template: 'email-verify-account',
  80. templateData: {
  81. fullName: inputs.fullName,
  82. token: newUserRecord.emailProofToken
  83. }
  84. });
  85. } else {
  86. sails.log.info('Skipping new account email verification... (since `verifyEmailAddresses` is disabled)');
  87. }
  88. // Since everything went ok, send our 200 response.
  89. return exits.success();
  90. }
  91. };