Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

864 B
Raw Blame
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
import { checkRateLimitForApiKey } from '../../common/utils'
import {
  getEffectiveAccountLimits,
  PlanCode,
} from '../../user/entities/user.entity'

@Injectable()
export class ApiKeyRateLimitGuard implements CanActivate {
  async canActivate(context: ExecutionContext) {
    const request = context.switchToHttp().getRequest()
    const { user } = request

    if (user?.apiKeyAccess) {
      if (!user) return false

      const { apiRateLimitPerHour } = getEffectiveAccountLimits(user)
      const reqAmount =
        user.planCode === PlanCode.none ||
        user.isAccountBillingSuspended ||
        user.dashboardBlockReason !== null
          ? 0
          : apiRateLimitPerHour
      return checkRateLimitForApiKey(`account:${user.id}`, reqAmount)
    }

    return true
  }
}