feat: Enhance debugging and authentication in Nostr chat
- Add detailed console logging in ChatComponent to track message sending attempts, selected peers, and authentication status. - Integrate authentication checks in useNostrChat, ensuring users have valid Nostr keypairs before sending messages. - Implement logic to generate and store new Nostr keys if none are found, improving user experience and key management.
This commit is contained in:
parent
59e0496ad9
commit
8e94216c02
2 changed files with 118 additions and 6 deletions
|
|
@ -537,6 +537,19 @@ const sendMessage = async () => {
|
||||||
if (!selectedPeer.value || !messageInput.value.trim()) return
|
if (!selectedPeer.value || !messageInput.value.trim()) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Add debugging information
|
||||||
|
console.log('🔍 ChatComponent: Attempting to send message...')
|
||||||
|
console.log('🔍 ChatComponent: Selected peer:', selectedPeer.value)
|
||||||
|
console.log('🔍 ChatComponent: Message content:', messageInput.value)
|
||||||
|
|
||||||
|
// Check authentication status
|
||||||
|
const keyStatus = nostrChat.getNostrKeyStatus()
|
||||||
|
console.log('🔍 ChatComponent: Nostr key status:', keyStatus)
|
||||||
|
|
||||||
|
// Check if user is logged in
|
||||||
|
console.log('🔍 ChatComponent: Is logged in:', nostrChat.isLoggedIn.value)
|
||||||
|
console.log('🔍 ChatComponent: Current user:', nostrChat.currentUser.value)
|
||||||
|
|
||||||
await sendNostrMessage(selectedPeer.value.pubkey, messageInput.value)
|
await sendNostrMessage(selectedPeer.value.pubkey, messageInput.value)
|
||||||
messageInput.value = ''
|
messageInput.value = ''
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { ref, computed, readonly } from 'vue'
|
import { ref, computed, readonly } from 'vue'
|
||||||
|
|
||||||
import { nip04, finalizeEvent, type EventTemplate } from 'nostr-tools'
|
import { nip04, finalizeEvent, type EventTemplate, generateSecretKey, getPublicKey } from 'nostr-tools'
|
||||||
import { hexToBytes } from '@/lib/utils/crypto'
|
import { hexToBytes, bytesToHex } from '@/lib/utils/crypto'
|
||||||
import { getAuthToken } from '@/lib/config/lnbits'
|
import { getAuthToken } from '@/lib/config/lnbits'
|
||||||
import { config } from '@/lib/config'
|
import { config } from '@/lib/config'
|
||||||
import { useRelayHub } from './useRelayHub'
|
import { useRelayHub } from './useRelayHub'
|
||||||
|
import { useAuth } from './useAuth'
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
export interface ChatMessage {
|
export interface ChatMessage {
|
||||||
|
|
@ -67,9 +68,11 @@ export function useNostrChat() {
|
||||||
// Use the centralized relay hub
|
// Use the centralized relay hub
|
||||||
const relayHub = useRelayHub()
|
const relayHub = useRelayHub()
|
||||||
|
|
||||||
|
// Use the main authentication system
|
||||||
|
const auth = useAuth()
|
||||||
|
|
||||||
// State
|
// State
|
||||||
const messages = ref<Map<string, ChatMessage[]>>(new Map())
|
const messages = ref<Map<string, ChatMessage[]>>(new Map())
|
||||||
const currentUser = ref<{ pubkey: string; prvkey: string } | null>(null)
|
|
||||||
const processedMessageIds = ref(new Set<string>())
|
const processedMessageIds = ref(new Set<string>())
|
||||||
const onMessageAdded = ref<((peerPubkey: string) => void) | null>(null)
|
const onMessageAdded = ref<((peerPubkey: string) => void) | null>(null)
|
||||||
|
|
||||||
|
|
@ -82,9 +85,97 @@ export function useNostrChat() {
|
||||||
// Store peers globally
|
// Store peers globally
|
||||||
const peers = ref<any[]>([])
|
const peers = ref<any[]>([])
|
||||||
|
|
||||||
// Computed - use relay hub's connection status
|
// Computed - use relay hub's connection status and auth system
|
||||||
const isConnected = computed(() => relayHub.isConnected.value)
|
const isConnected = computed(() => relayHub.isConnected.value)
|
||||||
const isLoggedIn = computed(() => !!currentUser.value)
|
const isLoggedIn = computed(() => auth.isAuthenticated.value)
|
||||||
|
|
||||||
|
// Get current user from auth system
|
||||||
|
const currentUser = computed(() => {
|
||||||
|
const user = auth.currentUser.value
|
||||||
|
if (!user || !user.pubkey) {
|
||||||
|
console.log('🔍 useNostrChat: No user or no pubkey available:', user)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('🔍 useNostrChat: User data available:', {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
pubkey: user.pubkey,
|
||||||
|
hasExtensions: user.extensions,
|
||||||
|
extra: user.extra,
|
||||||
|
fullUser: user
|
||||||
|
})
|
||||||
|
|
||||||
|
// Check if we have a stored Nostr keypair for this user
|
||||||
|
const storageKey = `nostr_keys_${user.id}`
|
||||||
|
let storedKeys = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
const stored = localStorage.getItem(storageKey)
|
||||||
|
if (stored) {
|
||||||
|
storedKeys = JSON.parse(stored)
|
||||||
|
console.log('🔍 useNostrChat: Found stored Nostr keys:', storedKeys)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to parse stored Nostr keys:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have stored keys, use them
|
||||||
|
if (storedKeys && storedKeys.prvkey && storedKeys.pubkey) {
|
||||||
|
console.log('🔍 useNostrChat: Using stored Nostr keys')
|
||||||
|
return {
|
||||||
|
pubkey: storedKeys.pubkey,
|
||||||
|
prvkey: storedKeys.prvkey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no stored keys, generate new ones
|
||||||
|
console.log('🔍 useNostrChat: No stored keys found, generating new Nostr keypair')
|
||||||
|
try {
|
||||||
|
const privateKeyBytes = generateSecretKey()
|
||||||
|
const privateKey = bytesToHex(privateKeyBytes)
|
||||||
|
const publicKey = getPublicKey(privateKeyBytes)
|
||||||
|
|
||||||
|
// Store the new keys
|
||||||
|
const newKeys = { pubkey: publicKey, prvkey: privateKey }
|
||||||
|
localStorage.setItem(storageKey, JSON.stringify(newKeys))
|
||||||
|
|
||||||
|
console.log('🔍 useNostrChat: Generated and stored new Nostr keypair')
|
||||||
|
|
||||||
|
return newKeys
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to generate Nostr keypair:', error)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Check if user has complete Nostr keypair
|
||||||
|
const hasNostrKeys = computed(() => {
|
||||||
|
const user = currentUser.value
|
||||||
|
return user && user.pubkey && user.prvkey
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get Nostr key status for debugging
|
||||||
|
const getNostrKeyStatus = () => {
|
||||||
|
const user = auth.currentUser.value
|
||||||
|
if (!user) {
|
||||||
|
return { hasUser: false, hasPubkey: false, hasPrvkey: false, message: 'No user logged in' }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.pubkey) {
|
||||||
|
return { hasUser: true, hasPubkey: false, hasPrvkey: false, message: 'User logged in but no Nostr public key' }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we can get the private key from somewhere
|
||||||
|
// For now, we don't have access to it
|
||||||
|
return {
|
||||||
|
hasUser: true,
|
||||||
|
hasPubkey: true,
|
||||||
|
hasPrvkey: false,
|
||||||
|
message: 'User has Nostr public key but private key not available',
|
||||||
|
pubkey: user.pubkey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get unread count for a peer
|
// Get unread count for a peer
|
||||||
const getUnreadCount = (peerPubkey: string): number => {
|
const getUnreadCount = (peerPubkey: string): number => {
|
||||||
|
|
@ -587,6 +678,11 @@ export function useNostrChat() {
|
||||||
throw new Error('No user logged in - please authenticate first')
|
throw new Error('No user logged in - please authenticate first')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if we have the required Nostr keypair
|
||||||
|
if (!currentUser.value.prvkey) {
|
||||||
|
throw new Error('Nostr private key not available. Please ensure your LNBits account has Nostr keys configured.')
|
||||||
|
}
|
||||||
|
|
||||||
// Check if we have a pool and are connected
|
// Check if we have a pool and are connected
|
||||||
if (!relayHub.isConnected.value) {
|
if (!relayHub.isConnected.value) {
|
||||||
console.warn('Not connected to relays - attempting to connect...')
|
console.warn('Not connected to relays - attempting to connect...')
|
||||||
|
|
@ -812,7 +908,10 @@ export function useNostrChat() {
|
||||||
|
|
||||||
// Peer management methods
|
// Peer management methods
|
||||||
loadPeers,
|
loadPeers,
|
||||||
subscribeToAllPeersForNotifications
|
subscribeToAllPeersForNotifications,
|
||||||
|
currentUser,
|
||||||
|
hasNostrKeys,
|
||||||
|
getNostrKeyStatus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue