Spaces:
Running
Running
Download tui/src/flows/auth.ts from Leon4gr45/brain: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/tui/src/flows/auth.ts
- Command line
-
hf download hf://spaces/Leon4gr45/brain/tui/src/flows/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/tui/src/flows/auth.ts
2.09 kB
| import { randomBytes } from "node:crypto"; | |
| import * as p from "@clack/prompts"; | |
| import pc from "picocolors"; | |
| import { askPassword, isPromptBack, pickOne, type PromptBack } from "../lib/prompts.js"; | |
| import type { AuthChoices } from "../types.js"; | |
| function generateToken(): string { | |
| return `brainpat_${randomBytes(24).toString("hex")}`; | |
| } | |
| export async function askAuth(opts?: { | |
| allowBack?: false; | |
| prechosenToken?: string; | |
| }): Promise<AuthChoices>; | |
| export async function askAuth(opts: { | |
| allowBack: true; | |
| backHint?: string; | |
| prechosenToken?: string; | |
| }): Promise<AuthChoices | PromptBack>; | |
| export async function askAuth(opts?: { | |
| allowBack?: boolean; | |
| backHint?: string; | |
| prechosenToken?: string; | |
| }): Promise<AuthChoices | PromptBack> { | |
| if (opts?.prechosenToken) { | |
| return { brainpatToken: opts.prechosenToken }; | |
| } | |
| p.log.step("Authentication token"); | |
| const action = opts?.allowBack | |
| ? await pickOne<"generate" | "paste">({ | |
| message: "BRAINPAT_TOKEN (used by the API for personal auth)", | |
| options: [ | |
| { value: "generate", label: "Generate one for me" }, | |
| { value: "paste", label: "Paste my own" }, | |
| ], | |
| initialValue: "generate", | |
| allowBack: true, | |
| backHint: opts.backHint, | |
| }) | |
| : await pickOne<"generate" | "paste">({ | |
| message: "BRAINPAT_TOKEN (used by the API for personal auth)", | |
| options: [ | |
| { value: "generate", label: "Generate one for me" }, | |
| { value: "paste", label: "Paste my own" }, | |
| ], | |
| initialValue: "generate", | |
| }); | |
| if (isPromptBack(action)) { | |
| return action; | |
| } | |
| if (action === "generate") { | |
| const token = generateToken(); | |
| p.note(token, "Generated token (saved to .env)"); | |
| p.log.info(pc.dim("Keep this somewhere safe — it grants API access.")); | |
| return { brainpatToken: token }; | |
| } | |
| const pasted = await askPassword({ | |
| message: "Paste your BRAINPAT_TOKEN", | |
| validate: (value) => (value.trim().length === 0 ? "Token is required" : undefined), | |
| }); | |
| return { brainpatToken: pasted.trim() }; | |
| } | |