migrate the pre-auth service calls to the service client

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-08-03 20:32:42 +03:00
parent dc90f4ee37
commit a92f28461b
4 changed files with 53 additions and 27 deletions
+9 -5
View File
@@ -1,12 +1,11 @@
import {useCallback, useState} from 'react'
import {Keyboard, View} from 'react-native'
import {type ComAtprotoServerDescribeServer} from '@atproto/api'
import {Trans, useLingui} from '@lingui/react/macro'
import * as EmailValidator from 'email-validator'
import {createServiceClient} from '#/lib/lexClient'
import {cleanError, isNetworkError} from '#/lib/strings/errors'
import {logger} from '#/logger'
import {Agent} from '#/state/session/agent'
import {atoms as a, useTheme, web} from '#/alf'
import {Admonition} from '#/components/Admonition'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
@@ -16,9 +15,10 @@ import {At_Stroke2_Corner0_Rounded as At} from '#/components/icons/At'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
import {IS_WEB} from '#/env'
import {com} from '#/lexicons'
import {FormContainer} from './FormContainer'
type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema
type ServiceDescription = com.atproto.server.describeServer.$OutputBody
export const ForgotPasswordForm = ({
error,
@@ -55,8 +55,12 @@ export const ForgotPasswordForm = ({
setIsProcessing(true)
try {
const agent = new Agent(null, {service: serviceUrl})
await agent.com.atproto.server.requestPasswordReset({email})
/*
* Pre-auth request against a user-chosen host, so it goes through a
* one-off service client rather than a session-scoped one.
*/
const client = createServiceClient(serviceUrl)
await client.call(com.atproto.server.requestPasswordReset, {email})
onEmailSent()
} catch (err) {
logger.warn('Failed to request password reset', {error: err})
+8 -3
View File
@@ -2,10 +2,10 @@ import {useState} from 'react'
import {View} from 'react-native'
import {Trans, useLingui} from '@lingui/react/macro'
import {createServiceClient} from '#/lib/lexClient'
import {cleanError, isNetworkError} from '#/lib/strings/errors'
import {checkAndFormatResetCode} from '#/lib/strings/password'
import {logger} from '#/logger'
import {Agent} from '#/state/session/agent'
import {atoms as a, web} from '#/alf'
import {Admonition} from '#/components/Admonition'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
@@ -16,6 +16,7 @@ import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
import {useAnalytics} from '#/analytics'
import {IS_WEB} from '#/env'
import {com} from '#/lexicons'
import {FormContainer} from './FormContainer'
export const SetNewPasswordForm = ({
@@ -61,8 +62,12 @@ export const SetNewPasswordForm = ({
setIsProcessing(true)
try {
const agent = new Agent(null, {service: serviceUrl})
await agent.com.atproto.server.resetPassword({
/*
* Pre-auth request against a user-chosen host, so it goes through a
* one-off service client rather than a session-scoped one.
*/
const client = createServiceClient(serviceUrl)
await client.call(com.atproto.server.resetPassword, {
token: formattedCode,
password,
})
+28 -15
View File
@@ -1,4 +1,4 @@
import {ComAtprotoTempCheckHandleAvailability} from '@atproto/api'
import {type DatetimeString, type HandleString} from '@atproto/syntax'
import {useQuery} from '@tanstack/react-query'
import {
@@ -7,10 +7,11 @@ import {
PUBLIC_BSKY_SERVICE,
} from '#/lib/constants'
import {useDebouncedValue} from '#/lib/hooks/useDebouncedValue'
import {createServiceClient} from '#/lib/lexClient'
import {createFullHandle} from '#/lib/strings/handles'
import {useAnalytics} from '#/analytics'
import {com} from '#/lexicons'
import * as bsky from '#/types/bsky'
import {Agent} from '../session/agent'
export const RQKEY_handleAvailability = (
handle: string,
@@ -79,25 +80,33 @@ export async function checkHandleAvailability(
},
) {
if (serviceDid === BSKY_SERVICE_DID) {
const agent = new Agent(null, {service: BSKY_SERVICE})
/*
* Runs pre-auth during signup, so it goes through a one-off service client
* rather than a session-scoped one. The target is the fixed entryway rather
* than a user-supplied host, but there is still no session to hang a client
* off.
*/
const client = createServiceClient(BSKY_SERVICE)
// entryway has a special API for handle availability
const {data} = await agent.com.atproto.temp.checkHandleAvailability({
handle,
birthDate,
const data = await client.call(com.atproto.temp.checkHandleAvailability, {
// the caller assembles this from a validated username and domain
handle: handle as HandleString,
// callers pass an ISO date string built from the birth-date picker
birthDate: birthDate as DatetimeString | undefined,
email,
})
if (
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultAvailable>(
bsky.isType(
com.atproto.temp.checkHandleAvailability.resultAvailable,
data.result,
ComAtprotoTempCheckHandleAvailability.isResultAvailable,
)
) {
return {available: true} as const
} else if (
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultUnavailable>(
bsky.isType(
com.atproto.temp.checkHandleAvailability.resultUnavailable,
data.result,
ComAtprotoTempCheckHandleAvailability.isResultUnavailable,
)
) {
return {
@@ -110,14 +119,18 @@ export async function checkHandleAvailability(
)
}
} else {
// 3rd party PDSes won't have this API so just try and resolve the handle
const agent = new Agent(null, {service: PUBLIC_BSKY_SERVICE})
/*
* 3rd party PDSes won't have this API so just try and resolve the handle.
* This is an unauthenticated public-appview read, not a call against the
* user's chosen host.
*/
const client = createServiceClient(PUBLIC_BSKY_SERVICE)
try {
const res = await agent.resolveHandle({
handle,
const data = await client.call(com.atproto.identity.resolveHandle, {
handle: handle as HandleString,
})
if (res.data.did) {
if (data.did) {
return {available: false} as const
}
} catch {}
+8 -4
View File
@@ -1,6 +1,7 @@
import {useQuery} from '@tanstack/react-query'
import {Agent} from '../session/agent'
import {createServiceClient} from '#/lib/lexClient'
import {com} from '#/lexicons'
const RQKEY_ROOT = 'service'
export const RQKEY = (serviceUrl: string) => [RQKEY_ROOT, serviceUrl]
@@ -9,9 +10,12 @@ export function useServiceQuery(serviceUrl: string) {
return useQuery({
queryKey: RQKEY(serviceUrl),
queryFn: async () => {
const agent = new Agent(null, {service: serviceUrl})
const res = await agent.com.atproto.server.describeServer()
return res.data
/*
* The host is whatever the user typed or picked, so this describes it
* through a one-off service client rather than a session-scoped one.
*/
const client = createServiceClient(serviceUrl)
return await client.call(com.atproto.server.describeServer)
},
enabled: isValidUrl(serviceUrl),
})