trails/apps/journal/app/routes/api.sync.connect.$provider.ts
Ullrich Schäfer 6f366293c9
journal: OAuth connect→callback→resume lifecycle as one module
The PKCE verifier cookie, state encoding, redirect-URI construction,
and code exchange were coordinated across three route handlers that
each knew part of the protocol. Two latent bugs lived in the gaps: a
push-initiated re-authorization skipped PKCE entirely (harmless today
only because the sole pusher, Wahoo, is not a PKCE provider), and the
push-resume callback path never cleared the spent verifier cookie.

oauth-flow.server.ts now owns the lifecycle: initiateOAuthFlow builds
the provider redirect (state + verifier cookie, on every entry path),
and completeOAuthFlow consumes the callback — state decode, verifier
recovery, code exchange, connection linking — returning a
discriminated result the route maps to redirects. The three routes are
thin adapters; the next OAuth provider reuses the flow, not the
pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 07:44:04 +02:00

16 lines
655 B
TypeScript

import { data } from "react-router";
import type { Route } from "./+types/api.sync.connect.$provider";
import { requireSessionUser } from "~/lib/auth/session.server";
import { getManifest } from "~/lib/connected-services";
import { initiateOAuthFlow } from "~/lib/connected-services/oauth-flow.server";
export async function loader({ params, request }: Route.LoaderArgs) {
await requireSessionUser(request);
const manifest = getManifest(params.provider);
if (!manifest || !manifest.buildAuthUrl) {
return data({ error: "Unknown provider" }, { status: 404 });
}
return initiateOAuthFlow(manifest, { returnTo: "/settings/connections" });
}