Spaces:
Running
Running
Pulastya B commited on
Commit ·
e7ac2d3
1
Parent(s): 4ac5690
Adding a SELECT policy to the database
Browse files- FRRONTEEEND/lib/supabase.ts +15 -43
FRRONTEEEND/lib/supabase.ts
CHANGED
|
@@ -279,37 +279,15 @@ export const getUserProfile = async (userId: string) => {
|
|
| 279 |
export const updateHuggingFaceToken = async (userId: string, hfToken: string, hfUsername?: string) => {
|
| 280 |
console.log('[HF Token] Starting update for user:', userId);
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
try {
|
| 283 |
-
|
| 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,27 +295,21 @@ export const updateHuggingFaceToken = async (userId: string, hfToken: string, hf
|
|
| 317 |
};
|
| 318 |
console.log('[HF Token] Update payload:', { ...updateData, huggingface_token: hfToken ? '****' : null });
|
| 319 |
|
| 320 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
|
|
|
| 341 |
} catch (err: any) {
|
| 342 |
console.error('[HF Token] Unexpected error:', err?.message || err);
|
| 343 |
return null;
|
|
|
|
| 279 |
export const updateHuggingFaceToken = async (userId: string, hfToken: string, hfUsername?: string) => {
|
| 280 |
console.log('[HF Token] Starting update for user:', userId);
|
| 281 |
|
| 282 |
+
// Check if Supabase is properly configured
|
| 283 |
+
if (!isSupabaseConfigured()) {
|
| 284 |
+
console.error('[HF Token] Supabase not configured!');
|
| 285 |
+
return null;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
try {
|
| 289 |
+
console.log('[HF Token] Attempting direct update...');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
const updateData = {
|
| 292 |
huggingface_token: hfToken || null,
|
| 293 |
huggingface_username: hfUsername || null,
|
|
|
|
| 295 |
};
|
| 296 |
console.log('[HF Token] Update payload:', { ...updateData, huggingface_token: hfToken ? '****' : null });
|
| 297 |
|
| 298 |
+
// Don't use .select() - just update and check if any rows were affected
|
| 299 |
+
// This only requires UPDATE permission, not SELECT
|
| 300 |
+
const { error, count } = await supabase
|
| 301 |
.from('user_profiles')
|
| 302 |
.update(updateData)
|
| 303 |
+
.eq('user_id', userId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
+
if (error) {
|
| 306 |
+
console.error('[HF Token] Update failed:', error.message, error.code, error.hint);
|
|
|
|
|
|
|
| 307 |
return null;
|
| 308 |
}
|
| 309 |
|
| 310 |
console.log('[HF Token] Update successful!');
|
| 311 |
+
// Return the data we sent since we can't select it back
|
| 312 |
+
return { ...updateData, user_id: userId };
|
| 313 |
} catch (err: any) {
|
| 314 |
console.error('[HF Token] Unexpected error:', err?.message || err);
|
| 315 |
return null;
|