Pulastya B commited on
Commit
4079a53
·
1 Parent(s): dd17c4c

Fixed HF Tokens not saving in the DB

Browse files
FRRONTEEEND/components/AuthPage.tsx CHANGED
@@ -202,6 +202,31 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
202
  }
203
 
204
  // Save user profile data to database
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  const profileData = {
206
  user_id: userId,
207
  name: formData.name,
@@ -213,6 +238,7 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
213
  experience: formData.experience,
214
  industry: formData.industry,
215
  huggingface_token: formData.huggingfaceToken || null,
 
216
  onboarding_completed: true
217
  };
218
 
 
202
  }
203
 
204
  // Save user profile data to database
205
+ let hfUsername = null;
206
+
207
+ // If HF token provided, validate it first
208
+ if (formData.huggingfaceToken?.trim()) {
209
+ console.log('Validating HuggingFace token...');
210
+ try {
211
+ const hfResponse = await fetch('https://huggingface.co/api/whoami-v2', {
212
+ headers: { 'Authorization': `Bearer ${formData.huggingfaceToken}` }
213
+ });
214
+
215
+ if (hfResponse.ok) {
216
+ const hfData = await hfResponse.json();
217
+ hfUsername = hfData.name;
218
+ console.log('HF token valid, username:', hfUsername);
219
+ } else {
220
+ console.warn('HF token invalid, will skip saving it');
221
+ // Don't fail the whole signup, just skip HF token
222
+ formData.huggingfaceToken = null;
223
+ }
224
+ } catch (err) {
225
+ console.warn('HF validation failed, will skip saving token:', err);
226
+ formData.huggingfaceToken = null;
227
+ }
228
+ }
229
+
230
  const profileData = {
231
  user_id: userId,
232
  name: formData.name,
 
238
  experience: formData.experience,
239
  industry: formData.industry,
240
  huggingface_token: formData.huggingfaceToken || null,
241
+ huggingface_username: hfUsername,
242
  onboarding_completed: true
243
  };
244
 
FRRONTEEEND/components/SettingsModal.tsx CHANGED
@@ -113,7 +113,7 @@ export const SettingsModal: React.FC<SettingsModalProps> = ({ isOpen, onClose })
113
  // Hide success message after 3 seconds
114
  setTimeout(() => setSaveSuccess(false), 3000);
115
  } else {
116
- setError('Failed to save token. Please try again.');
117
  }
118
  } catch (err) {
119
  setError('An error occurred. Please try again.');
 
113
  // Hide success message after 3 seconds
114
  setTimeout(() => setSaveSuccess(false), 3000);
115
  } else {
116
+ setError('Profile not found. Please complete onboarding first or try signing out and back in.');
117
  }
118
  } catch (err) {
119
  setError('An error occurred. Please try again.');
FRRONTEEEND/lib/supabase.ts CHANGED
@@ -275,19 +275,30 @@ export const getUserProfile = async (userId: string) => {
275
  }
276
  };
277
 
278
- // Update HuggingFace token for a user (UPSERT to handle new users)
279
  export const updateHuggingFaceToken = async (userId: string, hfToken: string, hfUsername?: string) => {
280
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  const { data, error } = await supabase
282
  .from('user_profiles')
283
- .upsert({
284
- user_id: userId,
285
  huggingface_token: hfToken,
286
  huggingface_username: hfUsername,
287
  updated_at: new Date().toISOString()
288
- }, {
289
- onConflict: 'user_id'
290
  })
 
291
  .select()
292
  .single();
293
 
 
275
  }
276
  };
277
 
278
+ // Update HuggingFace token for a user (only updates existing profiles)
279
  export const updateHuggingFaceToken = async (userId: string, hfToken: string, hfUsername?: string) => {
280
  try {
281
+ // First check if profile exists
282
+ const { data: existingProfile, error: fetchError } = await supabase
283
+ .from('user_profiles')
284
+ .select('user_id, name, email')
285
+ .eq('user_id', userId)
286
+ .single();
287
+
288
+ if (fetchError || !existingProfile) {
289
+ console.error('Profile not found, cannot update HF token:', fetchError);
290
+ return null;
291
+ }
292
+
293
+ // Profile exists, update only HF fields
294
  const { data, error } = await supabase
295
  .from('user_profiles')
296
+ .update({
 
297
  huggingface_token: hfToken,
298
  huggingface_username: hfUsername,
299
  updated_at: new Date().toISOString()
 
 
300
  })
301
+ .eq('user_id', userId)
302
  .select()
303
  .single();
304