Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const nodemailer = require('nodemailer');
- module.exports = {
- friendlyName: 'Signup',
- description: 'Sign up for a new user account.',
- extendedDescription:
- `This creates a new user record in the database for a customer-level user, signs in the requesting user agent
- by modifying its [session](https://sailsjs.com/documentation/concepts/sessions), and
- sends an account verification email.
- If a verification email is sent, the new user's account is put in an "unconfirmed" state
- until they confirm they are using a legitimate email address (by clicking the link in
- the account verification message.)`,
- inputs: {
- emailAddress: {
- required: true,
- type: 'string',
- isEmail: true,
- description: 'The email address for the new account, e.g. m@example.com.',
- extendedDescription: 'Must be a valid email address.',
- },
- password: {
- required: true,
- type: 'string',
- maxLength: 200,
- example: 'passwordlol',
- description: 'The unencrypted password to use for the new account.'
- },
- fullName: {
- required: true,
- type: 'string',
- example: 'Frida Kahlo de Rivera',
- description: 'The user\'s full name.',
- },
- customerToken : {
- required: true,
- type: 'string',
- example: 'hNgYlKkhraNV',
- description: 'Token used to identify the to be linked to customer'
- }
- },
- exits: {
- invalid: {
- responseType: 'badRequest',
- description: 'The provided fullName, password and/or email address are invalid.',
- extendedDescription: 'If this request was sent from a graphical user interface, the request '+
- 'parameters should have been validated/coerced _before_ they were sent.'
- },
- conflict: {
- statusCode: 409,
- description: 'Generic conflict'
- }
- },
- fn: async function (inputs, exits) {
- // Check if the provided customertoken does exist.
- const foundCustomer = await Customer.findOne({ referenceToken: inputs.customerToken });
- if (!foundCustomer) {
- return exits.conflict({ property: 'customerToken', message: 'Invalid token' });
- }
- // Build up data for the new user record and save it to the database
- let newUser = await User.create({
- emailAddress: inputs.emailAddress.toLowerCase(),
- password: await sails.helpers.password.hash(inputs.password).toString(),
- fullName: inputs.fullName,
- customer: foundCustomer.id,
- role: 'customer',
- emailProofToken: await sails.helpers.string.random(32),
- emailProofTokenExpiresAt: Date.now() + 24*60*60*1000,
- emailStatus: 'unconfirmed'
- })
- .intercept({ code: 'E_UNIQUE' }, (err) => {
- sails.log.debug(err);
- return exits.conflict({ code: 'E_UNIQUE', attrNames: attrNames });
- })
- .intercept({ name: 'UsageError' }, () => exits.invalid())
- .fetch();
- // Store the user's new id in their session.
- this.req.session.userId = newUser.id;
- // Send confirm account email.
- const transporter = nodemailer.createTransport(sails.config.custom.nodeMailer);
- const message = {
- from: 'info@lesage-nv.be',
- to: inputs.emailAddress.toLowerCase(),
- subject: 'Message title',
- text: 'Plaintext version of the message',
- html: '<p>HTML version of the message</p>'
- };
- await transporter.sendMail(message);
- return exits.success();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement