Till Tomczak 60f8a87028 Fix GitHub OAuth callback type errors completely
- Add USED_CALLBACK_URL export to fix type error
- Update the callback route to use the exported URL
- Enhance update-package.js to fix all OAuth-related issues

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-04-01 15:37:39 +02:00

122 lines
5.0 KiB
JavaScript
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Hilfsskript zur Aktualisierung der OAuth-Konfiguration im MYP-Frontend
*
* Dieses Skript wird automatisch beim Build ausgeführt, um sicherzustellen,
* dass die OAuth-Konfiguration korrekt ist.
*/
const fs = require('fs');
const path = require('path');
// Pfad zur OAuth-Callback-Route und OAuth-Konfiguration
const callbackRoutePath = path.join(__dirname, 'src/app/auth/login/callback/route.ts');
const oauthConfigPath = path.join(__dirname, 'src/server/auth/oauth.ts');
// Aktualisiere die OAuth-Konfiguration
try {
// 1. Prüfe, ob wir die USED_CALLBACK_URL exportieren müssen
let oauthContent = fs.readFileSync(oauthConfigPath, 'utf8');
if (!oauthContent.includes('export const USED_CALLBACK_URL')) {
console.log('✅ Aktualisiere OAuth-Konfiguration...');
// Füge die USED_CALLBACK_URL-Export hinzu
oauthContent = oauthContent.replace(
'// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI',
'// Berechne die Callback-URL\nexport const USED_CALLBACK_URL = getCallbackUrl();\n\n// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI'
);
// Schreibe die aktualisierte Datei
fs.writeFileSync(oauthConfigPath, oauthContent, 'utf8');
console.log('✅ OAuth-Konfiguration erfolgreich aktualisiert.');
} else {
console.log(' OAuth-Konfiguration ist bereits aktuell.');
}
} catch (error) {
console.error('❌ Fehler beim Aktualisieren der OAuth-Konfiguration:', error);
}
// Aktualisiere die OAuth-Callback-Route
try {
let callbackContent = fs.readFileSync(callbackRoutePath, 'utf8');
// Prüfe, ob Änderungen nötig sind
const needsUpdate =
callbackContent.includes('await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)') ||
!callbackContent.includes('USED_CALLBACK_URL');
if (needsUpdate) {
console.log('✅ Aktualisiere OAuth-Callback-Route...');
// 1. Aktualisiere den Import
if (!callbackContent.includes('USED_CALLBACK_URL')) {
callbackContent = callbackContent.replace(
'import { type GitHubUserResult, github, isValidCallbackHost } from "@/server/auth/oauth";',
'import { type GitHubUserResult, github, isValidCallbackHost, USED_CALLBACK_URL } from "@/server/auth/oauth";'
);
// Entferne den OAUTH_CALLBACK_URL-Import, wenn er nicht mehr benötigt wird
if (callbackContent.includes('OAUTH_CALLBACK_URL')) {
callbackContent = callbackContent.replace(
', OAUTH_CALLBACK_URL } from "@/utils/api-config"',
' } from "@/utils/api-config"'
);
}
}
// 2. Korrigiere die validateAuthorizationCode-Funktion
if (callbackContent.includes('await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)')) {
callbackContent = callbackContent.replace(
'await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)',
'await github.validateAuthorizationCode(code)'
);
}
// 3. Aktualisiere die Logging-Nachricht
if (callbackContent.includes('console.log(`GitHub OAuth Token-Validierung mit Callback-URL: ${OAUTH_CALLBACK_URL}`)')) {
callbackContent = callbackContent.replace(
'console.log(`GitHub OAuth Token-Validierung mit Callback-URL: ${OAUTH_CALLBACK_URL}`)',
'console.log(`GitHub OAuth Token-Validierung erfolgreich, verwendete Callback-URL: ${USED_CALLBACK_URL}`)'
);
} else if (callbackContent.includes('console.log("GitHub OAuth Token-Validierung erfolgreich")')) {
callbackContent = callbackContent.replace(
'console.log("GitHub OAuth Token-Validierung erfolgreich")',
'console.log(`GitHub OAuth Token-Validierung erfolgreich, verwendete Callback-URL: ${USED_CALLBACK_URL}`)'
);
}
// Schreibe die aktualisierte Datei
fs.writeFileSync(callbackRoutePath, callbackContent, 'utf8');
console.log('✅ OAuth-Callback-Route erfolgreich aktualisiert.');
} else {
console.log(' OAuth-Callback-Route ist bereits aktuell.');
}
} catch (error) {
console.error('❌ Fehler beim Aktualisieren der OAuth-Callback-Route:', error);
}
// Package.json aktualisieren, um das Skript vor dem Build auszuführen
try {
const packageJsonPath = path.join(__dirname, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
// Prüfe, ob das Skript bereits in den Build-Prozess integriert ist
if (packageJson.scripts.build === 'next build') {
console.log('✅ Aktualisiere package.json...');
// Füge das Skript zum Build-Prozess hinzu
packageJson.scripts.build = 'node update-package.js && next build';
// Schreibe die aktualisierte package.json
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
console.log('✅ package.json erfolgreich aktualisiert.');
} else {
console.log(' package.json ist bereits aktualisiert.');
}
} catch (error) {
console.error('❌ Fehler beim Aktualisieren der package.json:', error);
}
console.log('✅ OAuth-Konfiguration wurde erfolgreich vorbereitet.');