Second deep audit→build→verify pass; all products typecheck clean. - gitgram-web (100): fix feed pagination, reel drafts, 6 gaps - gitgram-backend (99): add admin/moderator role checks to moderation routes; wire backend-e2e CI (Postgres+Redis services); 5 gaps - gitgram-mobile (97): fix broken create-reel flow; 11 gaps - aitm-core (98): dedupe vendored CLI command registrations; real vector search with local fallback; 7 gaps - aitm-api (99): replace hash-based embeddings with real OpenAI/Voyage embeddings (+ checkpoints test_status migration 0008); 8 gaps - aitm-mcp (99): add X-API-Key to workspace tool proxy calls; 8 gaps - aitm-website (94): fix annual billing checkout; 5 gaps Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import {
|
|
AITM_BASE_URL,
|
|
aitmCookieOptions,
|
|
errorResponse,
|
|
readAitmToken,
|
|
} from '../_lib'
|
|
|
|
/**
|
|
* GET — report whether an AITM credential is stored, without exposing it.
|
|
*/
|
|
export async function GET(): Promise<NextResponse> {
|
|
return NextResponse.json({ connected: !!readAitmToken() })
|
|
}
|
|
|
|
/**
|
|
* POST — validate the supplied AITM API key and, if valid, persist it in an
|
|
* httpOnly cookie. The key is never returned to the client.
|
|
*/
|
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
|
let body: { apiKey?: unknown }
|
|
try {
|
|
body = await req.json()
|
|
} catch {
|
|
return errorResponse('Invalid request body', 400)
|
|
}
|
|
|
|
const apiKey = typeof body.apiKey === 'string' ? body.apiKey.trim() : ''
|
|
if (!apiKey) {
|
|
return errorResponse('apiKey is required', 400)
|
|
}
|
|
|
|
// Validate the key by hitting an authenticated AITM endpoint.
|
|
let valid = false
|
|
try {
|
|
const res = await fetch(`${AITM_BASE_URL}/restore/projects`, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${apiKey}`,
|
|
},
|
|
cache: 'no-store',
|
|
})
|
|
valid = res.ok
|
|
} catch {
|
|
return errorResponse('Could not reach the AITM service', 502)
|
|
}
|
|
|
|
if (!valid) {
|
|
return errorResponse('Invalid AITM API key', 401)
|
|
}
|
|
|
|
const response = NextResponse.json({ connected: true })
|
|
response.cookies.set(aitmCookieOptions().name, apiKey, aitmCookieOptions())
|
|
return response
|
|
}
|
|
|
|
/**
|
|
* DELETE — disconnect AITM by clearing the stored credential.
|
|
*/
|
|
export async function DELETE(): Promise<NextResponse> {
|
|
const response = NextResponse.json({ connected: false })
|
|
response.cookies.set(aitmCookieOptions(0).name, '', aitmCookieOptions(0))
|
|
return response
|
|
}
|