refactor: Transition to authentication system and remove identity management
- Replace identity management with a new authentication system across the application. - Update App.vue to integrate LoginDialog and remove PasswordDialog. - Modify Navbar.vue to handle user authentication state and logout functionality. - Enhance Home.vue to display user information upon login. - Implement routing changes in index.ts to enforce authentication requirements for protected routes.
This commit is contained in:
parent
5ceb12ca3b
commit
be4ab13b32
11 changed files with 1065 additions and 96 deletions
166
src/composables/useAuth.ts
Normal file
166
src/composables/useAuth.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
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> {
|
||||
try {
|
||||
await lnbitsAPI.logout()
|
||||
} catch (err) {
|
||||
console.error('Logout error:', err)
|
||||
} finally {
|
||||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue