Provider-agnostic sync framework + Wahoo as first implementation: Framework (apps/journal/app/lib/sync/): - SyncProvider interface for OAuth2, webhooks, import, conversion - Provider registry for settings UI iteration - Generic sync_connections + sync_imports tables - Token storage with auto-refresh Wahoo provider: - OAuth2 with workouts_read, user_read, offline_data scopes - Webhook-based auto-import (workout_summary events) - Manual import page with pagination - FIT→GPX conversion via fit-file-parser - Webhook token verification Routes: - /api/sync/connect/:provider — OAuth redirect - /api/sync/callback/:provider — OAuth callback - /api/sync/disconnect/:provider — remove connection - /api/sync/webhook/:provider — webhook receiver - /sync/import/:provider — manual import page Settings: Connected Services section with per-provider connect/disconnect Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
30 lines
1.2 KiB
TypeScript
30 lines
1.2 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);
|
|
await saveConnection(user.id, provider.id, tokens);
|
|
} catch (e) {
|
|
console.error(`OAuth callback failed for ${params.provider}:`, e);
|
|
return redirect("/settings?error=sync_failed");
|
|
}
|
|
|
|
return redirect("/settings");
|
|
}
|