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
132
src/lib/api/lnbits.ts
Normal file
132
src/lib/api/lnbits.ts
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
interface LoginCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
interface RegisterData {
|
||||
username: string
|
||||
email?: string
|
||||
password: string
|
||||
password_repeat: string
|
||||
}
|
||||
|
||||
interface AuthResponse {
|
||||
access_token: string
|
||||
user_id: string
|
||||
username?: string
|
||||
email?: string
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username?: string
|
||||
email?: string
|
||||
pubkey?: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
import { getApiUrl, getAuthToken, setAuthToken, removeAuthToken } from '@/lib/config/lnbits'
|
||||
|
||||
class LnbitsAPI {
|
||||
private accessToken: string | null = null
|
||||
|
||||
constructor() {
|
||||
// Try to load token from localStorage
|
||||
this.accessToken = getAuthToken()
|
||||
}
|
||||
|
||||
private async request<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
const url = getApiUrl(endpoint)
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
}
|
||||
|
||||
if (this.accessToken) {
|
||||
(headers as Record<string, string>)['Authorization'] = `Bearer ${this.accessToken}`
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}))
|
||||
throw new Error(errorData.detail || `HTTP ${response.status}: ${response.statusText}`)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
async login(credentials: LoginCredentials): Promise<AuthResponse> {
|
||||
const response = await this.request<AuthResponse>('/auth', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(credentials),
|
||||
})
|
||||
|
||||
this.accessToken = response.access_token
|
||||
setAuthToken(response.access_token)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
async register(data: RegisterData): Promise<AuthResponse> {
|
||||
const response = await this.request<AuthResponse>('/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
|
||||
this.accessToken = response.access_token
|
||||
setAuthToken(response.access_token)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
try {
|
||||
await this.request('/auth/logout', {
|
||||
method: 'POST',
|
||||
})
|
||||
} finally {
|
||||
this.accessToken = null
|
||||
removeAuthToken()
|
||||
}
|
||||
}
|
||||
|
||||
async getCurrentUser(): Promise<User> {
|
||||
return this.request<User>('/auth')
|
||||
}
|
||||
|
||||
async updatePassword(currentPassword: string, newPassword: string): Promise<User> {
|
||||
return this.request<User>('/auth/password', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({
|
||||
current_password: currentPassword,
|
||||
new_password: newPassword,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
async updateProfile(data: Partial<User>): Promise<User> {
|
||||
return this.request<User>('/auth/update', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
return !!this.accessToken
|
||||
}
|
||||
|
||||
getAccessToken(): string | null {
|
||||
return this.accessToken
|
||||
}
|
||||
}
|
||||
|
||||
export const lnbitsAPI = new LnbitsAPI()
|
||||
export type { LoginCredentials, RegisterData, AuthResponse, User }
|
||||
Loading…
Add table
Add a link
Reference in a new issue