Advertisement
Guest User

Untitled

a guest
Dec 31st, 2017
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nodemailer = require('nodemailer');
  2.  
  3. module.exports = {
  4.  
  5.  
  6.   friendlyName: 'Signup',
  7.  
  8.  
  9.   description: 'Sign up for a new user account.',
  10.  
  11.  
  12.   extendedDescription:
  13. `This creates a new user record in the database for a customer-level user, signs in the requesting user agent
  14. by modifying its [session](https://sailsjs.com/documentation/concepts/sessions), and
  15. sends an account verification email.
  16.  
  17. If a verification email is sent, the new user's account is put in an "unconfirmed" state
  18. until they confirm they are using a legitimate email address (by clicking the link in
  19. the account verification message.)`,
  20.  
  21.  
  22.  inputs: {
  23.    emailAddress: {
  24.      required: true,
  25.      type: 'string',
  26.      isEmail: true,
  27.      description: 'The email address for the new account, e.g. m@example.com.',
  28.      extendedDescription: 'Must be a valid email address.',
  29.    },
  30.  
  31.    password: {
  32.      required: true,
  33.      type: 'string',
  34.      maxLength: 200,
  35.      example: 'passwordlol',
  36.      description: 'The unencrypted password to use for the new account.'
  37.    },
  38.  
  39.    fullName:  {
  40.      required: true,
  41.      type: 'string',
  42.      example: 'Frida Kahlo de Rivera',
  43.      description: 'The user\'s full name.',
  44.     },
  45.  
  46.     customerToken : {
  47.       required: true,
  48.       type: 'string',
  49.       example: 'hNgYlKkhraNV',
  50.       description: 'Token used to identify the to be linked to customer'
  51.     }
  52.  
  53.   },
  54.  
  55.  
  56.   exits: {
  57.  
  58.     invalid: {
  59.       responseType: 'badRequest',
  60.       description: 'The provided fullName, password and/or email address are invalid.',
  61.       extendedDescription: 'If this request was sent from a graphical user interface, the request '+
  62.       'parameters should have been validated/coerced _before_ they were sent.'
  63.     },
  64.  
  65.     conflict: {
  66.       statusCode: 409,
  67.       description: 'Generic conflict'
  68.     }
  69.  
  70.   },
  71.  
  72.  
  73.   fn: async function (inputs, exits) {
  74.  
  75.     // Check if the provided customertoken does exist.
  76.  
  77.     const foundCustomer = await Customer.findOne({ referenceToken: inputs.customerToken });
  78.  
  79.     if (!foundCustomer) {
  80.       return exits.conflict({ property: 'customerToken', message: 'Invalid token' });
  81.     }
  82.  
  83.     // Build up data for the new user record and save it to the database
  84.     let newUser = await User.create({
  85.       emailAddress: inputs.emailAddress.toLowerCase(),
  86.       password: await sails.helpers.password.hash(inputs.password).toString(),
  87.       fullName: inputs.fullName,
  88.       customer: foundCustomer.id,
  89.       role: 'customer',
  90.       emailProofToken: await sails.helpers.string.random(32),
  91.       emailProofTokenExpiresAt: Date.now() + 24*60*60*1000,
  92.       emailStatus: 'unconfirmed'
  93.     })
  94.     .intercept({ code: 'E_UNIQUE' }, (err) => {
  95.       sails.log.debug(err);
  96.       return exits.conflict({ code: 'E_UNIQUE', attrNames: attrNames });
  97.     })
  98.     .intercept({ name: 'UsageError' }, () => exits.invalid())
  99.     .fetch();
  100.  
  101.     // Store the user's new id in their session.
  102.     this.req.session.userId = newUser.id;
  103.  
  104.     // Send confirm account email.
  105.     const transporter = nodemailer.createTransport(sails.config.custom.nodeMailer);
  106.     const message = {
  107.       from: 'info@lesage-nv.be',
  108.       to: inputs.emailAddress.toLowerCase(),
  109.       subject: 'Message title',
  110.       text: 'Plaintext version of the message',
  111.       html: '<p>HTML version of the message</p>'
  112.     };
  113.  
  114.     await transporter.sendMail(message);
  115.  
  116.     return exits.success();
  117.  
  118.   }
  119.  
  120.  
  121. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement