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:
@@ -24,7 +24,6 @@ import {
|
||||
DialogTitle,
|
||||
DialogContent,
|
||||
DialogActions,
|
||||
CircularProgress,
|
||||
} from '@mui/material'
|
||||
import {
|
||||
Chat,
|
||||
@@ -139,6 +138,7 @@ export default function FloatingChat() {
|
||||
const checkAuthStatus = useCallback(async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken')
|
||||
console.log('Chat - checkAuthStatus: token from localStorage:', token ? 'present' : 'null')
|
||||
if (token) {
|
||||
// Verify token with the server
|
||||
const response = await fetch('/api/auth/me', {
|
||||
@@ -146,18 +146,25 @@ export default function FloatingChat() {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
console.log('Chat - auth verification response:', response.ok)
|
||||
|
||||
if (response.ok) {
|
||||
setAuthToken(token)
|
||||
setIsAuthenticated(true)
|
||||
console.log('Chat - Auth state set: authenticated')
|
||||
} else {
|
||||
localStorage.removeItem('authToken')
|
||||
setIsAuthenticated(false)
|
||||
setAuthToken(null)
|
||||
console.log('Chat - Auth verification failed, cleared state')
|
||||
}
|
||||
} else {
|
||||
console.log('Chat - No token in localStorage')
|
||||
setIsAuthenticated(false)
|
||||
setAuthToken(null)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Auth check failed:', error)
|
||||
console.error('Chat - Auth check failed:', error)
|
||||
setIsAuthenticated(false)
|
||||
setAuthToken(null)
|
||||
}
|
||||
@@ -342,8 +349,12 @@ export default function FloatingChat() {
|
||||
}
|
||||
|
||||
// Add authentication if available
|
||||
console.log('Chat - authToken value:', authToken ? 'present' : 'null')
|
||||
if (authToken) {
|
||||
headers['Authorization'] = `Bearer ${authToken}`
|
||||
console.log('Chat - Authorization header added')
|
||||
} else {
|
||||
console.log('Chat - No authToken, skipping Authorization header')
|
||||
}
|
||||
|
||||
const response = await fetch('/api/chat', {
|
||||
@@ -569,7 +580,7 @@ export default function FloatingChat() {
|
||||
</Box>
|
||||
) : isLoadingConversations ? (
|
||||
<Box sx={{ display: 'flex', justifyContent: 'center', py: 3 }}>
|
||||
<CircularProgress size={24} />
|
||||
⟳
|
||||
</Box>
|
||||
) : conversations.length === 0 ? (
|
||||
<Box sx={{ textAlign: 'center', py: 3 }}>
|
||||
|
||||
Reference in New Issue
Block a user