Refactor authentication architecture to eliminate dual auth complexity
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>
This commit is contained in:
parent
5633aa154b
commit
4feb5459cc
27 changed files with 210 additions and 518 deletions
|
|
@ -1,8 +1,8 @@
|
|||
// Auth service for LNbits integration
|
||||
import { ref } from 'vue'
|
||||
import { ref, computed } from 'vue'
|
||||
import { BaseService } from '@/core/base/BaseService'
|
||||
import { eventBus } from '@/core/event-bus'
|
||||
import { lnbitsAPI, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits'
|
||||
import { lnbitsAPI, type LoginCredentials, type RegisterData, type User } from '@/lib/api/lnbits'
|
||||
|
||||
export class AuthService extends BaseService {
|
||||
// Service metadata
|
||||
|
|
@ -14,8 +14,23 @@ export class AuthService extends BaseService {
|
|||
|
||||
// Public state
|
||||
public isAuthenticated = ref(false)
|
||||
public user = ref<any>(null)
|
||||
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)
|
||||
|
|
@ -104,10 +119,11 @@ export class AuthService extends BaseService {
|
|||
}
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
async logout(): Promise<void> {
|
||||
lnbitsAPI.logout()
|
||||
this.user.value = null
|
||||
this.isAuthenticated.value = false
|
||||
this.error.value = null
|
||||
|
||||
eventBus.emit('auth:logout', {}, 'auth-service')
|
||||
}
|
||||
|
|
@ -116,6 +132,37 @@ export class AuthService extends BaseService {
|
|||
// 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue