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:
parent
daa9656680
commit
55e99e002d
5 changed files with 178 additions and 63 deletions
|
|
@ -1,8 +1,7 @@
|
|||
// Auth service for LNbits integration
|
||||
import { ref } from 'vue'
|
||||
import { eventBus } from '@/core/event-bus'
|
||||
import { getAuthToken } from '@/lib/config/lnbits'
|
||||
import { config } from '@/lib/config'
|
||||
import { lnbitsAPI, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits'
|
||||
|
||||
export class AuthService {
|
||||
public isAuthenticated = ref(false)
|
||||
|
|
@ -21,39 +20,16 @@ export class AuthService {
|
|||
}
|
||||
|
||||
async checkAuth(): Promise<boolean> {
|
||||
const authToken = getAuthToken()
|
||||
|
||||
if (!authToken) {
|
||||
if (!lnbitsAPI.isAuthenticated()) {
|
||||
console.log('🔑 No auth token found - user needs to login')
|
||||
this.isAuthenticated.value = false
|
||||
this.user.value = null
|
||||
return false
|
||||
}
|
||||
|
||||
// Fetch current user data from API
|
||||
try {
|
||||
this.isLoading.value = true
|
||||
const API_BASE_URL = config.api.baseUrl || 'http://localhost:5006'
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/auth/nostr/me`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
console.log('🔑 Auth token invalid - user needs to login')
|
||||
this.logout()
|
||||
return false
|
||||
}
|
||||
console.warn(`🔑 Failed to fetch user data: ${response.status} - authentication may not be properly configured`)
|
||||
this.isAuthenticated.value = false
|
||||
this.user.value = null
|
||||
return false
|
||||
}
|
||||
|
||||
const userData = await response.json()
|
||||
const userData = await lnbitsAPI.getCurrentUser()
|
||||
|
||||
this.user.value = userData
|
||||
this.isAuthenticated.value = true
|
||||
|
|
@ -66,25 +42,25 @@ export class AuthService {
|
|||
console.warn('🔑 Authentication check failed:', error)
|
||||
this.isAuthenticated.value = false
|
||||
this.user.value = null
|
||||
// Clear invalid token
|
||||
lnbitsAPI.logout()
|
||||
return false
|
||||
} finally {
|
||||
this.isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async login(credentials: any): Promise<void> {
|
||||
async login(credentials: LoginCredentials): Promise<void> {
|
||||
this.isLoading.value = true
|
||||
|
||||
try {
|
||||
// Implement your login logic here
|
||||
// For demo purposes, we'll accept any credentials
|
||||
this.user.value = credentials
|
||||
await lnbitsAPI.login(credentials)
|
||||
const userData = await lnbitsAPI.getCurrentUser()
|
||||
|
||||
this.user.value = userData
|
||||
this.isAuthenticated.value = true
|
||||
|
||||
// Store auth state
|
||||
localStorage.setItem('auth', JSON.stringify(credentials))
|
||||
|
||||
eventBus.emit('auth:login', { user: credentials }, 'auth-service')
|
||||
eventBus.emit('auth:login', { user: userData }, 'auth-service')
|
||||
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error)
|
||||
|
|
@ -95,10 +71,31 @@ export class AuthService {
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
console.error('Registration failed:', error)
|
||||
eventBus.emit('auth:login-failed', { error }, 'auth-service')
|
||||
throw error
|
||||
} finally {
|
||||
this.isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
lnbitsAPI.logout()
|
||||
this.user.value = null
|
||||
this.isAuthenticated.value = false
|
||||
localStorage.removeItem('auth')
|
||||
|
||||
eventBus.emit('auth:logout', {}, 'auth-service')
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue