admin frontend elkezdése, járművek tisztázása, frontend fejleszts
This commit is contained in:
@@ -474,6 +474,105 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the current user's account (soft delete).
|
||||
* DELETE /users/me with optional reason as query parameter.
|
||||
* On success, clears all local state and redirects to landing page.
|
||||
* If the backend returns is_last_admin=true, the response includes
|
||||
* a warning detail that the caller can display.
|
||||
*/
|
||||
async function deleteAccount(reason?: string): Promise<{ is_last_admin?: boolean }> {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const params: Record<string, string> = {}
|
||||
if (reason) {
|
||||
params.reason = reason
|
||||
}
|
||||
const res = await api.delete('/users/me', { params })
|
||||
// Check if backend returned is_last_admin warning
|
||||
const isLastAdmin = res.data?.is_last_admin === true
|
||||
// Clear all local state — same as logout but without API call
|
||||
token.value = null
|
||||
user.value = null
|
||||
myOrganizations.value = []
|
||||
localStorage.removeItem('access_token')
|
||||
localStorage.removeItem('refresh_token')
|
||||
router.push('/')
|
||||
return { is_last_admin: isLastAdmin }
|
||||
} catch (err: any) {
|
||||
// If the error response contains is_last_admin, pass it through
|
||||
if (err.response?.data?.is_last_admin) {
|
||||
error.value = err.response?.data?.detail || 'profile.deleteError'
|
||||
throw { ...err, is_last_admin: true }
|
||||
}
|
||||
error.value = err?.response?.data?.detail || 'profile.deleteError'
|
||||
throw err
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request account restoration code (Step 1 of Account Restore).
|
||||
* POST /auth/restore/request with { email }.
|
||||
* Sends a 6-digit OTP to the user's email.
|
||||
*/
|
||||
async function requestRestore(email: string): Promise<{ status: string; message: string }> {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const res = await api.post('/auth/restore/request', { email })
|
||||
return res.data as { status: string; message: string }
|
||||
} catch (err: any) {
|
||||
error.value = getErrorMessage(err, 'auth.restoreError')
|
||||
throw err
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify account restoration code and set new password (Step 2 of Account Restore).
|
||||
* POST /auth/restore/verify with { email, otp, new_password }.
|
||||
* On success, returns JWT tokens and auto-logs in the user.
|
||||
*/
|
||||
async function verifyRestore(email: string, otp: string, newPassword: string): Promise<void> {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const res = await api.post('/auth/restore/verify', {
|
||||
email,
|
||||
otp,
|
||||
new_password: newPassword,
|
||||
})
|
||||
const data = res.data as {
|
||||
access_token: string
|
||||
refresh_token?: string
|
||||
token_type: string
|
||||
}
|
||||
|
||||
// Persist token
|
||||
localStorage.setItem('access_token', data.access_token)
|
||||
if (data.refresh_token) {
|
||||
localStorage.setItem('refresh_token', data.refresh_token)
|
||||
}
|
||||
token.value = data.access_token
|
||||
|
||||
// Fetch user profile and organizations
|
||||
await fetchUser()
|
||||
await fetchMyOrganizations()
|
||||
} catch (err: any) {
|
||||
error.value = getErrorMessage(err, 'auth.restoreError')
|
||||
throw err
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear any stored error message.
|
||||
*/
|
||||
@@ -591,6 +690,9 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
completeKyc,
|
||||
updatePerson,
|
||||
changePassword,
|
||||
deleteAccount,
|
||||
requestRestore,
|
||||
verifyRestore,
|
||||
logout,
|
||||
init,
|
||||
clearError,
|
||||
|
||||
Reference in New Issue
Block a user