text: module.exports = {
    friendlyName: 'Update password and login',
  
    description: 'Finish the password recovery flow by setting the new password',
  
    inputs: {
      email: {
        description: 'Users email.',
        example: 'tomer@thegoodparty.org',
        isEmail: true,
        type: 'string',
      },
      phone: {
        type: 'string',
      },
      password: {
        description: 'The new, unencrypted password.',
        example: 'abc123v2',
        type: 'string',
        required: true,
        minLength: 8,
      },
  
      token: {
        description:
          'The password token that was generated by the `sendPasswordRecoveryEmail` endpoint.',
        example: 'gwa8gs8hgw9h2g9hg29hgw',
        required: true,
        type: 'string',
      },
    },
  
    exits: {
      success: {
        description: 'Password successfully updated.',
      },
  
      invalidToken: {
        description:
          'The provided password token is invalid, expired, or has already been used.',
        responseType: 'expired',
      },
      badRequest: {
        description: 'register Failed',
        responseType: 'badRequest',
      },
    },
  
    fn: async function(inputs, exits) {
      try {
        const { email, phone, password, token } = inputs;
        if (!email && !phone) {
          return exits.badRequest({ message: 'phone or email are required' });
        }
        if (!token) {
          return exits.badRequest({
            message: 'Token Expired.',
            expired: true,
          });
        }
        let user;
        if (email) {
          const lowerCaseEmail = email.toLowerCase();
          user = await User.findOne({
            email: lowerCaseEmail,
            passwordResetToken: token,
          });
        } else {
          user = await User.findOne({ phone, passwordResetToken: token });
        }
  
        // If no such user exists, or their token is expired, bail.
        if (!user || user.passwordResetTokenExpiresAt <= Date.now()) {
          return exits.badRequest({
            message: 'Token Expired.',
            expired: true,
          });
        }
  
        // Hash the new password.
        const hashed = await sails.helpers.passwords.hashPassword(password);
  
        // Store the user's new password and clear their reset token so it can't be used again.
        await User.updateOne({ id: user.id }).set({
          password: hashed,
          hasPassword: true,
          passwordResetToken: '',
          passwordResetTokenExpiresAt: 0,
        });
  
        return exits.success({
          message: 'Password successfully updated.',
        });
      } catch (e) {
        await sails.helpers.errorLoggerHelper(
          'Error at entrance/reset-password',
          e,
        );
        console.log('reset-password error', e);
        return exits.badRequest({ message: 'Error resetting password.' });
      }
    },
  };
  