From 92fda8cb24f4037a567d877367b3d4ce1ed2d477 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sat, 24 May 2025 14:24:50 +0200 Subject: [PATCH] feat: separate interface for decoded accounts --- web/src/repository/account.ts | 50 ++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/web/src/repository/account.ts b/web/src/repository/account.ts index b6b6a21..1d38136 100644 --- a/web/src/repository/account.ts +++ b/web/src/repository/account.ts @@ -2,7 +2,7 @@ import { useDbContext } from "@/context/db/db"; import { deriveDeviceKey, getDeviceId } from "@/util/deviceId"; import { useCallback } from "react"; -export interface LocalAccount { +export interface RawLocalAccount { accountId: string; label: string; email: string; @@ -11,6 +11,15 @@ export interface LocalAccount { updatedAt: string; } +export interface LocalAccount { + accountId: string; + label: string; + email: string; + access: string; + refresh: string; + updatedAt: string; +} + export interface CreateAccountRequest { accountId: string; label: string; @@ -96,25 +105,28 @@ export const useAccountRepo = () => { const tx = db.transaction("accounts", "readonly"); const store = tx.objectStore("accounts"); - const accounts: LocalAccount[] = await store.getAll(); + const accounts: RawLocalAccount[] = await store.getAll(); - const results = []; - for (const account of accounts) { - try { - const accessToken = await decryptToken(account.access); - const refreshToken = await decryptToken(account.refresh); - results.push({ - accountId: account.accountId, - label: account.label, - email: account.email, - access: accessToken, - refresh: refreshToken, - updatedAt: account.updatedAt, - }); - } catch (err) { - console.warn(`Failed to decrypt account ${account.label}:`, err); - } - } + const results: LocalAccount[] = ( + await Promise.all( + accounts.map(async (account) => { + try { + const accessToken = await decryptToken(account.access); + const refreshToken = await decryptToken(account.refresh); + return { + accountId: account.accountId, + label: account.label, + email: account.email, + access: accessToken, + refresh: refreshToken, + updatedAt: account.updatedAt, + }; + } catch (err) { + console.warn(`Failed to decrypt account ${account.label}:`, err); + } + }) + ) + ).filter((acc) => acc !== undefined); return results; }, [db, decryptToken]);