Shoraky commited on
Commit
ab01304
·
1 Parent(s): 200d616

Secure profile auth linkage

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. src/supabase.ts +14 -0
README.md CHANGED
@@ -56,7 +56,7 @@ Run one container with one Python worker: analysis jobs and retry payloads are i
56
 
57
  The production database must have the analysis functions and action-review structures used by secret-protected `/internal/analysis` routes. The database also needs the profile columns shown in `../Database/schema.sql` and a private `profile-images` Supabase Storage bucket for JPG, PNG, and WEBP files up to 4 MB. The frontend resizes profile photos to at most 1280 pixels and encodes them as WebP before upload. The backend reads and writes images through its service-role client; browsers access them through authenticated API routes with private, versioned browser caching.
58
 
59
- Before deploying access-control or deletion changes, run [`../Database/updates/access-permissions.sql`](../Database/updates/access-permissions.sql) in the Supabase SQL Editor. This update adds the admin-origin flag, nullable owner references for retained records, cascading action deletion, and the service-role RPCs used for quota refunds and analysis completion. All service-role reads and writes must remain behind the ownership and admin checks in the API. The full policy is documented in [`../Database/access-permissions.md`](../Database/access-permissions.md).
60
 
61
  To remove the unused `players.dominant_foot` and `players.notes` columns from an existing database, apply `../Database/migrations/20261017_remove_player_dominant_foot_notes.sql` in the Supabase SQL Editor.
62
 
 
56
 
57
  The production database must have the analysis functions and action-review structures used by secret-protected `/internal/analysis` routes. The database also needs the profile columns shown in `../Database/schema.sql` and a private `profile-images` Supabase Storage bucket for JPG, PNG, and WEBP files up to 4 MB. The frontend resizes profile photos to at most 1280 pixels and encodes them as WebP before upload. The backend reads and writes images through its service-role client; browsers access them through authenticated API routes with private, versioned browser caching.
58
 
59
+ Before deploying access-control or deletion changes, run [`../Database/updates/access-permissions.sql`](../Database/updates/access-permissions.sql) in the Supabase SQL Editor. To link profile records to Supabase Auth and block direct Data API access to application tables, also run [`../Database/updates/auth-profile-security.sql`](../Database/updates/auth-profile-security.sql); it stops if orphaned profile rows exist. These database changes do not replace the ownership and admin checks required on every Backend service-role route. The full policy is documented in [`../Database/access-permissions.md`](../Database/access-permissions.md).
60
 
61
  To remove the unused `players.dominant_foot` and `players.notes` columns from an existing database, apply `../Database/migrations/20261017_remove_player_dominant_foot_notes.sql` in the Supabase SQL Editor.
62
 
src/supabase.ts CHANGED
@@ -68,6 +68,20 @@ export async function syncProfile(user: User, profile?: { fname?: string; lname?
68
  account_role: "user",
69
  };
70
  const { error } = await supabaseAdmin.from("users").insert({ ...fields, password_hash: profile?.passwordHash ?? await randomPasswordHash() });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  if (error) throw new Error(`Unable to create user profile: ${error.message}`);
72
  }
73
 
 
68
  account_role: "user",
69
  };
70
  const { error } = await supabaseAdmin.from("users").insert({ ...fields, password_hash: profile?.passwordHash ?? await randomPasswordHash() });
71
+ if (error?.code === "23505") {
72
+ // Two OAuth callbacks or sign-in requests can race after both observe that
73
+ // the profile is missing. Re-read the user ID before treating the conflict
74
+ // as a harmless concurrent insert; an email conflict for another user must
75
+ // still fail.
76
+ const { data: racedProfile, error: racedReadError } = await supabaseAdmin.from("users")
77
+ .select("user_id").eq("user_id", user.id).maybeSingle();
78
+ if (racedReadError) throw new Error(`Unable to verify user profile: ${racedReadError.message}`);
79
+ if (racedProfile) {
80
+ const { error: syncError } = await supabaseAdmin.from("users").update({ email: user.email }).eq("user_id", user.id);
81
+ if (syncError) throw new Error(`Unable to update user profile: ${syncError.message}`);
82
+ return;
83
+ }
84
+ }
85
  if (error) throw new Error(`Unable to create user profile: ${error.message}`);
86
  }
87