How to use the http-errors.TooManyRequests function in http-errors

To help you get started, we’ve selected a few http-errors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fega / mongo-server / lib / middleware / magicLinks.js View on Github external
/**
   * Get configuration
   */
  const conf = getConfig(config.resources[resourceName].auth.magicLink);
  const MagicTokens = db.collection(conf.collection);
  const User = db.collection(resourceName);

  /**
   * Verify email and tokens
   */
  const email = (req.body[conf.emailField] || '').toLowerCase();
  if (!email) throw new HttpError.BadRequest('Missing Email');
  if (!isEmail(email)) throw new HttpError.BadRequest('Invalid Email');
  const tokens = await MagicTokens.find({ email, exp: { $gte: new Date() } }).toArray();
  if (tokens.length >= conf.max) throw new HttpError.TooManyRequests('Token limit reached');

  /**
    * Handle custom logic
    */
  const user = await User.findOne({ [conf.emailField]: email });
  const _id = !user ? new ObjectId().toString():false;
  if (conf.doGenerate) {
    await conf.doGenerate({
      req, res, next, user, HttpError, db, resourceName, newUserId: _id,
    });
  }
  /**
    * Create and  store token, also create user if it didnt exist
    */
  const token = generate(50) + new ObjectId().toString();
  const search = generate(50) + new ObjectId().toString();
github fastify / fastify-sensible / lib / httpErrors.js View on Github external
tooManyRequests: function tooManyRequests (message) {
    return new createError.TooManyRequests(message)
  },
github fega / mongo-server / lib / middleware / magicCode.js View on Github external
/**
     * Get configuration
     */
  const conf = getConfig(config.resources[resourceName].auth.magicCode);
  const MagicTokens = db.collection(conf.collection);
  const User = db.collection(resourceName);

  /**
   * Verify email and tokens
   */
  const email = (req.body[conf.emailField] || '').toLowerCase();
  if (!email) throw new HttpError.BadRequest('Missing Email');
  if (!isEmail(email)) throw new HttpError.BadRequest('Invalid Email');
  const tokens = await MagicTokens.find({ email, exp: { $gte: new Date() } }).toArray();
  if (tokens.length >= conf.max) throw new HttpError.TooManyRequests('Token limit reached');

  /**
   * Handle custom logic
   */
  const user = await User.findOne({ [conf.emailField]: email });
  const _id = !user ? new ObjectId().toString():false;
  if (conf.doGenerate) {
    await conf.doGenerate({
      req, res, next, user, HttpError, db, resourceName, newUserId: _id,
    });
  }
  /**
     * Create and  store token, also create user if it didnt exist
     */
  const token = generate({
    length: 4,