feat: Enhance Nostr chat functionality with improved authentication and error handling

- Integrate authentication token retrieval for loading peers and current user data.
- Update API endpoints to use a configurable base URL for better flexibility.
- Implement enhanced error handling for API responses, including JSON parsing and logging.
- Refactor relay connection logic to utilize a SimplePool for managing multiple relays efficiently.
- Improve user feedback with console logs for connection status and error details.
This commit is contained in:
padreug 2025-08-05 23:32:36 +02:00
parent 3bd87ee712
commit c30e4ba6c5
2 changed files with 159 additions and 88 deletions

View file

@ -279,6 +279,8 @@ import { Badge } from '@/components/ui/badge'
import { ScrollArea } from '@/components/ui/scroll-area'
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'
import { useNostrChat } from '@/composables/useNostrChat'
import { getAuthToken } from '@/lib/config/lnbits'
import { config } from '@/lib/config'
// Types
interface Peer {
@ -336,24 +338,45 @@ const currentMessages = computed(() => {
const loadPeers = async () => {
try {
isLoading.value = true
const response = await fetch('/users/api/v1/nostr/pubkeys', {
const authToken = getAuthToken()
if (!authToken) {
console.warn('No authentication token found - cannot load peers')
return
}
const API_BASE_URL = config.api.baseUrl || 'http://localhost:5006'
const response = await fetch(`${API_BASE_URL}/api/v1/auth/nostr/pubkeys`, {
headers: {
'Authorization': `Bearer ${localStorage.getItem('admin_token')}`
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
})
console.log('Peers API Response status:', response.status)
if (!response.ok) {
throw new Error('Failed to load peers')
const errorText = await response.text()
console.error('Peers API Error:', response.status, errorText)
throw new Error(`Failed to load peers: ${response.status}`)
}
const data = await response.json()
peers.value = data.map((peer: any) => ({
user_id: peer.user_id,
username: peer.username,
pubkey: peer.pubkey
}))
const responseText = await response.text()
console.log('Peers API Response text:', responseText)
console.log(`Loaded ${peers.value.length} peers`)
try {
const data = JSON.parse(responseText)
peers.value = data.map((peer: any) => ({
user_id: peer.user_id,
username: peer.username,
pubkey: peer.pubkey
}))
console.log(`Loaded ${peers.value.length} peers`)
} catch (parseError) {
console.error('JSON Parse Error for peers:', parseError)
console.error('Response was:', responseText)
throw new Error('Invalid JSON response from peers API')
}
} catch (error) {
console.error('Failed to load peers:', error)
} finally {