Pulastya B commited on
Commit
a5bd6df
·
1 Parent(s): a220eb5

Fix OAuth signup: don't create duplicate account, just save profile data

Browse files
Files changed (1) hide show
  1. FRRONTEEEND/components/AuthPage.tsx +14 -2
FRRONTEEEND/components/AuthPage.tsx CHANGED
@@ -155,13 +155,17 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
155
 
156
  // Check if user is already authenticated (OAuth flow)
157
  const isOAuthUser = !!user;
 
158
 
159
  try {
160
  let userId: string;
 
161
 
162
  if (isOAuthUser) {
163
  // User already authenticated via OAuth, just save profile
164
  userId = user.id;
 
 
165
  } else {
166
  // Email/password signup
167
  if (formData.password !== formData.confirmPassword) {
@@ -170,15 +174,17 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
170
  return;
171
  }
172
 
 
173
  const { error } = await signUp(formData.email, formData.password);
174
  if (error) {
 
175
  setError(error.message);
176
  setIsSubmitting(false);
177
  return;
178
  }
179
 
180
  // Wait for Supabase to create the auth user
181
- await new Promise(resolve => setTimeout(resolve, 1000));
182
 
183
  // Get the user ID from auth session
184
  const { data: { session } } = await supabase.auth.getSession();
@@ -188,13 +194,15 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
188
  return;
189
  }
190
  userId = session.user.id;
 
 
191
  }
192
 
193
  // Save user profile data to database
194
  const profileData = {
195
  user_id: userId,
196
  name: formData.name,
197
- email: formData.email,
198
  primary_goal: formData.primaryGoal,
199
  target_outcome: formData.targetOutcome,
200
  data_types: formData.dataTypes,
@@ -204,9 +212,13 @@ export const AuthPage: React.FC<AuthPageProps> = ({ onSuccess, onSkip }) => {
204
  onboarding_completed: true
205
  };
206
 
 
207
  const savedProfile = await saveUserProfile(profileData);
208
  if (!savedProfile) {
209
  console.warn('Failed to save profile data, but auth succeeded');
 
 
 
210
  }
211
 
212
  setSuccess(isOAuthUser ? 'Profile completed! Redirecting...' : 'Account created successfully! Redirecting...');
 
155
 
156
  // Check if user is already authenticated (OAuth flow)
157
  const isOAuthUser = !!user;
158
+ console.log('handleSignUp called, isOAuthUser:', isOAuthUser, 'user:', user);
159
 
160
  try {
161
  let userId: string;
162
+ let userEmail: string;
163
 
164
  if (isOAuthUser) {
165
  // User already authenticated via OAuth, just save profile
166
  userId = user.id;
167
+ userEmail = user.email || formData.email;
168
+ console.log('OAuth user detected, saving profile only. userId:', userId);
169
  } else {
170
  // Email/password signup
171
  if (formData.password !== formData.confirmPassword) {
 
174
  return;
175
  }
176
 
177
+ console.log('Email/password signup, creating new account...');
178
  const { error } = await signUp(formData.email, formData.password);
179
  if (error) {
180
+ console.error('Signup error:', error);
181
  setError(error.message);
182
  setIsSubmitting(false);
183
  return;
184
  }
185
 
186
  // Wait for Supabase to create the auth user
187
+ await new Promise(resolve => setTimeout(resolve, 1500));
188
 
189
  // Get the user ID from auth session
190
  const { data: { session } } = await supabase.auth.getSession();
 
194
  return;
195
  }
196
  userId = session.user.id;
197
+ userEmail = session.user.email || formData.email;
198
+ console.log('New account created, userId:', userId);
199
  }
200
 
201
  // Save user profile data to database
202
  const profileData = {
203
  user_id: userId,
204
  name: formData.name,
205
+ email: userEmail,
206
  primary_goal: formData.primaryGoal,
207
  target_outcome: formData.targetOutcome,
208
  data_types: formData.dataTypes,
 
212
  onboarding_completed: true
213
  };
214
 
215
+ console.log('Saving profile data:', profileData);
216
  const savedProfile = await saveUserProfile(profileData);
217
  if (!savedProfile) {
218
  console.warn('Failed to save profile data, but auth succeeded');
219
+ setError('Profile saved with warnings. You can continue.');
220
+ } else {
221
+ console.log('Profile saved successfully:', savedProfile);
222
  }
223
 
224
  setSuccess(isOAuthUser ? 'Profile completed! Redirecting...' : 'Account created successfully! Redirecting...');