/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { api } from '@/lib/api'
import type { LoginSession } from '@/stores/auth-store'
import type {
ApiResponse,
UserProfile,
UpdateUserRequest,
UpdateUserSettingsRequest,
DeleteAccountRequest,
CheckinStatusResponse,
CheckinResponse,
} from './types'
// ============================================================================
// User Profile APIs
// ============================================================================
/**
* Get current user profile
*/
export async function getUserProfile(): Promise> {
const res = await api.get('/api/user/self')
return res.data
}
/**
* Update user profile
*/
export async function updateUserProfile(
data: UpdateUserRequest
): Promise {
const res = await api.put('/api/user/self', data, {
acceptAuthRotation: Boolean(data.password),
})
return res.data
}
/**
* Update user settings
*/
export async function updateUserSettings(
data: UpdateUserSettingsRequest
): Promise {
const res = await api.put('/api/user/setting', data)
return res.data
}
/**
* Update interface language preference
*/
export async function updateUserLanguage(
language: string
): Promise {
const res = await api.put('/api/user/self', { language })
return res.data
}
/**
* Delete user account
*/
export async function deleteUserAccount(
data?: DeleteAccountRequest
): Promise {
const res = await api.delete('/api/user/self', { data })
return res.data
}
/**
* Generate/regenerate system access token
*/
export async function generateAccessToken(): Promise> {
const res = await api.get('/api/user/token')
return res.data
}
// ============================================================================
// Account Binding APIs
// ============================================================================
/**
* Send email verification code
*/
export async function sendEmailVerification(
email: string,
turnstileToken?: string
): Promise {
const params = new URLSearchParams({ email })
if (turnstileToken) {
params.append('turnstile', turnstileToken)
}
const res = await api.get(`/api/verification?${params}`)
return res.data
}
/**
* Bind email account
*/
export async function bindEmail(
email: string,
code: string
): Promise {
const res = await api.post('/api/oauth/email/bind', {
email,
code,
})
return res.data
}
/**
* Bind WeChat account
*/
export async function bindWeChat(code: string): Promise {
const res = await api.post(
'/api/oauth/wechat/bind',
{ code },
{ skipBusinessError: true, skipErrorHandler: true }
)
return res.data
}
export interface TelegramBindFlow {
flow_token: string
callback_url: string
expires_at: number
}
export async function startTelegramBind(): Promise<
ApiResponse
> {
const res = await api.post('/api/oauth/telegram/bind/start')
return res.data
}
// ============================================================================
// Login Session APIs
// ============================================================================
export async function getLoginSessions(): Promise> {
const res = await api.get('/api/user/sessions')
return res.data
}
export async function revokeLoginSession(sid: string): Promise {
const res = await api.delete(`/api/user/sessions/${encodeURIComponent(sid)}`)
return res.data
}
export async function revokeOtherLoginSessions(): Promise {
const res = await api.post('/api/user/sessions/revoke-others')
return res.data
}
// ============================================================================
// Custom OAuth Binding APIs
// ============================================================================
export interface CustomOAuthBinding {
provider_id: string
provider_name: string
external_id?: string
}
/**
* Get current user's custom OAuth bindings
*/
export async function getSelfOAuthBindings(): Promise<
ApiResponse
> {
const res = await api.get('/api/user/oauth/bindings')
return res.data
}
/**
* Unbind a custom OAuth provider for current user
*/
export async function unbindCustomOAuth(
providerId: string
): Promise {
const res = await api.delete(`/api/user/oauth/bindings/${providerId}`)
return res.data
}
// ============================================================================
// Checkin APIs
// ============================================================================
/**
* Get checkin status for a specific month
*/
export async function getCheckinStatus(
month: string
): Promise> {
const res = await api.get(`/api/user/checkin?month=${month}`)
return res.data
}
/**
* Perform daily checkin
*/
export async function performCheckin(
turnstileToken?: string
): Promise> {
const url = turnstileToken
? `/api/user/checkin?turnstile=${encodeURIComponent(turnstileToken)}`
: '/api/user/checkin'
const res = await api.post(url)
return res.data
}