Skip to content

Commit 7489ff6

Browse files
committed
feat: enhance Google Drive API initialization with robust error handling, detailed logging, and client ID validation.
1 parent 3629836 commit 7489ff6

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

lib/hooks/useGoogleDrive.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,70 @@ export function useGoogleDrive() {
3131

3232
const initClient = async () => {
3333
try {
34+
console.log('[Google Drive] Starting initialization...');
35+
console.log('[Google Drive] Client ID:', process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID ? 'Present' : 'Missing');
36+
37+
if (!process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID) {
38+
throw new Error('Missing Google Client ID');
39+
}
40+
3441
// Dynamically load gapi script
42+
console.log('[Google Drive] Loading gapi script...');
3543
if (!window.gapi) {
3644
await new Promise<void>((resolve, reject) => {
3745
const script = document.createElement('script');
3846
script.src = 'https://apis.google.com/js/api.js';
39-
script.onload = () => resolve();
40-
script.onerror = () => reject(new Error('Failed to load gapi script'));
47+
script.onload = () => {
48+
console.log('[Google Drive] gapi script loaded');
49+
resolve();
50+
};
51+
script.onerror = (e) => {
52+
console.error('[Google Drive] Failed to load gapi script', e);
53+
reject(new Error('Failed to load Google API script'));
54+
};
4155
document.body.appendChild(script);
4256
});
4357
}
4458

45-
await new Promise<void>((resolve) => {
46-
gapi.load('client:auth2', () => {
47-
resolve();
59+
console.log('[Google Drive] Loading client:auth2...');
60+
await new Promise<void>((resolve, reject) => {
61+
gapi.load('client:auth2', {
62+
callback: () => {
63+
console.log('[Google Drive] client:auth2 loaded');
64+
resolve();
65+
},
66+
onerror: (err: any) => {
67+
console.error('[Google Drive] Failed to load client:auth2', err);
68+
reject(new Error('Failed to load Google client libraries'));
69+
}
4870
});
4971
});
5072

73+
console.log('[Google Drive] Initializing client...');
5174
await gapi.client.init({
52-
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || '',
75+
apiKey: '', // No API key needed for OAuth
76+
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
5377
discoveryDocs: DISCOVERY_DOCS,
5478
scope: SCOPES,
5579
});
5680

81+
console.log('[Google Drive] Client initialized successfully');
82+
5783
// Listen for sign-in state changes.
5884
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
5985

6086
// Handle the initial sign-in state.
6187
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
6288
setIsInitialized(true);
89+
console.log('[Google Drive] Initialization complete');
6390
} catch (err: any) {
64-
console.error('Error initializing Google API client', err);
65-
setError(err.message || 'Failed to initialize Google API');
91+
console.error('[Google Drive] Error initializing Google API client', err);
92+
const errorMessage = err?.message || err?.error || 'Failed to initialize Google API';
93+
setError(errorMessage);
6694
}
6795
};
6896

69-
if (process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID) {
70-
initClient();
71-
} else {
72-
setError('Missing Google Client ID');
73-
}
97+
initClient();
7498
}, []);
7599

76100
const updateSigninStatus = (isSignedIn: boolean) => {

0 commit comments

Comments
 (0)