Extends the SyncProvider interface with an optional pushRoute method plus PushRoutePayload, PushRouteResult, and a typed PushError so the slice-4 action route can map error codes to user-facing copy without parsing HTTP statuses itself. Wahoo provider: - adds routes_write to the requested OAuth scope set - implements pushRoute: base64-encodes the FIT, builds the form body Wahoo's POST /v1/routes expects, parses the remote id out of the response, and throws typed PushError on 401/403/422/429/5xx - saveConnection now persists the requested scopes as grantedScopes on exchangeCode (Wahoo doesn't return a scope field, so the requested set is the granted set) Token refresh on 401 is deferred to the slice-4 action route — same pattern as the existing webhook handler in this repo, which does inline refresh rather than wrapping every call. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
import { redirect, data } from "react-router";
|
|
import type { Route } from "./+types/api.sync.callback.$provider";
|
|
import { getSessionUser } from "~/lib/auth.server";
|
|
import { getProvider } from "~/lib/sync/registry";
|
|
import { saveConnection } from "~/lib/sync/connections.server";
|
|
|
|
export async function loader({ params, request }: Route.LoaderArgs) {
|
|
const user = await getSessionUser(request);
|
|
if (!user) return redirect("/auth/login");
|
|
|
|
const provider = getProvider(params.provider);
|
|
if (!provider) return data({ error: "Unknown provider" }, { status: 404 });
|
|
|
|
const url = new URL(request.url);
|
|
const code = url.searchParams.get("code");
|
|
if (!code) return data({ error: "Missing authorization code" }, { status: 400 });
|
|
|
|
const origin = process.env.ORIGIN ?? "http://localhost:3000";
|
|
const redirectUri = `${origin}/api/sync/callback/${params.provider}`;
|
|
|
|
try {
|
|
const tokens = await provider.exchangeCode(code, redirectUri);
|
|
// Wahoo's token endpoint doesn't return a `scope` field and grants
|
|
// scopes all-or-nothing, so the requested set is the granted set.
|
|
await saveConnection(user.id, provider.id, tokens, provider.scopes);
|
|
} catch (e) {
|
|
console.error(`OAuth callback failed for ${params.provider}:`, e);
|
|
return redirect("/settings?error=sync_failed");
|
|
}
|
|
|
|
return redirect("/settings");
|
|
}
|