Skip to content

Commit

Permalink
🐛 Wait for auth before attempting to resolve handle
Browse files Browse the repository at this point in the history
  • Loading branch information
foysalit committed Feb 7, 2024
1 parent 6fcfdf2 commit fc77ab2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions components/repositories/useHandleToDidRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use client'
import { useEffect, useState } from 'react'
import { useContext, useEffect, useState } from 'react'
import { redirect, useRouter, useSearchParams } from 'next/navigation'

import { getDidFromHandle } from '@/lib/identity'
import clientManager from '@/lib/client'
import { AuthContext } from '@/shell/AuthContext'

export const useHandleToDidRedirect = (
handle: string,
Expand All @@ -11,15 +13,23 @@ export const useHandleToDidRedirect = (
const router = useRouter()
const searchParams = useSearchParams()
const [isFetching, setIsFetching] = useState<boolean>(true)
const { isLoggedIn } = useContext(AuthContext)

useEffect(() => {
setIsFetching(true)

// If the handle is already a DID, don't try to resolve it
if (handle.startsWith('did:')) {
setIsFetching(false)
return
}

// If we aren't logged in yet, leave the state at loading and don't try to resolve handle
if (!isLoggedIn) {
return
}

const fetchDidAndRedirect = async () => {
setIsFetching(true)
const did = await getDidFromHandle(handle)
const params = searchParams.toString()
if (did) {
Expand All @@ -33,7 +43,7 @@ export const useHandleToDidRedirect = (
}

fetchDidAndRedirect()
}, [handle])
}, [handle, isLoggedIn])

return { isFetching }
}

0 comments on commit fc77ab2

Please sign in to comment.