addServerUser

tcmts.cli.sync.addServerUser · #bf76glyx · term · runs on the server

uses node:crypto

Add a contributor to a served codebase, or give one a new token (the old one stops working): the token, shown once.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { HANDLE, serverUsers, tokenDigest, USERS_SETTING } from "#codebase/tcmts/cli/sync";
import { randomBytes } from "node:crypto";

export function addServerUser(cb: Codebase, handle: string): string {
  const h = handle.replace(/^@/, "");
  if (!HANDLE.test(h))
    throw new Error(
      `'${handle}' isn't a handle: letters, digits and dashes, starting with a letter or digit`,
    );
  const token = randomBytes(18).toString("base64url");
  const others = serverUsers(cb).filter((u) => u.handle !== h);
  cb.store.setSetting(
    USERS_SETTING,
    JSON.stringify([...others, { handle: h, digest: tokenDigest(token), added: Date.now() }]),
  );
  return token;
}

uses

used by