Refactor services to extend BaseService for improved structure and dependency management

- Update AuthService and RelayHub to extend BaseService, introducing standardized initialization and metadata handling.
- Implement service-specific initialization methods in both services, enhancing error handling and logging.
- Modify NostrmarketService to inherit from BaseService, ensuring consistent dependency management and initialization.
- Refactor market module to dynamically import NostrmarketService, improving service registration and initialization flow.
- Enhance debug logging across services for better traceability during initialization and operation.
This commit is contained in:
padreug 2025-09-05 06:41:19 +02:00
parent 8d4c389f71
commit dc4da570a7
5 changed files with 151 additions and 50 deletions

View file

@ -1,15 +1,27 @@
// Auth service for LNbits integration
import { ref } from 'vue'
import { BaseService } from '@/core/base/BaseService'
import { eventBus } from '@/core/event-bus'
import { lnbitsAPI, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits'
export class AuthService {
export class AuthService extends BaseService {
// Service metadata
protected readonly metadata = {
name: 'AuthService',
version: '1.0.0',
dependencies: [] // Auth service has no dependencies on other services
}
// Public state
public isAuthenticated = ref(false)
public user = ref<any>(null)
public isLoading = ref(false)
async initialize(): Promise<void> {
console.log('🔑 Initializing auth service...')
/**
* Service-specific initialization (called by BaseService)
*/
protected async onInitialize(): Promise<void> {
this.debug('Initializing auth service...')
// Check for existing auth state and fetch user data
await this.checkAuth()
@ -21,7 +33,7 @@ export class AuthService {
async checkAuth(): Promise<boolean> {
if (!lnbitsAPI.isAuthenticated()) {
console.log('🔑 No auth token found - user needs to login')
this.debug('No auth token found - user needs to login')
this.isAuthenticated.value = false
this.user.value = null
return false
@ -34,12 +46,12 @@ export class AuthService {
this.user.value = userData
this.isAuthenticated.value = true
console.log('🔑 User authenticated:', userData.username || userData.id, userData.pubkey?.slice(0, 8))
this.debug(`User authenticated: ${userData.username || userData.id} (${userData.pubkey?.slice(0, 8)})`)
return true
} catch (error) {
console.warn('🔑 Authentication check failed:', error)
this.handleError(error, 'checkAuth')
this.isAuthenticated.value = false
this.user.value = null
// Clear invalid token
@ -63,9 +75,9 @@ export class AuthService {
eventBus.emit('auth:login', { user: userData }, 'auth-service')
} catch (error) {
console.error('Login failed:', error)
eventBus.emit('auth:login-failed', { error }, 'auth-service')
throw error
const err = this.handleError(error, 'login')
eventBus.emit('auth:login-failed', { error: err }, 'auth-service')
throw err
} finally {
this.isLoading.value = false
}
@ -84,9 +96,9 @@ export class AuthService {
eventBus.emit('auth:login', { user: userData }, 'auth-service')
} catch (error) {
console.error('Registration failed:', error)
eventBus.emit('auth:login-failed', { error }, 'auth-service')
throw error
const err = this.handleError(error, 'register')
eventBus.emit('auth:login-failed', { error: err }, 'auth-service')
throw err
} finally {
this.isLoading.value = false
}
@ -104,6 +116,14 @@ export class AuthService {
// Re-fetch user data from API
await this.checkAuth()
}
/**
* Cleanup when service is disposed
*/
protected async onDispose(): Promise<void> {
this.logout()
this.debug('Auth service disposed')
}
}
// Export singleton instance