Fix chat history authentication and conversation saving
- Fix critical async/await bug in chat API token verification - Add comprehensive authentication debugging logs - Fix conversations API Zod schema validation for query parameters - Remove problematic CircularProgress import causing build warnings - Improve error handling and user feedback in chat component The main issue was that verifyToken() was called without await, causing the chat API to receive a Promise object instead of the user payload, resulting in undefined userId and failed conversation persistence. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -29,15 +29,21 @@ export async function POST(request: NextRequest) {
|
||||
// Try to get user from authentication (optional for backward compatibility)
|
||||
let userId: string | null = null
|
||||
const authHeader = request.headers.get('authorization')
|
||||
console.log('Chat API - authHeader present:', !!authHeader)
|
||||
if (authHeader?.startsWith('Bearer ')) {
|
||||
try {
|
||||
const token = authHeader.substring(7)
|
||||
const payload = verifyToken(token)
|
||||
console.log('Chat API - token extracted, length:', token.length)
|
||||
const payload = await verifyToken(token)
|
||||
console.log('Chat API - token payload:', payload)
|
||||
userId = payload.userId
|
||||
console.log('Chat API - userId extracted from token:', userId)
|
||||
} catch (error) {
|
||||
// Continue without authentication for backward compatibility
|
||||
console.log('Chat without authentication')
|
||||
console.log('Chat API - authentication failed:', error.message)
|
||||
}
|
||||
} else {
|
||||
console.log('Chat API - no valid auth header')
|
||||
}
|
||||
|
||||
// Handle conversation logic
|
||||
@@ -97,6 +103,11 @@ export async function POST(request: NextRequest) {
|
||||
const aiResponse = await generateBiblicalResponse(message, locale, conversationHistory)
|
||||
|
||||
// Save messages to database if user is authenticated
|
||||
console.log('Chat API - conversation saving check:', {
|
||||
userId: userId ? 'present' : 'null',
|
||||
finalConversationId: finalConversationId ? 'present' : 'null',
|
||||
willSave: !!(userId && finalConversationId)
|
||||
})
|
||||
if (userId && finalConversationId) {
|
||||
await prisma.$transaction([
|
||||
// Save user message
|
||||
|
||||
Reference in New Issue
Block a user