Skip to content

Commit 27359da

Browse files
Added better oauth google sign-in error handling.
1 parent 372215c commit 27359da

File tree

1 file changed

+116
-9
lines changed

1 file changed

+116
-9
lines changed

app/login.tsx

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ function LoginScreenContent() {
6969

7070
// Simple configuration with minimal logging to prevent recursion
7171
try {
72-
const webClientId =
73-
Constants.expoConfig?.extra?.googleWebClientId ||
74-
// Expo public envs are embedded at bundle-time
75-
(process.env.EXPO_PUBLIC_GOOGLE_WEB_CLIENT_ID as string | undefined);
72+
// Use different client IDs for debug vs production
73+
const webClientId = __DEV__
74+
? Constants.expoConfig?.extra?.googleWebClientIdDev || Constants.expoConfig?.extra?.googleWebClientId
75+
: Constants.expoConfig?.extra?.googleWebClientId;
7676

7777
if (!webClientId) {
7878
console.warn('🟡 Google Web Client ID is missing. Falling back to no offline access.');
@@ -82,7 +82,8 @@ function LoginScreenContent() {
8282
return;
8383
}
8484

85-
console.log('🟢 Configuring Google Sign-In with client ID:', webClientId);
85+
console.log('🟢 Configuring Google Sign-In with client ID:', webClientId.substring(0, 20) + '...');
86+
console.log('📱 Build type:', __DEV__ ? 'DEBUG' : 'PRODUCTION');
8687

8788
// Configure Google Sign-In
8889
GoogleSignin.configure({
@@ -107,17 +108,72 @@ function LoginScreenContent() {
107108
const signIn = async () => {
108109
try {
109110
console.log('🟢 Starting Google sign in process...');
111+
console.log('📱 Build type:', __DEV__ ? 'DEBUG' : 'PRODUCTION');
112+
console.log('📦 Package name:', Constants.expoConfig?.android?.package);
110113

111114
// Check if Google Play Services are available
112115
console.log('🟢 Checking Google Play Services...');
113116
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
114117
console.log('✅ Google Play Services available');
115118

116-
// Attempt to sign in
119+
// Log current configuration
120+
console.log('🔧 Current GoogleSignin configuration complete');
121+
122+
// Enhanced sign-in with better error detection
117123
console.log('🟢 Initiating Google Sign-In...');
124+
125+
// Check if user has previously signed in
126+
const hasPreviousSignIn = GoogleSignin.hasPreviousSignIn();
127+
console.log('� Has previous sign-in:', hasPreviousSignIn);
128+
129+
// Get current user if available
130+
const currentUser = GoogleSignin.getCurrentUser();
131+
console.log('👤 Current user:', currentUser ? 'Found' : 'Not found');
132+
133+
// If there's a current user, try to clear session first
134+
if (currentUser) {
135+
try {
136+
console.log('� Clearing existing Google session...');
137+
await GoogleSignin.signOut();
138+
console.log('✅ Previous session cleared');
139+
} catch (clearError: any) {
140+
console.log('⚠️ Could not clear session:', clearError?.message);
141+
}
142+
}
118143

119144
// Perform the sign-in
120-
const signInResult = await GoogleSignin.signIn();
145+
console.log('🚀 Calling GoogleSignin.signIn()...');
146+
147+
let signInResult;
148+
try {
149+
signInResult = await GoogleSignin.signIn();
150+
console.log('✅ GoogleSignin.signIn() completed successfully');
151+
console.log('📋 Raw signInResult type:', typeof signInResult);
152+
console.log('📋 Raw signInResult keys:', signInResult ? Object.keys(signInResult) : 'null/undefined');
153+
} catch (signInError: any) {
154+
// Log the specific error from GoogleSignin
155+
console.error('🔴 GoogleSignin.signIn() threw an error:', {
156+
code: signInError?.code,
157+
message: signInError?.message,
158+
statusCode: signInError?.statusCode,
159+
statusCodes: signInError?.statusCodes,
160+
error: signInError
161+
});
162+
163+
// Handle specific error codes
164+
if (signInError?.code === 'SIGN_IN_CANCELLED') {
165+
console.log('ℹ️ User cancelled sign-in');
166+
return; // Don't show error for user cancellation
167+
} else if (signInError?.code === 'IN_PROGRESS') {
168+
console.log('⏳ Sign-in already in progress');
169+
return;
170+
} else if (signInError?.code === 'PLAY_SERVICES_NOT_AVAILABLE') {
171+
throw new Error('Google Play Services not available');
172+
} else {
173+
// Re-throw the specific error
174+
throw signInError;
175+
}
176+
}
121177

122178
// The structure from Expo's implementation is different - data is nested
123179
// Expo adds a wrapper with { type: 'success', data: { actual response } }
@@ -127,16 +183,24 @@ function LoginScreenContent() {
127183
let userData;
128184
let idToken;
129185

186+
console.log('🔍 Attempting to extract user data and token...');
187+
130188
if (signInResult?.type === 'success' && signInResult?.data) {
131189
// Expo structure
190+
console.log('📋 Using Expo structure');
132191
userData = signInResult.data.user;
133192
idToken = signInResult.data.idToken;
134193
} else {
135194
// Regular structure
195+
console.log('📋 Using regular structure');
136196
userData = signInResult?.user;
137197
idToken = signInResult?.idToken;
138198
}
139199

200+
console.log('👤 Extracted userData:', userData ? 'Found' : 'Missing');
201+
console.log('🔑 Extracted idToken:', idToken ? 'Found' : 'Missing');
202+
console.log('📧 User email:', userData?.email);
203+
140204
if (userData && idToken) {
141205
console.log('✅ Google Sign-In successful:', userData.email);
142206
console.log('✅ ID Token received, setting user and calling API');
@@ -160,7 +224,10 @@ function LoginScreenContent() {
160224
const response = await fetch('https://api.codebuilder.org/auth/google', {
161225
method: 'POST',
162226
headers: { 'Content-Type': 'application/json' },
163-
body: JSON.stringify({ idToken }),
227+
body: JSON.stringify({
228+
idToken,
229+
buildType: __DEV__ ? 'development' : 'production'
230+
}),
164231
});
165232

166233
console.log('✅ Auth API response received:', response.status);
@@ -222,6 +289,46 @@ function LoginScreenContent() {
222289
});
223290
}
224291
} catch (e: any) {
292+
// Enhanced error logging
293+
console.error('🔴 Google sign in error details:', {
294+
name: e?.name,
295+
code: e?.code,
296+
message: e?.message,
297+
statusCode: e?.statusCode,
298+
statusCodes: e?.statusCodes,
299+
stack: e?.stack,
300+
fullError: e
301+
});
302+
303+
// More specific error handling based on common issues
304+
let userFriendlyMessage = 'Failed to sign in with Google';
305+
let diagnosticInfo = '';
306+
307+
if (e?.code === '12500' || e?.statusCode === 12500) {
308+
// API_NOT_CONNECTED - common with SHA-1 mismatch
309+
diagnosticInfo = 'API_NOT_CONNECTED: This usually indicates SHA-1 fingerprint mismatch or OAuth client misconfiguration.';
310+
userFriendlyMessage = 'Google Sign-In configuration error. Please check your OAuth setup.';
311+
} else if (e?.code === '10' || e?.statusCode === 10) {
312+
// DEVELOPER_ERROR - wrong configuration
313+
diagnosticInfo = 'DEVELOPER_ERROR: OAuth client configuration is incorrect.';
314+
userFriendlyMessage = 'Google Sign-In setup error. Please verify your OAuth client configuration.';
315+
} else if (e?.code === '12501' || e?.statusCode === 12501) {
316+
// SIGN_IN_CANCELLED by user
317+
console.log('ℹ️ User cancelled sign-in');
318+
return; // Don't show error notification
319+
} else if (e?.code === '7' || e?.statusCode === 7) {
320+
// NETWORK_ERROR
321+
diagnosticInfo = 'NETWORK_ERROR: Check your internet connection.';
322+
userFriendlyMessage = 'Network error. Please check your internet connection and try again.';
323+
} else if (e?.message?.includes('SIGN_IN_TIMEOUT')) {
324+
diagnosticInfo = 'SIGN_IN_TIMEOUT: This usually indicates SHA-1 fingerprint mismatch in production builds.';
325+
userFriendlyMessage = 'Sign-in timed out. This usually indicates a configuration issue.';
326+
}
327+
328+
if (diagnosticInfo) {
329+
console.error('🔍 DIAGNOSIS:', diagnosticInfo);
330+
}
331+
225332
// Log error info for troubleshooting
226333
const code = e?.code || e?.statusCodes || 'UNKNOWN';
227334
const message = e?.message || 'No error message available';
@@ -232,7 +339,7 @@ function LoginScreenContent() {
232339
notify({
233340
type: 'error',
234341
title: 'Google Sign-In Failed',
235-
message: message || 'Failed to sign in with Google',
342+
message: userFriendlyMessage,
236343
});
237344
}
238345
};

0 commit comments

Comments
 (0)