ApiGuard (middlewares/api.guard.ts)

trench · apps/trench/src/middlewares/api.guard.ts · typescript · MIT
auth

NestJS guard that validates API keys and attaches workspace to requests.

// Source: https://github.com/FrigadeHQ/trench | License: MIT | Copyright 2024 Frigade Inc
// Provided via justclaw.it.com; keep this notice with the code.
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'
import { Observable } from 'rxjs'
import { ApiKeysService } from 'src/api-keys/api-keys.service'
import { ApiKeyType } from 'src/api-keys/api-keys.interface'

export class ApiGuard implements CanActivate {
  constructor(
    protected readonly apiKeysService: ApiKeysService,
    protected readonly apiKeyType: ApiKeyType
  ) {}

  canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
    return this.validateRequest(context)
  }

  async validateRequest(context: ExecutionContext): Promise<boolean> {
    co

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key