|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { verification } from '@sim/db/schema' |
| 3 | +import { eq } from 'drizzle-orm' |
| 4 | +import type { NextRequest } from 'next/server' |
| 5 | +import { NextResponse } from 'next/server' |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns the original OAuth authorize parameters stored in the verification record |
| 9 | + * for a given consent code. Used by the consent page to reconstruct the authorize URL |
| 10 | + * when switching accounts. |
| 11 | + */ |
| 12 | +export async function GET(request: NextRequest) { |
| 13 | + const consentCode = request.nextUrl.searchParams.get('consent_code') |
| 14 | + if (!consentCode) { |
| 15 | + return NextResponse.json({ error: 'consent_code is required' }, { status: 400 }) |
| 16 | + } |
| 17 | + |
| 18 | + const [record] = await db |
| 19 | + .select({ value: verification.value }) |
| 20 | + .from(verification) |
| 21 | + .where(eq(verification.identifier, consentCode)) |
| 22 | + .limit(1) |
| 23 | + |
| 24 | + if (!record) { |
| 25 | + return NextResponse.json({ error: 'Invalid or expired consent code' }, { status: 404 }) |
| 26 | + } |
| 27 | + |
| 28 | + const data = JSON.parse(record.value) as { |
| 29 | + clientId: string |
| 30 | + redirectURI: string |
| 31 | + scope: string[] |
| 32 | + codeChallenge: string |
| 33 | + codeChallengeMethod: string |
| 34 | + state: string | null |
| 35 | + nonce: string | null |
| 36 | + } |
| 37 | + |
| 38 | + return NextResponse.json({ |
| 39 | + client_id: data.clientId, |
| 40 | + redirect_uri: data.redirectURI, |
| 41 | + scope: data.scope.join(' '), |
| 42 | + code_challenge: data.codeChallenge, |
| 43 | + code_challenge_method: data.codeChallengeMethod, |
| 44 | + state: data.state, |
| 45 | + nonce: data.nonce, |
| 46 | + response_type: 'code', |
| 47 | + }) |
| 48 | +} |
0 commit comments