makeAuthMiddleware (dashboard/auth.ts)

Patter · libraries/typescript/src/dashboard/auth.ts · typescript · MIT
auth, Auth / Identity

Express middleware that protects dashboard routes using a static bearer token or query parameter.

// Source: https://github.com/PatterAI/Patter | License: MIT | Copyright 2026 Patter Contributors
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Dashboard authentication middleware for Express.
 *
 * When a token is configured, requests must include either:
 * - Authorization: Bearer <token> header
 * - ?token=<token> query parameter
 */

import type { Request, Response, NextFunction } from 'express';
import crypto from 'node:crypto';

function timingSafeCompare(a: string, b: string): boolean {
  const aBuf = Buffer.from(a);
  const bBuf = Buffer.from(b);
  if (aBuf.length !== bBuf.length) {
    // Still run a timingSafeEqual to keep the code path timing uniform.
    crypto.timingSafeEqual(aBuf, aBuf);
    return false;
  }
  return crypto.timingSafeEqual(aBuf, bBu

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

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

Get a free API key