···44 };
4546 // Step 1: Verify OAuth session is valid
47- await step.run("verify-oauth-session", async () => {
48 const result = await restoreOAuthSession(did);
49 if (!result.ok) {
50- throw new Error(
51- `Failed to restore OAuth session: ${result.error.message}`,
52- );
00000053 }
54 return { success: true };
55 });
000000000005657 // Step 2: Get user's pub.leaflet.publication records
58 const oldPublications = await step.run(
···471 // 2. External references (e.g., from other AT Proto apps) to old URIs continue to work
472 // 3. The normalization layer handles both schemas transparently for reads
473 // Old records are also kept on the user's PDS so existing AT-URI references remain valid.
0000000000474475 return {
476 success: stats.errors.length === 0,
···44 };
4546 // Step 1: Verify OAuth session is valid
47+ const oauthValid = await step.run("verify-oauth-session", async () => {
48 const result = await restoreOAuthSession(did);
49 if (!result.ok) {
50+ // Mark identity as needing migration so we can retry later
51+ await supabaseServerClient
52+ .from("identities")
53+ .update({
54+ metadata: { needsStandardSiteMigration: true },
55+ })
56+ .eq("atp_did", did);
57+58+ return { success: false, error: result.error.message };
59 }
60 return { success: true };
61 });
62+63+ if (!oauthValid.success) {
64+ return {
65+ success: false,
66+ error: `Failed to restore OAuth session`,
67+ stats,
68+ publicationUriMap: {},
69+ documentUriMap: {},
70+ userSubscriptionUriMap: {},
71+ };
72+ }
7374 // Step 2: Get user's pub.leaflet.publication records
75 const oldPublications = await step.run(
···488 // 2. External references (e.g., from other AT Proto apps) to old URIs continue to work
489 // 3. The normalization layer handles both schemas transparently for reads
490 // Old records are also kept on the user's PDS so existing AT-URI references remain valid.
491+492+ // Clear the migration flag on success
493+ if (stats.errors.length === 0) {
494+ await step.run("clear-migration-flag", async () => {
495+ await supabaseServerClient
496+ .from("identities")
497+ .update({ metadata: null })
498+ .eq("atp_did", did);
499+ });
500+ }
501502 return {
503 success: stats.errors.length === 0,