{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package authflow\n\nPackage authflow is the browser login flow shared by \\`memcode login\\` (cobra) and the TUI's /login slash command. A local HTTP server on 127.0.0.1:19090 receives the token from the web app's /api/cli/auth callback redirect; the caller persists it via WriteGlobalEnvToken so the existing LoadDotEnv → provider path picks it up.\n\n## Constants\n\n```go\nconst DefaultGatewayURL = provider.DefaultAPIURL\n```\nDefaultGatewayURL is the production memcode gateway. The web app's /api/cli/auth may override this by returning api\\_url in the callback, but this default lets login work even when the callback doesn't include it.\n\n```go\nconst DefaultWebAppURL = \"https://www.memcode.ai\"\n```\nDefaultWebAppURL is where the browser opens to authenticate. Use the www host directly: the apex (memcode.ai) 308-redirects to www, and that extra cross-host hop on the /api/cli/auth?port=\u0026state= URL is needless churn on a load-bearing flow. Point straight at the canonical host.\n\n## Functions\n\n```go\nfunc OpenBrowser(url string) error\n```\nOpenBrowser opens url in the user's default browser. Best-effort — callers always print the URL too, so a headless session still works.\n\n```go\nfunc SetGlobalEnv(kv map[string]string) error\n```\nSetGlobalEnv writes key=value pairs into the global env file (~/.config/memcode/.env), replacing any existing lines for those keys and leaving everything else intact. The wizard uses it to persist a credential choice (a selected subscription source, an own key, an endpoint). Values are written verbatim; callers pass already-trimmed values.\n\n```go\nfunc StripGlobalEnvToken() (bool, error)\n```\nStripGlobalEnvToken removes the token/url lines from the global env file (logout). Returns whether anything was removed; a missing file is a no-op. The token stays valid server-side until revoked from the web app (a /api/cli/revoke endpoint is a follow-up).\n\n```go\nfunc WriteGlobalEnvToken(token, apiURL string) error\n```\nWriteGlobalEnvToken writes MEMCODE\\_API\\_TOKEN and MEMCODE\\_API\\_URL into the global env file (~/.config/memcode/.env), creating it if needed and replacing any existing values for those keys (no-override is for LoadDotEnv; login is the explicit user action that SHOULD overwrite).\n\n## Types\n\n```go\ntype Identity struct {\n\tEmail       string `json:\"email\"`\n\tDisplayName string `json:\"display_name\"`\n\tUsername    string `json:\"username\"`\n\tOrg         struct {\n\t\tID   string `json:\"id\"`\n\t\tName string `json:\"name\"`\n\t\tSlug string `json:\"slug\"`\n\t\tPlan string `json:\"plan\"`\n\t} `json:\"org\"`\n}\n```\nIdentity is the signed-in account, from GET {webAppURL}/api/cli/whoami.\n\n```go\nfunc Whoami(ctx context.Context, token string) (Identity, error)\n```\nWhoami asks the web app who a memcode\\_ bearer token belongs to. Network-bound; pass a bounded context.\n\n```go\ntype Result struct {\n\tToken      string\n\tGatewayURL string\n\tEmail      string // the signed-in account email, when the web app supplies it\n}\n```\nResult is a successful login: the minted org key and the gateway to use it against.\n\n```go\nfunc Run(ctx context.Context, status func(string)) (Result, error)\n```\nRun executes the browser login flow: starts the local callback server, opens the browser, and waits for the redirect. status (nil-safe) receives progress lines for display. Cancel ctx to abort (the TUI's Esc); an internal 2-minute timeout applies regardless. Run does NOT persist anything — callers decide (WriteGlobalEnvToken).\n\n","path":"github.com/memcode-ai/memcode/internal/authflow","name":"authflow","synopsis":"Package authflow is the browser login flow shared by `memcode login` (cobra) and the TUI's /login slash command.","isRedistributable":true}
