fix(deployment): fixed api key generation during deployment

This commit is contained in:
Waleed Latif
2025-04-07 15:38:05 -07:00
parent dcb88e66bb
commit 1f22ed2ec5
3 changed files with 45 additions and 15 deletions
+2
View File
@@ -1,3 +1,4 @@
import { unstable_noStore as noStore } from 'next/cache'
import { NextRequest, NextResponse } from 'next/server'
import OpenAI from 'openai'
import { createLogger } from '@/lib/logs/console-logger'
@@ -196,6 +197,7 @@ export async function POST(req: NextRequest) {
try {
const body = (await req.json()) as RequestBody
noStore()
// Destructure history along with other fields
const { prompt, generationType, context, stream = false, history = [] } = body
+41 -11
View File
@@ -66,6 +66,30 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
.where(eq(apiKey.userId, workflowData.userId))
.limit(1)
let userKey = null
// If no API key exists, create one automatically
if (userApiKey.length === 0) {
try {
const newApiKey = generateApiKey()
await db.insert(apiKey).values({
id: uuidv4(),
userId: workflowData.userId,
name: 'Default API Key',
key: newApiKey,
createdAt: new Date(),
updatedAt: new Date(),
})
userKey = newApiKey
logger.info(`[${requestId}] Generated new API key for user: ${workflowData.userId}`)
} catch (keyError) {
// If key generation fails, log the error but continue with the request
logger.error(`[${requestId}] Failed to generate API key:`, keyError)
}
} else {
userKey = userApiKey[0].key
}
// Check if the workflow has meaningful changes that would require redeployment
let needsRedeployment = false
if (workflowData.deployedState) {
@@ -78,7 +102,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
logger.info(`[${requestId}] Successfully retrieved deployment info: ${id}`)
return createSuccessResponse({
apiKey: userApiKey.length > 0 ? userApiKey[0].key : null,
apiKey: userKey,
isDeployed: workflowData.isDeployed,
deployedAt: workflowData.deployedAt,
needsRedeployment,
@@ -134,16 +158,22 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
// If no API key exists, create one
if (userApiKey.length === 0) {
const newApiKey = generateApiKey()
await db.insert(apiKey).values({
id: uuidv4(),
userId,
name: 'Default API Key',
key: newApiKey,
createdAt: new Date(),
updatedAt: new Date(),
})
userKey = newApiKey
try {
const newApiKey = generateApiKey()
await db.insert(apiKey).values({
id: uuidv4(),
userId,
name: 'Default API Key',
key: newApiKey,
createdAt: new Date(),
updatedAt: new Date(),
})
userKey = newApiKey
logger.info(`[${requestId}] Generated new API key for user: ${userId}`)
} catch (keyError) {
// If key generation fails, log the error but continue with the request
logger.error(`[${requestId}] Failed to generate API key:`, keyError)
}
} else {
userKey = userApiKey[0].key
}
@@ -102,13 +102,11 @@ export function DeploymentControls({
},
{
label: 'API Key',
content: apiKey || 'No API key found. Visit your account settings to create one.',
content: apiKey,
},
{
label: 'Example curl command',
content: apiKey
? `curl -X POST -H "X-API-Key: ${apiKey}" -H "Content-Type: application/json"${inputFormatExample} ${endpoint}`
: `You need an API key to call this endpoint. Visit your account settings to create one.`,
content: `curl -X POST -H "X-API-Key: ${apiKey}" -H "Content-Type: application/json"${inputFormatExample} ${endpoint}`,
},
],
needsRedeployment,