caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

76 lines
2.7 KiB
TypeScript

/**
* OAuth connect flow for the Connections screen.
*
* The backend mints an opaque `authorize_url` (which already embeds a single-use
* CSRF `state`); we open it in an in-app auth session and wait for the provider
* to redirect back to our app deep link with `code` + the echoed `state`, then
* hand those to the token-free callback to finish the handshake.
*
* SECURITY: nothing sensitive lives here. No access/refresh token is ever
* requested, parsed, or returned — the callback yields only the token-free
* connection list. The only value we forward is the opaque `code` + `state`.
*/
import * as Linking from "expo-linking";
import * as WebBrowser from "expo-web-browser";
import { completeConnect, startConnect } from "@/lib/api";
import type { Connection, SocialProvider } from "@/lib/types";
/** Thrown when the user dismisses/cancels the in-app auth browser. */
export class ConnectCancelledError extends Error {
constructor(message = "Connection cancelled.") {
super(message);
this.name = "ConnectCancelledError";
}
}
/** Narrow a `string | string[] | undefined` query param to its first string. */
function firstParam(value: string | string[] | undefined): string {
if (Array.isArray(value)) return typeof value[0] === "string" ? value[0] : "";
return typeof value === "string" ? value : "";
}
/**
* Runs the full connect handshake for `provider` and resolves with the
* refreshed, token-free connection list on success.
*
* Throws {@link ConnectCancelledError} if the user backs out of the browser,
* and a plain Error (with a user-facing message) if the provider returns an
* error or no authorization code.
*/
export async function connectProvider(
provider: SocialProvider | string,
token: string | undefined,
scopes?: string,
): Promise<Connection[]> {
const info = await startConnect(provider, { token, scopes });
// The deep link the provider should redirect back to once the user approves.
const returnUrl = Linking.createURL("connections");
const result = await WebBrowser.openAuthSessionAsync(
info.authorize_url,
returnUrl,
);
if (result.type !== "success" || !result.url) {
throw new ConnectCancelledError();
}
const { queryParams } = Linking.parse(result.url);
const providerError = firstParam(queryParams?.error);
if (providerError) {
throw new Error(`The provider reported an error: ${providerError}.`);
}
const code = firstParam(queryParams?.code);
// Prefer the state the provider echoed back; fall back to the minted one.
const state = firstParam(queryParams?.state) || info.state;
if (!code) {
throw new Error("The provider did not return an authorization code.");
}
return completeConnect(provider, code, state, { token });
}