Skip to content

Commit

Permalink
Fix account linking issues (#128)
Browse files Browse the repository at this point in the history
Everything's looking good/working well πŸ‘ŒπŸΌ Nice catch πŸ™πŸΌ
  • Loading branch information
bgins authored Jun 20, 2023
1 parent 1457d6b commit 506172e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/lib/auth/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,32 @@ export const loadAccount = async (hashedUsername: string, fullUsername: string):
backupCreated: backupStatus.created
}))
}

export async function waitForDataRoot(username: string): Promise<void> {
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)
})
}
8 changes: 7 additions & 1 deletion src/routes/link-device/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand All @@ -51,7 +55,9 @@
goto('/')
}
initAccountLinkingConsumer()
if (!$sessionStore.session) {
initAccountLinkingConsumer()
}
</script>

<input type="checkbox" id="my-modal-5" checked class="modal-toggle" />
Expand Down

1 comment on commit 506172e

@vercel
Copy link

@vercel vercel bot commented on 506172e Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.