- Eliminate console logging from the authentication initialization and login processes in useAuth.ts for cleaner code. - Streamline wallet computation in useTicketPurchase.ts by removing unnecessary logging while maintaining functionality. - Refactor LNBits API methods to reduce logging, enhancing code clarity and maintainability.
164 lines
No EOL
3.8 KiB
TypeScript
164 lines
No EOL
3.8 KiB
TypeScript
import { ref, computed } from 'vue'
|
|
import { lnbitsAPI, type User, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits'
|
|
|
|
const currentUser = ref<User | null>(null)
|
|
const isLoading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
export function useAuth() {
|
|
const isAuthenticated = computed(() => !!currentUser.value)
|
|
|
|
/**
|
|
* Initialize authentication on app start
|
|
*/
|
|
async function initialize(): Promise<void> {
|
|
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<void> {
|
|
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<void> {
|
|
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<void> {
|
|
// Clear local state
|
|
lnbitsAPI.logout()
|
|
currentUser.value = null
|
|
error.value = null
|
|
}
|
|
|
|
/**
|
|
* Update user password
|
|
*/
|
|
async function updatePassword(currentPassword: string, newPassword: string): Promise<void> {
|
|
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<User>): Promise<void> {
|
|
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()
|