import { NextRequest, NextResponse } from 'next/server' export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams const bookId = searchParams.get('bookId') const redirectUrl = searchParams.get('redirectUrl') || '/dashboard' if (!bookId) { return NextResponse.json( { error: 'bookId parameter is required' }, { status: 400 } ) } // Use the repository management OAuth app const clientId = process.env.GITHUB_REPO_CLIENT_ID if (!clientId) { return NextResponse.json( { error: 'GitHub repository OAuth not configured' }, { status: 500 } ) } // Use a simple state parameter for CSRF protection const state = Buffer.from(JSON.stringify({ bookId, redirectUrl })).toString('base64') // Use the custom callback URL for repository integration const customCallbackUrl = `${process.env.NEXT_PUBLIC_BASE_URL || request.nextUrl.origin}/auth/github/integration-callback` // GitHub OAuth URL for repository management const githubAuthUrl = new URL('https://github.com/login/oauth/authorize') githubAuthUrl.searchParams.set('client_id', clientId) githubAuthUrl.searchParams.set('redirect_uri', customCallbackUrl) githubAuthUrl.searchParams.set('scope', 'repo user:email') githubAuthUrl.searchParams.set('state', state) githubAuthUrl.searchParams.set('allow_signup', 'true') return NextResponse.redirect(githubAuthUrl.toString()) }