This major refactor consolidates the authentication system to use a single source of truth, eliminating timing issues and architectural complexity that was causing chat and payment functionality problems. Key Changes: • Remove old global useAuth composable and replace with useAuthService wrapper • Update all 25+ files to use consistent auth pattern via dependency injection • Eliminate dual auth detection workarounds from services (ChatService, PaymentService, etc.) • Fix TypeScript errors and add proper Uint8Array conversion for Nostr private keys • Consolidate auth state management to AuthService as single source of truth Benefits: • Resolves chat peer loading and message subscription timing issues • Fixes wallet detection problems for Lightning payments • Eliminates race conditions between global and injected auth • Maintains API compatibility while improving architecture • Reduces code complexity and improves maintainability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
177 lines
No EOL
4.9 KiB
TypeScript
177 lines
No EOL
4.9 KiB
TypeScript
// Auth service for LNbits integration
|
|
import { ref, computed } from 'vue'
|
|
import { BaseService } from '@/core/base/BaseService'
|
|
import { eventBus } from '@/core/event-bus'
|
|
import { lnbitsAPI, type LoginCredentials, type RegisterData, type User } from '@/lib/api/lnbits'
|
|
|
|
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<User | null>(null)
|
|
public isLoading = ref(false)
|
|
public error = ref<Error | null>(null)
|
|
|
|
// Computed properties for compatibility with global auth
|
|
public currentUser = computed(() => this.user.value)
|
|
public userDisplay = computed(() => {
|
|
if (!this.user.value) return null
|
|
|
|
return {
|
|
name: this.user.value.username || this.user.value.email || 'Anonymous',
|
|
username: this.user.value.username,
|
|
email: this.user.value.email,
|
|
id: this.user.value.id,
|
|
shortId: this.user.value.id.slice(0, 8) + '...' + this.user.value.id.slice(-8)
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 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()
|
|
|
|
if (this.isAuthenticated.value) {
|
|
eventBus.emit('auth:login', { user: this.user.value }, 'auth-service')
|
|
}
|
|
}
|
|
|
|
async checkAuth(): Promise<boolean> {
|
|
if (!lnbitsAPI.isAuthenticated()) {
|
|
this.debug('No auth token found - user needs to login')
|
|
this.isAuthenticated.value = false
|
|
this.user.value = null
|
|
return false
|
|
}
|
|
|
|
try {
|
|
this.isLoading.value = true
|
|
const userData = await lnbitsAPI.getCurrentUser()
|
|
|
|
this.user.value = userData
|
|
this.isAuthenticated.value = true
|
|
|
|
this.debug(`User authenticated: ${userData.username || userData.id} (${userData.pubkey?.slice(0, 8)})`)
|
|
|
|
return true
|
|
|
|
} catch (error) {
|
|
this.handleError(error, 'checkAuth')
|
|
this.isAuthenticated.value = false
|
|
this.user.value = null
|
|
// Clear invalid token
|
|
lnbitsAPI.logout()
|
|
return false
|
|
} finally {
|
|
this.isLoading.value = false
|
|
}
|
|
}
|
|
|
|
async login(credentials: LoginCredentials): Promise<void> {
|
|
this.isLoading.value = true
|
|
|
|
try {
|
|
await lnbitsAPI.login(credentials)
|
|
const userData = await lnbitsAPI.getCurrentUser()
|
|
|
|
this.user.value = userData
|
|
this.isAuthenticated.value = true
|
|
|
|
eventBus.emit('auth:login', { user: userData }, 'auth-service')
|
|
|
|
} catch (error) {
|
|
const err = this.handleError(error, 'login')
|
|
eventBus.emit('auth:login-failed', { error: err }, 'auth-service')
|
|
throw err
|
|
} finally {
|
|
this.isLoading.value = false
|
|
}
|
|
}
|
|
|
|
async register(data: RegisterData): Promise<void> {
|
|
this.isLoading.value = true
|
|
|
|
try {
|
|
await lnbitsAPI.register(data)
|
|
const userData = await lnbitsAPI.getCurrentUser()
|
|
|
|
this.user.value = userData
|
|
this.isAuthenticated.value = true
|
|
|
|
eventBus.emit('auth:login', { user: userData }, 'auth-service')
|
|
|
|
} catch (error) {
|
|
const err = this.handleError(error, 'register')
|
|
eventBus.emit('auth:login-failed', { error: err }, 'auth-service')
|
|
throw err
|
|
} finally {
|
|
this.isLoading.value = false
|
|
}
|
|
}
|
|
|
|
async logout(): Promise<void> {
|
|
lnbitsAPI.logout()
|
|
this.user.value = null
|
|
this.isAuthenticated.value = false
|
|
this.error.value = null
|
|
|
|
eventBus.emit('auth:logout', {}, 'auth-service')
|
|
}
|
|
|
|
async refresh(): Promise<void> {
|
|
// Re-fetch user data from API
|
|
await this.checkAuth()
|
|
}
|
|
|
|
async initialize(): Promise<void> {
|
|
// Alias for checkAuth for compatibility
|
|
await this.checkAuth()
|
|
}
|
|
|
|
async updatePassword(currentPassword: string, newPassword: string): Promise<void> {
|
|
try {
|
|
this.isLoading.value = true
|
|
const updatedUser = await lnbitsAPI.updatePassword(currentPassword, newPassword)
|
|
this.user.value = updatedUser
|
|
} catch (error) {
|
|
const err = this.handleError(error, 'updatePassword')
|
|
throw err
|
|
} finally {
|
|
this.isLoading.value = false
|
|
}
|
|
}
|
|
|
|
async updateProfile(data: Partial<User>): Promise<void> {
|
|
try {
|
|
this.isLoading.value = true
|
|
const updatedUser = await lnbitsAPI.updateProfile(data)
|
|
this.user.value = updatedUser
|
|
} catch (error) {
|
|
const err = this.handleError(error, 'updateProfile')
|
|
throw err
|
|
} finally {
|
|
this.isLoading.value = false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cleanup when service is disposed
|
|
*/
|
|
protected async onDispose(): Promise<void> {
|
|
this.logout()
|
|
this.debug('Auth service disposed')
|
|
}
|
|
}
|
|
|
|
// Export singleton instance
|
|
export const auth = new AuthService() |