Verbessere OAuth-Konfiguration mit explicit Redirect-URIs

- Konfiguriere Redirect-URI für GitHub OAuth explizit
- Füge NEXT_PUBLIC_FRONTEND_URL für konsistente OAuth-Callbacks hinzu
- Verwende hostname in Redirect-URIs für bessere Kompatibilität
- Aktualisiere Scripts, um Frontend-URL in Umgebungsvariablen zu setzen
- Füge bessere Fehlerdiagnose für OAuth-Prozess hinzu

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-04-01 14:02:17 +02:00
parent a9a1bf52db
commit a7760a12ce
5 changed files with 56 additions and 4 deletions

View File

@@ -39,6 +39,9 @@ cat > "$ENV_FILE" << EOL
# Backend API Konfiguration
NEXT_PUBLIC_API_URL=${BACKEND_URL}
# Frontend-URL für OAuth Callback
NEXT_PUBLIC_FRONTEND_URL=http://$(hostname):3000
# OAuth Konfiguration (falls nötig)
OAUTH_CLIENT_ID=client_id
OAUTH_CLIENT_SECRET=client_secret

View File

@@ -34,7 +34,12 @@ export async function GET(request: Request): Promise<Response> {
}
try {
const tokens = await github.validateAuthorizationCode(code);
// Explizit die redirect_uri übergeben, um Konsistenz zu gewährleisten
const callbackUrl = new URL("/auth/login/callback", process.env.NEXT_PUBLIC_FRONTEND_URL || "http://localhost:3000").toString();
// Übergabe der redirect_uri bei der Token-Validierung
const tokens = await github.validateAuthorizationCode(code, callbackUrl);
const githubUserResponse = await fetch("https://git.i.mercedes-benz.com/api/v3/user", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`,

View File

@@ -6,8 +6,13 @@ export const dynamic = "force-dynamic";
export async function GET(): Promise<Response> {
const state = generateState();
// Explizit die redirect_uri übergeben, um Konsistenz zu gewährleisten
const callbackUrl = new URL("/auth/login/callback", process.env.NEXT_PUBLIC_FRONTEND_URL || "http://localhost:3000").toString();
const url = await github.createAuthorizationURL(state, {
scopes: ["user"],
redirectURI: callbackUrl,
});
const ONE_HOUR = 60 * 60;
@@ -19,5 +24,9 @@ export async function GET(): Promise<Response> {
sameSite: "lax",
});
// Log zur Fehlersuche
console.log(`GitHub OAuth redirect zu: ${url.toString()}`);
console.log(`Verwendete Callback-URL: ${callbackUrl}`);
return Response.redirect(url);
}

View File

@@ -1,8 +1,38 @@
import { GitHub } from "arctic";
export const github = new GitHub(process.env.OAUTH_CLIENT_ID as string, process.env.OAUTH_CLIENT_SECRET as string, {
enterpriseDomain: "https://git.i.mercedes-benz.com",
});
// Bestimme die Callback-URL basierend auf der NEXT_PUBLIC_API_URL für das Backend
// und dem Standardport 3000 für das Frontend
const getCallbackUrl = () => {
// Extrahiere die Basis-URL (Hostname) aus der Backend-URL, falls vorhanden
let baseUrl = "http://localhost:3000";
try {
const apiUrlEnv = process.env.NEXT_PUBLIC_API_URL;
if (apiUrlEnv) {
// Wenn wir eine Backend-URL haben, extrahieren wir nur den Hostname-Teil für das Frontend
const apiUrl = new URL(apiUrlEnv);
const hostname = apiUrl.hostname;
// Verwende den Hostname, aber mit Frontend-Port 3000
baseUrl = `http://${hostname}:3000`;
}
} catch (error) {
console.error("Fehler beim Parsen der API-URL:", error);
// Fallback auf localhost
}
return `${baseUrl}/auth/login/callback`;
};
export const github = new GitHub(
process.env.OAUTH_CLIENT_ID as string,
process.env.OAUTH_CLIENT_SECRET as string,
{
enterpriseDomain: "https://git.i.mercedes-benz.com",
redirectURI: getCallbackUrl(),
}
);
export interface GitHubUserResult {
id: number;