Enhance market module with authentication service integration

- Inject AuthService into useMarket for improved user authentication checks.
- Add error handling for missing AuthService and RelayHub to ensure proper module installation.
- Update comments to clarify the use of pubkey from either nostrStore or AuthService, indicating a need for confirmation on the preferred source.
- Refactor NostrmarketService to utilize injected RelayHub, improving error handling and code clarity.
This commit is contained in:
padreug 2025-09-05 04:02:22 +02:00
parent fec577ba39
commit e504b1f7e2
2 changed files with 18 additions and 3 deletions

View file

@ -16,11 +16,16 @@ export function useMarket() {
const nostrStore = useNostrStore() const nostrStore = useNostrStore()
const marketStore = useMarketStore() const marketStore = useMarketStore()
const relayHub = injectService(SERVICE_TOKENS.RELAY_HUB) as any const relayHub = injectService(SERVICE_TOKENS.RELAY_HUB) as any
const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any
if (!relayHub) { if (!relayHub) {
throw new Error('RelayHub not available. Make sure base module is installed.') throw new Error('RelayHub not available. Make sure base module is installed.')
} }
if (!authService) {
throw new Error('AuthService not available. Make sure base module is installed.')
}
// State // State
const isLoading = ref(false) const isLoading = ref(false)
const error = ref<Error | null>(null) const error = ref<Error | null>(null)
@ -48,6 +53,7 @@ export function useMarket() {
// Load market from naddr // Load market from naddr
// Parse naddr to get market data // Parse naddr to get market data
// TODO: Confirm if this should use nostrStore.account?.pubkey or authService.user.value?.pubkey
const marketData = { const marketData = {
identifier: naddr.split(':')[2] || 'default', identifier: naddr.split(':')[2] || 'default',
pubkey: naddr.split(':')[1] || nostrStore.account?.pubkey || '' pubkey: naddr.split(':')[1] || nostrStore.account?.pubkey || ''
@ -469,6 +475,7 @@ export function useMarket() {
// Load market data // Load market data
console.log('🛒 Loading basic market data...') console.log('🛒 Loading basic market data...')
// TODO: Confirm if this should use nostrStore.account?.pubkey or authService.user.value?.pubkey
await loadMarketData({ await loadMarketData({
identifier: 'default', identifier: 'default',
pubkey: nostrStore.account?.pubkey || '' pubkey: nostrStore.account?.pubkey || ''

View file

@ -1,5 +1,5 @@
import { finalizeEvent, type EventTemplate, nip04 } from 'nostr-tools' import { finalizeEvent, type EventTemplate, nip04 } from 'nostr-tools'
import { relayHub } from '@/lib/nostr/relayHub' import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { auth } from '@/composables/useAuth' import { auth } from '@/composables/useAuth'
import type { Stall, Product, Order } from '@/stores/market' import type { Stall, Product, Order } from '@/stores/market'
@ -68,6 +68,14 @@ export interface NostrmarketOrderStatus {
} }
export class NostrmarketService { export class NostrmarketService {
private get relayHub() {
const hub = injectService(SERVICE_TOKENS.RELAY_HUB)
if (!hub) {
throw new Error('RelayHub not available. Make sure base module is installed.')
}
return hub
}
/** /**
* Convert hex string to Uint8Array (browser-compatible) * Convert hex string to Uint8Array (browser-compatible)
*/ */
@ -150,7 +158,7 @@ export class NostrmarketService {
const prvkeyBytes = this.hexToUint8Array(prvkey) const prvkeyBytes = this.hexToUint8Array(prvkey)
const event = finalizeEvent(eventTemplate, prvkeyBytes) const event = finalizeEvent(eventTemplate, prvkeyBytes)
const result = await relayHub.publishEvent(event) const result = await this.relayHub.publishEvent(event)
console.log('Stall published to nostrmarket:', { console.log('Stall published to nostrmarket:', {
stallId: stall.id, stallId: stall.id,
@ -193,7 +201,7 @@ export class NostrmarketService {
const prvkeyBytes = this.hexToUint8Array(prvkey) const prvkeyBytes = this.hexToUint8Array(prvkey)
const event = finalizeEvent(eventTemplate, prvkeyBytes) const event = finalizeEvent(eventTemplate, prvkeyBytes)
const result = await relayHub.publishEvent(event) const result = await this.relayHub.publishEvent(event)
console.log('Product published to nostrmarket:', { console.log('Product published to nostrmarket:', {
productId: product.id, productId: product.id,