fix: resolve team_id during auth and preserve across token refresh

P1 from Codex review: interactive auth saved team_id: '' making all
subsequent sync operations fail. Now resolves team_id from team_members
table immediately after OAuth callback.

Also fixes token refresh in sync.ts to preserve the existing team_id
instead of resetting it to empty, and removes order=created_at.desc
from pullTable() default query since sync_heartbeats and team_members
tables don't have that column (P2).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-03-19 01:28:15 -07:00
parent d051f84060
commit 7808ee380b
2 changed files with 41 additions and 6 deletions
+4 -3
View File
@@ -39,7 +39,7 @@ interface CacheMeta {
* Refresh an expired access token using the refresh token.
* Returns new tokens on success, null on failure.
*/
async function refreshToken(supabaseUrl: string, refreshToken: string, anonKey: string): Promise<AuthTokens | null> {
async function refreshToken(supabaseUrl: string, refreshToken: string, anonKey: string, existingTeamId?: string): Promise<AuthTokens | null> {
try {
const res = await fetchWithTimeout(`${supabaseUrl}/auth/v1/token?grant_type=refresh_token`, {
method: 'POST',
@@ -58,7 +58,7 @@ async function refreshToken(supabaseUrl: string, refreshToken: string, anonKey:
refresh_token: data.refresh_token as string || refreshToken,
expires_at: Math.floor(Date.now() / 1000) + ((data.expires_in as number) || 3600),
user_id: (data.user as any)?.id || '',
team_id: '',
team_id: existingTeamId || '', // preserve existing team_id across refresh
email: (data.user as any)?.email || '',
};
} catch {
@@ -78,6 +78,7 @@ export async function getValidToken(config: SyncConfig): Promise<string | null>
config.team.supabase_url,
config.auth.refresh_token,
config.team.supabase_anon_key,
config.auth.team_id,
);
if (!newTokens) return null;
@@ -234,7 +235,7 @@ export async function pullTable(table: string, query?: string): Promise<Record<s
const url = query
? `${restUrl(config.team.supabase_url, table)}?${query}`
: `${restUrl(config.team.supabase_url, table)}?team_id=eq.${config.auth.team_id}&order=created_at.desc&limit=500`;
: `${restUrl(config.team.supabase_url, table)}?team_id=eq.${config.auth.team_id}&limit=500`;
const res = await fetchWithTimeout(url, {
method: 'GET',