Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix account linking issues #128

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/lib/auth/account.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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()
}
</script>

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