callerNow

tcmts.share.callerNow · #t7j4vt2y · term · runs on the server

uses process

Who the request is from: the token its cookie holds (the server's, its maintainer's; a contributor's), or none, a reader (on a public server). Undefined when the server is private and there's no token. With no server token at all (tcm dev on a codebase of your own), everyone is its maintainer.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { callerOf, serverUsers } from "#codebase/tcmts/cli/sync";
import type { Caller } from "#codebase/tcmts/cli/sync";
import { cookie } from "#codebase/tcmts/server";
import { TOKEN_COOKIE } from "#codebase/tcmts/share";

export function callerNow(cb: Codebase): Caller | undefined {
  const token = process.env.TCM_SHARE_TOKEN;
  if (!token) return { role: "maintainer" };
  const kept = cookie(TOKEN_COOKIE);
  const who = kept ? callerOf(serverUsers(cb), `Bearer ${kept}`, token, false) : undefined;
  return who ?? (process.env.TCM_SHARE_PUBLIC === "1" ? { role: "reader" } : undefined);
}

uses

used by