feat: add settings page to analytics-frontend

This commit is contained in:
Usman Baig
2026-01-16 22:37:40 +01:00
parent 63921d5e04
commit e1cf7d4b13
6 changed files with 1343 additions and 0 deletions

60
lib/api/user.ts Normal file
View File

@@ -0,0 +1,60 @@
import apiRequest from './client'
export async function deleteAccount(password: string): Promise<void> {
// This goes to ciphera-auth
return apiRequest<void>('/auth/user', {
method: 'DELETE',
body: JSON.stringify({ password }),
})
}
export interface Session {
id: string
client_ip: string
user_agent: string
created_at: string
expires_at: string
is_current: boolean
}
export async function getUserSessions(): Promise<{ sessions: Session[] }> {
// Hash the current refresh token to identify current session
const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null
let currentTokenHash = ''
if (refreshToken) {
// Hash the refresh token using SHA-256
const encoder = new TextEncoder()
const data = encoder.encode(refreshToken)
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
currentTokenHash = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}
return apiRequest<{ sessions: Session[] }>('/auth/user/sessions', {
headers: currentTokenHash ? {
'X-Current-Session-Hash': currentTokenHash,
} : undefined,
})
}
export async function revokeSession(sessionId: string): Promise<void> {
return apiRequest<void>(`/auth/user/sessions/${sessionId}`, {
method: 'DELETE',
})
}
export interface UserPreferences {
email_notifications: {
new_file_received: boolean
file_downloaded: boolean
security_alerts: boolean
}
}
export async function updateUserPreferences(preferences: UserPreferences): Promise<void> {
return apiRequest<void>('/auth/user/preferences', {
method: 'PUT',
body: JSON.stringify(preferences),
})
}