Use public api to resolve handle
This commit is contained in:
@@ -174,7 +174,7 @@ export async function resolveLink(
|
|||||||
throw new Error('getPost: post not found')
|
throw new Error('getPost: post not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forked from useFetchDid. TODO: move into RQ.
|
// TODO: move into RQ.
|
||||||
async function fetchDid(handleOrDid: string) {
|
async function fetchDid(handleOrDid: string) {
|
||||||
let identifier = handleOrDid
|
let identifier = handleOrDid
|
||||||
if (!identifier.startsWith('did:')) {
|
if (!identifier.startsWith('did:')) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import {HITSLOP_10} from '#/lib/constants'
|
|||||||
import {cleanError} from '#/lib/strings/errors'
|
import {cleanError} from '#/lib/strings/errors'
|
||||||
import {sanitizeHandle} from '#/lib/strings/handles'
|
import {sanitizeHandle} from '#/lib/strings/handles'
|
||||||
import {createFullHandle, validateHandle} from '#/lib/strings/handles'
|
import {createFullHandle, validateHandle} from '#/lib/strings/handles'
|
||||||
import {useFetchDid, useUpdateHandleMutation} from '#/state/queries/handle'
|
import {useResolveHandle, useUpdateHandleMutation} from '#/state/queries/handle'
|
||||||
import {RQKEY as RQKEY_PROFILE} from '#/state/queries/profile'
|
import {RQKEY as RQKEY_PROFILE} from '#/state/queries/profile'
|
||||||
import {useServiceQuery} from '#/state/queries/service'
|
import {useServiceQuery} from '#/state/queries/service'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAgent, useSession} from '#/state/session'
|
||||||
@@ -282,7 +282,7 @@ function OwnHandlePage({goToServiceHandle}: {goToServiceHandle: () => void}) {
|
|||||||
const [domain, setDomain] = useState('')
|
const [domain, setDomain] = useState('')
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const control = Dialog.useDialogContext()
|
const control = Dialog.useDialogContext()
|
||||||
const fetchDid = useFetchDid()
|
const resolveHandle = useResolveHandle()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -307,10 +307,10 @@ function OwnHandlePage({goToServiceHandle}: {goToServiceHandle: () => void}) {
|
|||||||
isSuccess: isVerified,
|
isSuccess: isVerified,
|
||||||
error: verifyError,
|
error: verifyError,
|
||||||
reset: resetVerification,
|
reset: resetVerification,
|
||||||
} = useMutation<true, Error | DidMismatchError>({
|
} = useMutation<true>({
|
||||||
mutationKey: ['verify-handle', domain],
|
mutationKey: ['verify-handle', domain],
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
const did = await fetchDid(domain)
|
const did = await resolveHandle(domain)
|
||||||
if (did !== currentAccount?.did) {
|
if (did !== currentAccount?.did) {
|
||||||
throw new DidMismatchError(did)
|
throw new DidMismatchError(did)
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-14
@@ -3,6 +3,7 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'
|
|||||||
|
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
import {createPublicAgent} from '#/state/session/agent'
|
||||||
|
|
||||||
const handleQueryKeyRoot = 'handle'
|
const handleQueryKeyRoot = 'handle'
|
||||||
const fetchHandleQueryKey = (handleOrDid: string) => [
|
const fetchHandleQueryKey = (handleOrDid: string) => [
|
||||||
@@ -10,7 +11,7 @@ const fetchHandleQueryKey = (handleOrDid: string) => [
|
|||||||
handleOrDid,
|
handleOrDid,
|
||||||
]
|
]
|
||||||
const didQueryKeyRoot = 'did'
|
const didQueryKeyRoot = 'did'
|
||||||
const fetchDidQueryKey = (handleOrDid: string) => [didQueryKeyRoot, handleOrDid]
|
const fetchDidQueryKey = (handle: string) => [didQueryKeyRoot, handle]
|
||||||
|
|
||||||
export function useFetchHandle() {
|
export function useFetchHandle() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
@@ -51,25 +52,29 @@ export function useUpdateHandleMutation(opts?: {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useFetchDid() {
|
export function useResolveHandle() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
|
||||||
|
// @NOTE: We are *not* using the logged in agent (from `useAgent()`) here.
|
||||||
|
// Using the public API rather than the user's PDS ensures that the handle
|
||||||
|
// properly resolves.
|
||||||
|
const publicAgent = React.useMemo(() => createPublicAgent(), [])
|
||||||
|
|
||||||
return React.useCallback(
|
return React.useCallback(
|
||||||
async (handleOrDid: string) => {
|
async (handle: string) => {
|
||||||
return queryClient.fetchQuery({
|
return queryClient.fetchQuery({
|
||||||
staleTime: STALE.INFINITY,
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: fetchDidQueryKey(handleOrDid),
|
queryKey: fetchDidQueryKey(handle),
|
||||||
queryFn: async () => {
|
queryFn: async ({signal}) => {
|
||||||
let identifier = handleOrDid
|
const res = await publicAgent.resolveHandle(
|
||||||
if (!identifier.startsWith('did:')) {
|
{handle},
|
||||||
const res = await agent.resolveHandle({handle: identifier})
|
// Retries should force a fresh resolution; avoid cache.
|
||||||
identifier = res.data.did
|
{signal, headers: {'cache-control': 'no-cache'}},
|
||||||
}
|
)
|
||||||
return identifier
|
return res.data.did
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[queryClient, agent],
|
[queryClient, publicAgent],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user