Refactor AuthService and market components for improved functionality and error handling

- Integrate LNbits API for authentication in AuthService, replacing token management with direct API calls for user data.
- Enhance login and registration processes to utilize the new API, improving user experience and error handling.
- Update market components to include detailed logging and fallback mechanisms for offline scenarios, ensuring better resilience during market data loading.
- Refactor market preloader to handle connection timeouts and provide sample data as a fallback, enhancing user experience in offline mode.
This commit is contained in:
padreug 2025-09-05 03:07:55 +02:00
parent daa9656680
commit 55e99e002d
5 changed files with 178 additions and 63 deletions

View file

@ -70,8 +70,31 @@ export function useMarket() {
// Load market data from Nostr events
const loadMarketData = async (marketData: any) => {
try {
console.log('🛒 Loading market data for:', { identifier: marketData.identifier, pubkey: marketData.pubkey?.slice(0, 8) })
// Check if we can query events (relays are connected)
if (!isConnected.value) {
console.log('🛒 Not connected to relays, creating default market')
// Create default market without trying to fetch from Nostr
const market = {
d: marketData.identifier,
pubkey: marketData.pubkey,
relays: config.nostr.relays,
selected: true,
opts: {
name: 'Demo Market (Offline)',
description: 'Demo market running in offline mode',
merchants: [],
ui: {}
}
}
marketStore.addMarket(market)
marketStore.setActiveMarket(market)
return
}
// Load market data from Nostr events
// Fetch market configuration event
const events = await relayHub.queryEvents([
{
@ -80,6 +103,8 @@ export function useMarket() {
'#d': [marketData.identifier]
}
])
console.log('🛒 Found', events.length, 'market events')
// Process market events
@ -90,7 +115,7 @@ export function useMarket() {
const market = {
d: marketData.identifier,
pubkey: marketData.pubkey,
relays: config.market.supportedRelays,
relays: config.nostr.relays,
selected: true,
opts: JSON.parse(marketEvent.content)
}
@ -103,7 +128,7 @@ export function useMarket() {
const market = {
d: marketData.identifier,
pubkey: marketData.pubkey,
relays: config.market.supportedRelays,
relays: config.nostr.relays,
selected: true,
opts: {
name: 'Ariège Market',
@ -122,7 +147,7 @@ export function useMarket() {
const market = {
d: marketData.identifier,
pubkey: marketData.pubkey,
relays: config.market.supportedRelays,
relays: config.nostr.relays,
selected: true,
opts: {
name: 'Default Market',
@ -159,6 +184,8 @@ export function useMarket() {
authors: merchants
}
])
console.log('🛒 Found', events.length, 'stall events for', merchants.length, 'merchants')
// Process stall events
@ -221,6 +248,8 @@ export function useMarket() {
authors: merchants
}
])
console.log('🛒 Found', events.length, 'product events for', merchants.length, 'merchants')
// Process product events
@ -463,35 +492,46 @@ export function useMarket() {
// Connect to market
const connectToMarket = async () => {
try {
console.log('🛒 Checking RelayHub connection...')
// Use existing relay hub connection (should already be connected by base module)
isConnected.value = relayHub.isConnected.value
console.log('🛒 RelayHub connected:', isConnected.value)
if (!isConnected.value) {
console.warn('RelayHub not connected, attempting to connect...')
await relayHub.connect()
isConnected.value = relayHub.isConnected.value
if (!isConnected.value) {
throw new Error('Failed to connect to Nostr relays')
}
console.warn('🛒 RelayHub not connected - this is expected if authentication is not complete')
// Don't try to connect here - let the base module handle connections
// Just proceed with offline/demo mode
console.log('🛒 Proceeding in offline mode')
}
// Market connected successfully
console.log('🛒 Market connected successfully')
// Load market data
console.log('🛒 Loading basic market data...')
await loadMarketData({
identifier: 'default',
pubkey: nostrStore.account?.pubkey || ''
})
// Load stalls and products
await loadStalls()
await loadProducts()
// Load stalls and products only if connected
if (isConnected.value) {
console.log('🛒 Loading stalls...')
await loadStalls()
console.log('🛒 Loading products...')
await loadProducts()
} else {
console.log('🛒 Skipping stalls/products loading - not connected to relays')
// Add sample products for demo mode
console.log('🛒 Adding sample products for offline demo...')
addSampleProducts()
}
// Subscribe to updates
console.log('🛒 Subscribing to market updates...')
subscribeToMarketUpdates()
} catch (err) {
console.error('🛒 Failed to connect to market:', err)
error.value = err instanceof Error ? err : new Error('Failed to connect to market')
throw err
}