diff --git a/src/lib/auth/account.ts b/src/lib/auth/account.ts index ea681b6..bda561e 100644 --- a/src/lib/auth/account.ts +++ b/src/lib/auth/account.ts @@ -122,3 +122,32 @@ export const loadAccount = async (hashedUsername: string, fullUsername: string): backupCreated: backupStatus.created })) } + +export async function waitForDataRoot(username: string): Promise { + const session = getStore(sessionStore) + const reference = session.program?.components.reference + const EMPTY_CID = 'Qmc5m94Gu7z62RC8waSKkZUrCCBJPyHbkpmGzEePxy2oXJ' + + if (!reference) throw new Error('Program must be initialized to check for data root') + + let dataRoot = await reference.dataRoot.lookup(username) + + if (dataRoot.toString() !== EMPTY_CID) return + + return new Promise((resolve) => { + const maxRetries = 50 + let attempt = 0 + + const dataRootInterval = setInterval(async () => { + dataRoot = await reference.dataRoot.lookup(username) + + if (dataRoot.toString() === EMPTY_CID && attempt < maxRetries) { + attempt++ + return + } + + clearInterval(dataRootInterval) + resolve() + }, 500) + }) +} diff --git a/src/routes/link-device/+page.svelte b/src/routes/link-device/+page.svelte index 7228123..b29c64e 100644 --- a/src/routes/link-device/+page.svelte +++ b/src/routes/link-device/+page.svelte @@ -6,6 +6,8 @@ import { addNotification } from '$lib/notifications' import { createAccountLinkingConsumer } from '$lib/auth/linking' import { loadAccount } from '$lib/auth/account' + import { sessionStore } from '../../stores' + import { waitForDataRoot } from '$lib/auth/account' import type { LinkDeviceView } from '$lib/views' import FilesystemActivity from '$components/common/FilesystemActivity.svelte' import LinkDevice from '$components/auth/link-device/LinkDevice.svelte' @@ -33,6 +35,8 @@ if (approved) { view = 'load-filesystem' + // See https://github.com/oddsdk/ts-odd/issues/529 + await waitForDataRoot(hashedUsername) await loadAccount(hashedUsername, fullUsername) addNotification("You're now connected!", 'success') @@ -51,7 +55,9 @@ goto('/') } - initAccountLinkingConsumer() + if (!$sessionStore.session) { + initAccountLinkingConsumer() + }