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 }
|
||||
38
src/lib/config/lnbits.ts
Normal file
38
src/lib/config/lnbits.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// LNBits API Configuration
|
||||
export const LNBITS_CONFIG = {
|
||||
// Base URL for the LNBits API
|
||||
// This should point to your LNBits instance
|
||||
API_BASE_URL: import.meta.env.VITE_LNBITS_API_URL || '/api/v1',
|
||||
|
||||
// Whether to enable debug logging
|
||||
DEBUG: import.meta.env.VITE_LNBITS_DEBUG === 'true',
|
||||
|
||||
// Timeout for API requests (in milliseconds)
|
||||
REQUEST_TIMEOUT: 10000,
|
||||
|
||||
// Auth token storage key
|
||||
AUTH_TOKEN_KEY: 'lnbits_access_token',
|
||||
|
||||
// User storage key
|
||||
USER_STORAGE_KEY: 'lnbits_user_data'
|
||||
}
|
||||
|
||||
// Helper function to get the full API URL
|
||||
export function getApiUrl(endpoint: string): string {
|
||||
return `${LNBITS_CONFIG.API_BASE_URL}${endpoint}`
|
||||
}
|
||||
|
||||
// Helper function to get auth token from storage
|
||||
export function getAuthToken(): string | null {
|
||||
return localStorage.getItem(LNBITS_CONFIG.AUTH_TOKEN_KEY)
|
||||
}
|
||||
|
||||
// Helper function to set auth token in storage
|
||||
export function setAuthToken(token: string): void {
|
||||
localStorage.setItem(LNBITS_CONFIG.AUTH_TOKEN_KEY, token)
|
||||
}
|
||||
|
||||
// Helper function to remove auth token from storage
|
||||
export function removeAuthToken(): void {
|
||||
localStorage.removeItem(LNBITS_CONFIG.AUTH_TOKEN_KEY)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue