Pulastya B commited on
Commit
4ac5690
·
1 Parent(s): d072c9e

Configuring Supabase RLS

Browse files
Files changed (1) hide show
  1. FRRONTEEEND/lib/supabase.ts +33 -17
FRRONTEEEND/lib/supabase.ts CHANGED
@@ -280,27 +280,36 @@ export const updateHuggingFaceToken = async (userId: string, hfToken: string, hf
280
  console.log('[HF Token] Starting update for user:', userId);
281
 
282
  try {
283
- // First check if profile exists
284
  console.log('[HF Token] Checking if profile exists...');
285
- const { data: existingProfile, error: fetchError } = await supabase
 
286
  .from('user_profiles')
287
  .select('user_id, name, email')
288
  .eq('user_id', userId)
289
  .single();
290
 
291
- if (fetchError) {
292
- console.error('[HF Token] Profile fetch error:', fetchError);
293
- return null;
294
- }
295
 
296
- if (!existingProfile) {
297
- console.error('[HF Token] Profile not found for user:', userId);
298
- return null;
 
 
 
 
 
 
 
 
299
  }
300
 
301
- console.log('[HF Token] Profile found, updating HF fields...');
302
 
303
- // Profile exists, update only HF fields
304
  const updateData = {
305
  huggingface_token: hfToken || null,
306
  huggingface_username: hfUsername || null,
@@ -308,22 +317,29 @@ export const updateHuggingFaceToken = async (userId: string, hfToken: string, hf
308
  };
309
  console.log('[HF Token] Update payload:', { ...updateData, huggingface_token: hfToken ? '****' : null });
310
 
311
- const { data, error } = await supabase
312
  .from('user_profiles')
313
  .update(updateData)
314
  .eq('user_id', userId)
315
  .select()
316
  .single();
317
 
318
- if (error) {
319
- console.error('[HF Token] Update failed:', error.message, error.code, error.details);
 
 
 
 
 
 
 
320
  return null;
321
  }
322
 
323
  console.log('[HF Token] Update successful!');
324
- return data;
325
- } catch (err) {
326
- console.error('[HF Token] Unexpected error:', err);
327
  return null;
328
  }
329
  };
 
280
  console.log('[HF Token] Starting update for user:', userId);
281
 
282
  try {
283
+ // First check if profile exists with timeout
284
  console.log('[HF Token] Checking if profile exists...');
285
+
286
+ const profileCheckPromise = supabase
287
  .from('user_profiles')
288
  .select('user_id, name, email')
289
  .eq('user_id', userId)
290
  .single();
291
 
292
+ // Add 5 second timeout
293
+ const timeoutPromise = new Promise((_, reject) =>
294
+ setTimeout(() => reject(new Error('Profile check timeout')), 5000)
295
+ );
296
 
297
+ let existingProfile;
298
+ try {
299
+ const result = await Promise.race([profileCheckPromise, timeoutPromise]) as any;
300
+ if (result.error) {
301
+ console.error('[HF Token] Profile fetch error:', result.error);
302
+ return null;
303
+ }
304
+ existingProfile = result.data;
305
+ } catch (timeoutErr) {
306
+ console.error('[HF Token] Profile check timed out, proceeding with update anyway...');
307
+ // Proceed with update even if check times out - the update will fail if profile doesn't exist
308
  }
309
 
310
+ console.log('[HF Token] Proceeding with update...');
311
 
312
+ // Update HF fields (will fail if profile doesn't exist due to no matching rows)
313
  const updateData = {
314
  huggingface_token: hfToken || null,
315
  huggingface_username: hfUsername || null,
 
317
  };
318
  console.log('[HF Token] Update payload:', { ...updateData, huggingface_token: hfToken ? '****' : null });
319
 
320
+ const updatePromise = supabase
321
  .from('user_profiles')
322
  .update(updateData)
323
  .eq('user_id', userId)
324
  .select()
325
  .single();
326
 
327
+ // Add 5 second timeout for update
328
+ const updateTimeoutPromise = new Promise((_, reject) =>
329
+ setTimeout(() => reject(new Error('Update timeout')), 5000)
330
+ );
331
+
332
+ const updateResult = await Promise.race([updatePromise, updateTimeoutPromise]) as any;
333
+
334
+ if (updateResult.error) {
335
+ console.error('[HF Token] Update failed:', updateResult.error.message, updateResult.error.code);
336
  return null;
337
  }
338
 
339
  console.log('[HF Token] Update successful!');
340
+ return updateResult.data;
341
+ } catch (err: any) {
342
+ console.error('[HF Token] Unexpected error:', err?.message || err);
343
  return null;
344
  }
345
  };