From 2a11e012cc12ecb57fafd9643e2d88537dfce658 Mon Sep 17 00:00:00 2001 From: ishaileshpant Date: Mon, 13 Oct 2025 22:28:58 +0530 Subject: [PATCH 1/5] Fix installation token authentication error - Call auth function to get actual installation token before passing to Octokit - Fixes '[@octokit/auth-token] Token passed to createTokenAuth is not a string' error - Ensures proper token string is passed to Octokit constructor - Adds proper error handling for installation token retrieval --- api/github/create-content-pr.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/api/github/create-content-pr.js b/api/github/create-content-pr.js index 046c643..631e159 100644 --- a/api/github/create-content-pr.js +++ b/api/github/create-content-pr.js @@ -132,8 +132,8 @@ async function tryGitHubAppApproach(contentData, userToken) { console.log('๐Ÿ”ง Attempting GitHub App approach...'); console.log('GitHub App ID:', process.env.GITHUB_APP_ID); - console.log('Private Key length:', process.env.GITHUB_APP_PRIVATE_KEY?.length); - + console.log('Private Key length:', process.env.GITHUB_APP_PRIVATE_KEY?.length); + // Use the exact same approach as the diagnostic endpoint console.log('๐Ÿ”ง Using diagnostic endpoint approach for private key processing...'); let privateKey = process.env.GITHUB_APP_PRIVATE_KEY; @@ -229,7 +229,22 @@ async function tryGitHubAppApproach(contentData, userToken) { return { success: false, error: `Failed to create installation auth: ${authError.message}` }; } - const octokit = new Octokit({ auth }); + // Get the installation token + console.log('๐Ÿ”‘ Getting installation token...'); + let installationToken; + try { + const tokenResult = await auth({ type: 'installation' }); + installationToken = tokenResult.token; + console.log('โœ… Installation token obtained successfully'); + console.log(' - Token length:', installationToken ? installationToken.length : 'null'); + console.log(' - Token type:', tokenResult.type); + } catch (tokenError) { + console.error('โŒ Failed to get installation token:', tokenError.message); + console.error('Installation token error details:', tokenError); + return { success: false, error: `Failed to get installation token: ${tokenError.message}` }; + } + + const octokit = new Octokit({ auth: installationToken }); // Test installation token by making a simple API call console.log('๐Ÿงช Testing installation token with simple API call...'); From 3f63f7e00efa295743b868a08b4a8e738a6a4ff3 Mon Sep 17 00:00:00 2001 From: ishaileshpant Date: Mon, 13 Oct 2025 22:30:13 +0530 Subject: [PATCH 2/5] Fix Octokit auth approach - use auth function directly - Pass auth function directly to Octokit instead of extracting token - This is the correct approach for @octokit/auth-app - Fixes JWT decoding errors when using installation tokens - Octokit will handle token refresh automatically --- api/github/create-content-pr.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/api/github/create-content-pr.js b/api/github/create-content-pr.js index 631e159..53d9785 100644 --- a/api/github/create-content-pr.js +++ b/api/github/create-content-pr.js @@ -229,22 +229,7 @@ async function tryGitHubAppApproach(contentData, userToken) { return { success: false, error: `Failed to create installation auth: ${authError.message}` }; } - // Get the installation token - console.log('๐Ÿ”‘ Getting installation token...'); - let installationToken; - try { - const tokenResult = await auth({ type: 'installation' }); - installationToken = tokenResult.token; - console.log('โœ… Installation token obtained successfully'); - console.log(' - Token length:', installationToken ? installationToken.length : 'null'); - console.log(' - Token type:', tokenResult.type); - } catch (tokenError) { - console.error('โŒ Failed to get installation token:', tokenError.message); - console.error('Installation token error details:', tokenError); - return { success: false, error: `Failed to get installation token: ${tokenError.message}` }; - } - - const octokit = new Octokit({ auth: installationToken }); + const octokit = new Octokit({ auth }); // Test installation token by making a simple API call console.log('๐Ÿงช Testing installation token with simple API call...'); From ec8493f13b8e023873b2e5a65dd9e36c42a1b030 Mon Sep 17 00:00:00 2001 From: ishaileshpant Date: Mon, 13 Oct 2025 22:31:11 +0530 Subject: [PATCH 3/5] Fix Octokit auth strategy - use authStrategy parameter - Use authStrategy parameter instead of auth parameter - This is the correct way to use @octokit/auth-app with Octokit - Fixes 'Token passed to createTokenAuth is not a string' error - Octokit will properly handle the auth function as a strategy --- api/github/create-content-pr.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/github/create-content-pr.js b/api/github/create-content-pr.js index 53d9785..2791415 100644 --- a/api/github/create-content-pr.js +++ b/api/github/create-content-pr.js @@ -229,7 +229,9 @@ async function tryGitHubAppApproach(contentData, userToken) { return { success: false, error: `Failed to create installation auth: ${authError.message}` }; } - const octokit = new Octokit({ auth }); + const octokit = new Octokit({ + authStrategy: auth, + }); // Test installation token by making a simple API call console.log('๐Ÿงช Testing installation token with simple API call...'); From d1b6492c483408942d4b7b4cee9a28f32313fe8a Mon Sep 17 00:00:00 2001 From: ishaileshpant Date: Mon, 13 Oct 2025 22:32:23 +0530 Subject: [PATCH 4/5] Revert to direct auth function approach - Use auth function directly with Octokit constructor - This should work with @octokit/auth-app - Let's test this approach to see if it resolves the token issue --- api/github/create-content-pr.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/github/create-content-pr.js b/api/github/create-content-pr.js index 2791415..35a8130 100644 --- a/api/github/create-content-pr.js +++ b/api/github/create-content-pr.js @@ -230,7 +230,7 @@ async function tryGitHubAppApproach(contentData, userToken) { } const octokit = new Octokit({ - authStrategy: auth, + auth: auth, }); // Test installation token by making a simple API call From 8956198ccb7c0f9a7e780cadb14f925871a20649 Mon Sep 17 00:00:00 2001 From: ishaileshpant Date: Mon, 13 Oct 2025 22:35:50 +0530 Subject: [PATCH 5/5] Fix installation token approach - use direct API calls - Get installation token directly via GitHub API - Use token string with Octokit constructor - Test token with direct fetch API call instead of Octokit - This should resolve the JWT decoding errors --- api/github/create-content-pr.js | 51 +++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/api/github/create-content-pr.js b/api/github/create-content-pr.js index 35a8130..be8e1bb 100644 --- a/api/github/create-content-pr.js +++ b/api/github/create-content-pr.js @@ -215,33 +215,54 @@ async function tryGitHubAppApproach(contentData, userToken) { console.log(' - Installation ID:', targetInstallation.id); console.log(' - Private key length:', privateKey.length); - let auth; + // Get installation token directly + console.log('๐Ÿ”‘ Getting installation token...'); + let installationToken; try { - auth = createAppAuth({ - appId: process.env.GITHUB_APP_ID, - privateKey: privateKey, - installationId: targetInstallation.id, + const installationTokenResponse = await fetch(`https://api.github.com/app/installations/${targetInstallation.id}/access_tokens`, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${appToken.token}`, + 'Accept': 'application/vnd.github.v3+json' + } }); - console.log('โœ… Installation auth created successfully'); - } catch (authError) { - console.error('โŒ Failed to create installation auth:', authError.message); - console.error('Installation auth error details:', authError); - return { success: false, error: `Failed to create installation auth: ${authError.message}` }; + + if (!installationTokenResponse.ok) { + throw new Error(`Failed to create installation token: ${installationTokenResponse.status}`); + } + + const installationTokenData = await installationTokenResponse.json(); + installationToken = installationTokenData.token; + console.log('โœ… Installation token obtained successfully'); + console.log(' - Token length:', installationToken ? installationToken.length : 'null'); + } catch (tokenError) { + console.error('โŒ Failed to get installation token:', tokenError.message); + console.error('Installation token error details:', tokenError); + return { success: false, error: `Failed to get installation token: ${tokenError.message}` }; } const octokit = new Octokit({ - auth: auth, + auth: installationToken, }); // Test installation token by making a simple API call console.log('๐Ÿงช Testing installation token with simple API call...'); try { - const testResponse = await octokit.rest.apps.getInstallation({ - installation_id: targetInstallation.id + const testResponse = await fetch('https://api.github.com/repos/prepguides/prepguides.dev', { + headers: { + 'Authorization': `Bearer ${installationToken}`, + 'Accept': 'application/vnd.github.v3+json' + } }); + + if (!testResponse.ok) { + throw new Error(`Repository access failed: ${testResponse.status}`); + } + + const repoData = await testResponse.json(); console.log('โœ… Installation token test successful'); - console.log(' - Installation ID:', testResponse.data.id); - console.log(' - Account:', testResponse.data.account.login); + console.log(' - Repository:', repoData.full_name); + console.log(' - Default branch:', repoData.default_branch); } catch (testError) { console.error('โŒ Installation token test failed:', testError.message); console.error('Test error details:', testError);