Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add import export pages (#2413)
Leendert de Borst4aa287f710e8
diff --git a/apps/web/src/App.tsx b/apps/web/src/App.tsxindex 9716e322a..0644337e8 100644--- a/apps/web/src/App.tsx+++ b/apps/web/src/App.tsx@@ -17,6 +17,8 @@ import ItemView from '@/pages/items/View';import NotFound from '@/pages/NotFound';import AppsSettings from '@/pages/settings/Apps';import GeneralSettings from '@/pages/settings/General';+import ImportExport from '@/pages/settings/importexport/ImportExport';+import ResetVault from '@/pages/settings/importexport/ResetVault';import ChangePassword from '@/pages/settings/security/ChangePassword';import DeleteAccount from '@/pages/settings/security/DeleteAccount';import Disable2Fa from '@/pages/settings/security/Disable2Fa';@@ -60,6 +62,8 @@ const App: React.FC = () => (<Route path="/settings/security/enable-2fa" element={<Enable2Fa />} /><Route path="/settings/security/disable-2fa" element={<Disable2Fa />} /><Route path="/settings/security/delete-account" element={<DeleteAccount />} />+ <Route path="/settings/import-export" element={<ImportExport />} />+ <Route path="/settings/import-export/reset-vault" element={<ResetVault />} /><Route path="/settings/apps" element={<AppsSettings />} /></Route>diff --git a/apps/web/src/pages/settings/importexport/ImportExport.tsx b/apps/web/src/pages/settings/importexport/ImportExport.tsxnew file mode 100644index 000000000..af1203b70--- /dev/null+++ b/apps/web/src/pages/settings/importexport/ImportExport.tsx@@ -0,0 +1,305 @@+import { MasterPasswordService, PasswordVerificationResult } from '@aliasvault/client/auth/MasterPasswordService';+import { AliasVaultCsvExportService } from '@aliasvault/client/transfer/export/AliasVaultCsvExportService';+import { AvexExportService } from '@aliasvault/client/transfer/export/AvexExportService';+import { AvuxExportService } from '@aliasvault/client/transfer/export/AvuxExportService';+import { downloadBytes } from '@aliasvault/client/utilities/FileDownload';+import React, { useState } from 'react';+import { useTranslation } from 'react-i18next';++import ExportPasswordModal from '@/components/settings/importexport/ExportPasswordModal';+import ImportServices from '@/components/settings/importexport/ImportServices';+import ResetVaultSection from '@/components/settings/importexport/ResetVaultSection';+import Button from '@/components/shared/Button';+import Card from '@/components/shared/Card';+import PageContent from '@/components/shared/PageContent';+import PageHeader from '@/components/shared/PageHeader';+import PasswordConfirmationModal from '@/components/shared/PasswordConfirmationModal';+import { useAuth } from '@/context/AuthContext';+import { useConfirmModal } from '@/context/ConfirmModalContext';+import { useDb } from '@/context/DbContext';+import { useLoading } from '@/context/LoadingContext';+import { useNotifications } from '@/context/NotificationContext';+import { usePageTitle } from '@/hooks/usePageTitle';++/**+ * The export formats.+ */+enum ExportType {+ Csv = 'Csv',+ Avux = 'Avux',+ Avex = 'Avex',+}++/**+ * The import/export settings page.+ */+const ImportExport: React.FC = () => {+ const { t } = useTranslation();+ const tk = 'pages.main.settings.importExport.importExport';+ usePageTitle(t(`${tk}.PageTitle`));+ const dbContext = useDb();+ const { username } = useAuth();+ const { showConfirmation } = useConfirmModal();+ const { showLoading, hideLoading } = useLoading();+ const notifications = useNotifications();++ const [showPasswordConfirmation, setShowPasswordConfirmation] = useState(false);+ const [passwordError, setPasswordError] = useState('');+ const [showExportPasswordModal, setShowExportPasswordModal] = useState(false);+ const [currentExportType, setCurrentExportType] = useState<ExportType>(ExportType.Csv);++ const isDebugBuild = import.meta.env.DEV;++ /**+ * The export file name: the date and the username.+ */+ const getExportFileName = (extension: string): string => {+ const dateStr = new Date().toISOString().substring(0, 10);+ return `aliasvault-export-${username ?? ''}-${dateStr}.${extension}`;+ };++ /**+ * Ask for confirmation, then for the master password.+ */+ const showExportConfirmation = async (exportType: ExportType): Promise<void> => {+ setCurrentExportType(exportType);+ const confirmMessage = exportType === ExportType.Avex ? t(`${tk}.ExportEncryptedWarningMessage`) : t(`${tk}.ExportWarningMessage`);++ const confirmed = await showConfirmation(t(`${tk}.ExportConfirmTitle`), confirmMessage, t('sharedResources.Confirm'), t('sharedResources.Cancel'));+ if (!confirmed) {+ return;+ }++ // Every export requires the master password.+ setPasswordError('');+ setShowPasswordConfirmation(true);+ };++ /**+ * The current vault as .avux bytes, the base of both the .avux and the .avex export.+ */+ const generateAvuxBytes = (): Uint8Array => {+ const sqliteClient = dbContext.sqliteClient;+ if (!sqliteClient) {+ throw new Error('Vault is not available');+ }+ const data = sqliteClient.importExport.getExportData();+ return AvuxExportService.exportToAvux(data.items, data.folders, data.tags, data.itemTags, data.fieldDefinitions, data.logos, username ?? '');+ };++ /**+ * Export the vault to CSV.+ */+ const exportVaultCsv = async (): Promise<void> => {+ try {+ const sqliteClient = dbContext.sqliteClient;+ if (!sqliteClient) {+ throw new Error('Vault is not available');+ }+ const items = [...sqliteClient.items.getAll(), ...sqliteClient.items.getArchived()];+ downloadBytes(getExportFileName('csv'), AliasVaultCsvExportService.exportItemsToCsv(items, item => sqliteClient.items.getTotpCodesForItem(item)), 'text/csv');+ notifications.addSuccessMessage(t(`${tk}.ExportSuccessMessage`), true);+ } catch (error) {+ console.error('[Export] Error downloading file:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ }+ };++ /**+ * Export the vault to .avux.+ */+ const exportVaultAvux = async (): Promise<void> => {+ showLoading(t(`${tk}.ExportingVaultMessage`));+ try {+ downloadBytes(getExportFileName('avux'), generateAvuxBytes());+ notifications.addSuccessMessage(t(`${tk}.ExportSuccessMessage`), true);+ } catch (error) {+ console.error('[Export] Error exporting vault to .avux format:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } finally {+ hideLoading();+ }+ };++ /**+ * Export the vault to .avex, encrypted with the given password.+ */+ const exportVaultAvex = async (exportPassword: string): Promise<void> => {+ showLoading(t(`${tk}.ExportingVaultMessage`));+ try {+ const avexBytes = await AvexExportService.encryptToAvex(generateAvuxBytes(), exportPassword, username ?? '');+ downloadBytes(getExportFileName('avex'), avexBytes);+ notifications.addSuccessMessage(t(`${tk}.ExportSuccessMessage`), true);+ } catch (error) {+ console.error('[Export] Error exporting vault to .avex format:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } finally {+ hideLoading();+ }+ };++ /**+ * Debug only: the raw SQLite vault file as-is.+ */+ const exportVaultSqlite = (): void => {+ showLoading('Exporting SQLite vault...');+ try {+ const sqliteClient = dbContext.sqliteClient;+ if (!sqliteClient) {+ throw new Error('Vault is not available');+ }+ downloadBytes(getExportFileName('sqlite'), sqliteClient.exportToBytes());+ } catch (error) {+ console.error('[Export] Error exporting raw SQLite vault:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } finally {+ hideLoading();+ }+ };++ /**+ * Run the export the user picked.+ */+ const handleExportConfirmed = async (): Promise<void> => {+ switch (currentExportType) {+ case ExportType.Csv:+ await exportVaultCsv();+ break;+ case ExportType.Avux:+ await exportVaultAvux();+ break;+ case ExportType.Avex:+ break;+ }+ };++ /**+ * Verify the master password, then export (or ask for the export password first for .avex).+ */+ const handlePasswordSubmitted = async (password: string): Promise<void> => {+ setShowPasswordConfirmation(false);+ setPasswordError('');++ showLoading(t(`${tk}.VerifyingPasswordMessage`));+ try {+ const result = await MasterPasswordService.verifyPassword(password);+ hideLoading();++ switch (result) {+ case PasswordVerificationResult.Success:+ if (currentExportType === ExportType.Avex) {+ setShowExportPasswordModal(true);+ } else {+ await handleExportConfirmed();+ }+ break;+ case PasswordVerificationResult.InvalidPassword:+ setPasswordError(t(`${tk}.PasswordIncorrect`));+ setShowPasswordConfirmation(true);+ break;+ default:+ setPasswordError(t(`${tk}.PasswordVerificationFailed`));+ setShowPasswordConfirmation(true);+ break;+ }+ } catch (error) {+ console.error('[Export] Error verifying password:', error);+ hideLoading();+ setPasswordError(t(`${tk}.PasswordVerificationFailed`));+ setShowPasswordConfirmation(true);+ }+ };++ /**+ * The encryption password was entered: create the .avex export.+ */+ const handleExportPasswordSubmitted = async (exportPassword: string): Promise<void> => {+ setShowExportPasswordModal(false);+ await exportVaultAvex(exportPassword);+ };++ return (+ <>+ <PageHeader breadcrumbItems={[{ displayName: t(`${tk}.PageTitle`) }]} title={t(`${tk}.PageTitle`)} description={t(`${tk}.PageDescription`)} />++ <PageContent>+ <Card className="2xl:col-span-2">+ <h3 className="mb-4 text-xl font-semibold dark:text-white">{t(`${tk}.ImportSectionTitle`)}</h3>+ <div className="mb-4 text-sm text-gray-500 dark:text-gray-400">{t(`${tk}.ImportSectionDescription`)}</div>+ <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">+ <ImportServices />+ </div>+ </Card>++ <Card className="2xl:col-span-2">+ <h3 className="mb-4 text-xl font-semibold dark:text-white">{t(`${tk}.ExportSectionTitle`)}</h3>+ <p className="text-sm text-gray-500 dark:text-gray-400 mb-4">{t(`${tk}.ExportSectionDescription`)}</p>++ <div className="space-y-3">+ <div className="flex items-center justify-between p-3 border-2 border-orange-200 dark:border-orange-800 rounded-lg bg-orange-50 dark:bg-orange-900/20">+ <div className="flex-1 min-w-0 mr-4">+ <div className="flex items-center gap-2 mb-1">+ <h4 className="text-sm font-semibold text-gray-900 dark:text-white">{t(`${tk}.ExportAvexTitle`)}</h4>+ <span className="px-1.5 py-0.5 text-xs font-medium text-orange-700 dark:text-orange-300 bg-orange-100 dark:bg-orange-900 rounded">{t(`${tk}.RecommendedLabel`)}</span>+ </div>+ <p className="text-xs text-gray-600 dark:text-gray-300">{t(`${tk}.ExportAvexDescription`)}</p>+ </div>+ <Button onClick={() => void showExportConfirmation(ExportType.Avex)}>{t(`${tk}.ExportAvexButton`)}</Button>+ </div>++ <div className="flex items-center justify-between p-3 border border-gray-200 dark:border-gray-700 rounded-lg">+ <div className="flex-1 min-w-0 mr-4">+ <h4 className="text-sm font-semibold text-gray-900 dark:text-white mb-1">{t(`${tk}.ExportAvuxTitle`)}</h4>+ <p className="text-xs text-gray-600 dark:text-gray-300">{t(`${tk}.ExportAvuxDescription`)}</p>+ </div>+ <Button onClick={() => void showExportConfirmation(ExportType.Avux)}>{t(`${tk}.ExportAvuxButton`)}</Button>+ </div>++ <div className="flex items-center justify-between p-3 border border-gray-200 dark:border-gray-700 rounded-lg">+ <div className="flex-1 min-w-0 mr-4">+ <h4 className="text-sm font-semibold text-gray-900 dark:text-white mb-1">{t(`${tk}.ExportCsvTitle`)}</h4>+ <p className="text-xs text-gray-600 dark:text-gray-300">{t(`${tk}.ExportCsvDescription`)}</p>+ </div>+ <Button onClick={() => void showExportConfirmation(ExportType.Csv)}>{t(`${tk}.ExportCsvButton`)}</Button>+ </div>++ {isDebugBuild && (+ <div className="flex items-center justify-between p-3 border-2 border-dashed border-yellow-400 dark:border-yellow-600 rounded-lg bg-yellow-50 dark:bg-yellow-900/20">+ <div className="flex-1 min-w-0 mr-4">+ <div className="flex items-center gap-2 mb-1">+ <h4 className="text-sm font-semibold text-gray-900 dark:text-white">Export raw SQLite</h4>+ <span className="px-1.5 py-0.5 text-xs font-medium text-yellow-800 dark:text-yellow-200 bg-yellow-200 dark:bg-yellow-800 rounded">DEBUG</span>+ </div>+ <p className="text-xs text-gray-600 dark:text-gray-300">Downloads the unencrypted SQLite vault file as-is. Anyone with the file can read everything.</p>+ </div>+ <Button onClick={exportVaultSqlite}>Download .sqlite</Button>+ </div>+ )}+ </div>+ </Card>++ <ResetVaultSection />+ </PageContent>++ <PasswordConfirmationModal+ isOpen={showPasswordConfirmation}+ title={t(`${tk}.ExportPasswordConfirmTitle`)}+ description={t(`${tk}.ExportPasswordConfirmDescription`)}+ errorMessage={passwordError}+ onPasswordSubmitted={password => void handlePasswordSubmitted(password)}+ onClose={() => {+ setShowPasswordConfirmation(false);+ setPasswordError('');+ }} />++ <ExportPasswordModal+ isOpen={showExportPasswordModal}+ title={t(`${tk}.ExportEncryptedPasswordTitle`)}+ description={t(`${tk}.ExportEncryptedPasswordDescription`)}+ onPasswordSubmitted={password => void handleExportPasswordSubmitted(password)}+ onClose={() => setShowExportPasswordModal(false)} />+ </>+ );+};++export default ImportExport;