feat: Enhance authentication and user management with detailed logging
- Add console logging for authentication initialization, login attempts, and user retrieval to improve debugging and traceability. - Introduce a new getCurrentUser function in useAuth for better user data management. - Update useTicketPurchase to include detailed logging for user wallets and balance checks, enhancing visibility into wallet states. - Refactor LNBits API request and response logging for clearer error handling and debugging.
This commit is contained in:
parent
217ca70334
commit
0cc0bf3555
4 changed files with 75 additions and 44 deletions
|
|
@ -13,14 +13,23 @@ export function useAuth() {
|
||||||
*/
|
*/
|
||||||
async function initialize(): Promise<void> {
|
async function initialize(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
console.log('Initializing authentication...')
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
if (lnbitsAPI.isAuthenticated()) {
|
const isAuth = lnbitsAPI.isAuthenticated()
|
||||||
|
console.log('Is authenticated:', isAuth)
|
||||||
|
|
||||||
|
if (isAuth) {
|
||||||
|
console.log('Getting current user...')
|
||||||
const user = await lnbitsAPI.getCurrentUser()
|
const user = await lnbitsAPI.getCurrentUser()
|
||||||
|
console.log('Current user set:', user)
|
||||||
currentUser.value = user
|
currentUser.value = user
|
||||||
|
} else {
|
||||||
|
console.log('Not authenticated, skipping user fetch')
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('Authentication initialization error:', err)
|
||||||
error.value = err instanceof Error ? err.message : 'Failed to initialize authentication'
|
error.value = err instanceof Error ? err.message : 'Failed to initialize authentication'
|
||||||
// Clear invalid token
|
// Clear invalid token
|
||||||
await logout()
|
await logout()
|
||||||
|
|
@ -34,15 +43,19 @@ export function useAuth() {
|
||||||
*/
|
*/
|
||||||
async function login(credentials: LoginCredentials): Promise<void> {
|
async function login(credentials: LoginCredentials): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
console.log('Login attempt with credentials:', { username: credentials.username })
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
await lnbitsAPI.login(credentials)
|
await lnbitsAPI.login(credentials)
|
||||||
|
console.log('Login successful, getting user details...')
|
||||||
|
|
||||||
// Get user details
|
// Get user details
|
||||||
const user = await lnbitsAPI.getCurrentUser()
|
const user = await lnbitsAPI.getCurrentUser()
|
||||||
|
console.log('User details after login:', user)
|
||||||
currentUser.value = user
|
currentUser.value = user
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('Login error:', err)
|
||||||
error.value = err instanceof Error ? err.message : 'Login failed'
|
error.value = err instanceof Error ? err.message : 'Login failed'
|
||||||
throw err
|
throw err
|
||||||
} finally {
|
} finally {
|
||||||
|
|
@ -139,6 +152,19 @@ export function useAuth() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Get current user from API
|
||||||
|
async function getCurrentUser(): Promise<User | null> {
|
||||||
|
try {
|
||||||
|
console.log('Getting current user from API...')
|
||||||
|
const user = await lnbitsAPI.getCurrentUser()
|
||||||
|
console.log('Current user from API:', user)
|
||||||
|
return user
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting current user:', error)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// State
|
// State
|
||||||
currentUser: computed(() => currentUser.value),
|
currentUser: computed(() => currentUser.value),
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,25 @@ export function useTicketPurchase() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const userWallets = computed(() => currentUser.value?.wallets || [])
|
const userWallets = computed(() => {
|
||||||
const hasWalletWithBalance = computed(() =>
|
const wallets = currentUser.value?.wallets || []
|
||||||
userWallets.value.some((wallet: any) => wallet.balance_msat > 0)
|
console.log('User wallets computed:', {
|
||||||
)
|
currentUser: currentUser.value,
|
||||||
|
wallets: wallets,
|
||||||
|
walletCount: wallets.length,
|
||||||
|
hasWallets: wallets.length > 0
|
||||||
|
})
|
||||||
|
return wallets
|
||||||
|
})
|
||||||
|
const hasWalletWithBalance = computed(() => {
|
||||||
|
const hasBalance = userWallets.value.some((wallet: any) => wallet.balance_msat > 0)
|
||||||
|
console.log('Wallet balance check:', {
|
||||||
|
wallets: userWallets.value,
|
||||||
|
hasBalance: hasBalance,
|
||||||
|
walletBalances: userWallets.value.map((w: any) => ({ id: w.id, balance: w.balance_msat }))
|
||||||
|
})
|
||||||
|
return hasBalance
|
||||||
|
})
|
||||||
|
|
||||||
// Generate QR code for Lightning payment
|
// Generate QR code for Lightning payment
|
||||||
async function generateQRCode(bolt11: string) {
|
async function generateQRCode(bolt11: string) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { Event, Ticket } from '../types/event'
|
import type { Event, EventsApiError, Ticket } from '../types/event'
|
||||||
import { config } from '@/lib/config'
|
import { config } from '@/lib/config'
|
||||||
import { lnbitsAPI } from './lnbits'
|
import { lnbitsAPI } from './lnbits'
|
||||||
|
|
||||||
|
|
@ -72,7 +72,7 @@ export async function purchaseTicket(eventId: string): Promise<{ payment_hash: s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function payInvoiceWithWallet(paymentRequest: string, _walletId: string, adminKey: string): Promise<{ payment_hash: string; fee_msat: number; preimage: string }> {
|
export async function payInvoiceWithWallet(paymentRequest: string, walletId: string, adminKey: string): Promise<{ payment_hash: string; fee_msat: number; preimage: string }> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE_URL}/api/v1/payments`,
|
`${API_BASE_URL}/api/v1/payments`,
|
||||||
|
|
|
||||||
|
|
@ -85,52 +85,39 @@ class LnbitsAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug logging
|
// Debug logging
|
||||||
if (import.meta.env.VITE_LNBITS_DEBUG === 'true') {
|
|
||||||
console.log('LNBits API Request:', {
|
console.log('LNBits API Request:', {
|
||||||
url,
|
url,
|
||||||
method: options.method || 'GET',
|
method: options.method || 'GET',
|
||||||
headers: options.headers,
|
headers: headers,
|
||||||
body: options.body
|
hasAccessToken: !!this.accessToken,
|
||||||
|
endpoint
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers,
|
headers,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Debug logging
|
|
||||||
if (import.meta.env.VITE_LNBITS_DEBUG === 'true') {
|
|
||||||
console.log('LNBits API Response:', {
|
console.log('LNBits API Response:', {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
|
ok: response.ok,
|
||||||
url: response.url
|
url: response.url
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
let errorMessage = `HTTP ${response.status}: ${response.statusText}`
|
const errorText = await response.text()
|
||||||
try {
|
|
||||||
const errorData = await response.json()
|
|
||||||
errorMessage = errorData.detail || errorData.message || errorMessage
|
|
||||||
} catch {
|
|
||||||
// If we can't parse JSON, use the default error message
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug logging for errors
|
|
||||||
if (import.meta.env.VITE_LNBITS_DEBUG === 'true') {
|
|
||||||
console.error('LNBits API Error:', {
|
console.error('LNBits API Error:', {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
url: response.url,
|
errorText
|
||||||
error: errorMessage
|
|
||||||
})
|
})
|
||||||
|
throw new Error(`API request failed: ${response.status} ${response.statusText}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(errorMessage)
|
const data = await response.json()
|
||||||
}
|
console.log('LNBits API Response Data:', data)
|
||||||
|
return data
|
||||||
return response.json()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async login(credentials: LoginCredentials): Promise<AuthResponse> {
|
async login(credentials: LoginCredentials): Promise<AuthResponse> {
|
||||||
|
|
@ -164,7 +151,10 @@ class LnbitsAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCurrentUser(): Promise<User> {
|
async getCurrentUser(): Promise<User> {
|
||||||
return this.request<User>('/auth')
|
console.log('getCurrentUser called, accessToken:', this.accessToken ? 'present' : 'missing')
|
||||||
|
const user = await this.request<User>('/user')
|
||||||
|
console.log('getCurrentUser response:', user)
|
||||||
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
async updatePassword(currentPassword: string, newPassword: string): Promise<User> {
|
async updatePassword(currentPassword: string, newPassword: string): Promise<User> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue