signIn

tcmts.share.signIn · #ipb3czov · term · runs on the server

uses process

Sign this browser in with a token (a contributor's, or the server's): kept as a cookie. Says who it is.

import { callerOf, serverUsers } from "#codebase/tcmts/cli/sync";
import { HttpError, setCookie } from "#codebase/tcmts/server";
import { servedCodebase, TOKEN_COOKIE } from "#codebase/tcmts/share";

export async function signIn(token: string): Promise<string> {
  const { cb } = servedCodebase();
  const server = process.env.TCM_SHARE_TOKEN;
  const who = server
    ? callerOf(serverUsers(cb), `Bearer ${token}`, server, false)
    : { role: "maintainer" as const };
  if (!who) throw new HttpError(401, "This server doesn't take that token.");
  setCookie(TOKEN_COOKIE, token, { maxAge: 60 * 60 * 24 * 365, sameSite: "Strict", path: "/" });
  return who.role === "contributor" ? `@${who.handle}` : "maintainer";
}

uses

used by