All files / src/lib user-uid.ts

88.23% Statements 15/17
88.88% Branches 8/9
66.66% Functions 2/3
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251x       24x 24x 24x 24x 24x 24x     1x 24x 24x 4x 4x 20x 20x     1x      
import { AppError } from "@ontrack/backend-common";
import type { FastifyRequest } from "fastify";
 
/** Firebase uid, проброшенный gateway-ем в заголовке `X-User-Uid` (за X-Service-Token). */
function rawUserUid(request: FastifyRequest): string | undefined {
  const raw = request.headers["x-user-uid"];
  const value = Array.isArray(raw) ? raw[0] : raw;
  const trimmed = value?.trim();
  return trimmed && trimmed.length > 0 ? trimmed : undefined;
}
 
/** uid обязателен (профиль и т.п.) — иначе 401. */
export function readUserUid(request: FastifyRequest): string {
  const uid = rawUserUid(request);
  if (!uid) {
    throw new AppError(401, "USER_REQUIRED", "Missing user identity");
  }
  return uid;
}
 
/** uid опционален (напр. проставить владельца при загрузке трека). */
export function readOptionalUserUid(request: FastifyRequest): string | undefined {
  return rawUserUid(request);
}