import { ref, computed } from 'vue' import { lnbitsAPI, type User, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits' const currentUser = ref(null) const isLoading = ref(false) const error = ref(null) export function useAuth() { const isAuthenticated = computed(() => !!currentUser.value) /** * Initialize authentication on app start */ async function initialize(): Promise { try { isLoading.value = true error.value = null if (lnbitsAPI.isAuthenticated()) { const user = await lnbitsAPI.getCurrentUser() currentUser.value = user } } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to initialize authentication' // Clear invalid token await logout() } finally { isLoading.value = false } } /** * Login with username and password */ async function login(credentials: LoginCredentials): Promise { try { isLoading.value = true error.value = null await lnbitsAPI.login(credentials) // Get user details const user = await lnbitsAPI.getCurrentUser() currentUser.value = user } catch (err) { error.value = err instanceof Error ? err.message : 'Login failed' throw err } finally { isLoading.value = false } } /** * Register new user */ async function register(data: RegisterData): Promise { try { isLoading.value = true error.value = null await lnbitsAPI.register(data) // Get user details const user = await lnbitsAPI.getCurrentUser() currentUser.value = user } catch (err) { error.value = err instanceof Error ? err.message : 'Registration failed' throw err } finally { isLoading.value = false } } /** * Logout and clear user data */ async function logout(): Promise { // Clear local state lnbitsAPI.logout() currentUser.value = null error.value = null } /** * Update user password */ async function updatePassword(currentPassword: string, newPassword: string): Promise { try { isLoading.value = true error.value = null const updatedUser = await lnbitsAPI.updatePassword(currentPassword, newPassword) currentUser.value = updatedUser } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to update password' throw err } finally { isLoading.value = false } } /** * Update user profile */ async function updateProfile(data: Partial): Promise { try { isLoading.value = true error.value = null const updatedUser = await lnbitsAPI.updateProfile(data) currentUser.value = updatedUser } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to update profile' throw err } finally { isLoading.value = false } } /** * Check if user is authenticated */ function checkAuth(): boolean { return lnbitsAPI.isAuthenticated() } /** * Get user display info */ const userDisplay = computed(() => { if (!currentUser.value) return null return { name: currentUser.value.username || currentUser.value.email || 'Anonymous', username: currentUser.value.username, email: currentUser.value.email, id: currentUser.value.id, shortId: currentUser.value.id.slice(0, 8) + '...' + currentUser.value.id.slice(-8) } }) return { // State currentUser: computed(() => currentUser.value), isAuthenticated, isLoading, error, userDisplay, // Actions initialize, login, register, logout, updatePassword, updateProfile, checkAuth } } // Export singleton instance for global state export const auth = useAuth()