migrate the pre-auth service calls to the service client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import {useCallback, useState} from 'react'
|
import {useCallback, useState} from 'react'
|
||||||
import {Keyboard, View} from 'react-native'
|
import {Keyboard, View} from 'react-native'
|
||||||
import {type ComAtprotoServerDescribeServer} from '@atproto/api'
|
|
||||||
import {Trans, useLingui} from '@lingui/react/macro'
|
import {Trans, useLingui} from '@lingui/react/macro'
|
||||||
import * as EmailValidator from 'email-validator'
|
import * as EmailValidator from 'email-validator'
|
||||||
|
|
||||||
|
import {createServiceClient} from '#/lib/lexClient'
|
||||||
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {Agent} from '#/state/session/agent'
|
|
||||||
import {atoms as a, useTheme, web} from '#/alf'
|
import {atoms as a, useTheme, web} from '#/alf'
|
||||||
import {Admonition} from '#/components/Admonition'
|
import {Admonition} from '#/components/Admonition'
|
||||||
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
|
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 {Loader} from '#/components/Loader'
|
||||||
import {Text} from '#/components/Typography'
|
import {Text} from '#/components/Typography'
|
||||||
import {IS_WEB} from '#/env'
|
import {IS_WEB} from '#/env'
|
||||||
|
import {com} from '#/lexicons'
|
||||||
import {FormContainer} from './FormContainer'
|
import {FormContainer} from './FormContainer'
|
||||||
|
|
||||||
type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema
|
type ServiceDescription = com.atproto.server.describeServer.$OutputBody
|
||||||
|
|
||||||
export const ForgotPasswordForm = ({
|
export const ForgotPasswordForm = ({
|
||||||
error,
|
error,
|
||||||
@@ -55,8 +55,12 @@ export const ForgotPasswordForm = ({
|
|||||||
setIsProcessing(true)
|
setIsProcessing(true)
|
||||||
|
|
||||||
try {
|
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()
|
onEmailSent()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('Failed to request password reset', {error: err})
|
logger.warn('Failed to request password reset', {error: err})
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import {useState} from 'react'
|
|||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
import {Trans, useLingui} from '@lingui/react/macro'
|
import {Trans, useLingui} from '@lingui/react/macro'
|
||||||
|
|
||||||
|
import {createServiceClient} from '#/lib/lexClient'
|
||||||
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
||||||
import {checkAndFormatResetCode} from '#/lib/strings/password'
|
import {checkAndFormatResetCode} from '#/lib/strings/password'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {Agent} from '#/state/session/agent'
|
|
||||||
import {atoms as a, web} from '#/alf'
|
import {atoms as a, web} from '#/alf'
|
||||||
import {Admonition} from '#/components/Admonition'
|
import {Admonition} from '#/components/Admonition'
|
||||||
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
|
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
|
||||||
@@ -16,6 +16,7 @@ import {Loader} from '#/components/Loader'
|
|||||||
import {Text} from '#/components/Typography'
|
import {Text} from '#/components/Typography'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
import {IS_WEB} from '#/env'
|
import {IS_WEB} from '#/env'
|
||||||
|
import {com} from '#/lexicons'
|
||||||
import {FormContainer} from './FormContainer'
|
import {FormContainer} from './FormContainer'
|
||||||
|
|
||||||
export const SetNewPasswordForm = ({
|
export const SetNewPasswordForm = ({
|
||||||
@@ -61,8 +62,12 @@ export const SetNewPasswordForm = ({
|
|||||||
setIsProcessing(true)
|
setIsProcessing(true)
|
||||||
|
|
||||||
try {
|
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,
|
token: formattedCode,
|
||||||
password,
|
password,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {ComAtprotoTempCheckHandleAvailability} from '@atproto/api'
|
import {type DatetimeString, type HandleString} from '@atproto/syntax'
|
||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -7,10 +7,11 @@ import {
|
|||||||
PUBLIC_BSKY_SERVICE,
|
PUBLIC_BSKY_SERVICE,
|
||||||
} from '#/lib/constants'
|
} from '#/lib/constants'
|
||||||
import {useDebouncedValue} from '#/lib/hooks/useDebouncedValue'
|
import {useDebouncedValue} from '#/lib/hooks/useDebouncedValue'
|
||||||
|
import {createServiceClient} from '#/lib/lexClient'
|
||||||
import {createFullHandle} from '#/lib/strings/handles'
|
import {createFullHandle} from '#/lib/strings/handles'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
|
import {com} from '#/lexicons'
|
||||||
import * as bsky from '#/types/bsky'
|
import * as bsky from '#/types/bsky'
|
||||||
import {Agent} from '../session/agent'
|
|
||||||
|
|
||||||
export const RQKEY_handleAvailability = (
|
export const RQKEY_handleAvailability = (
|
||||||
handle: string,
|
handle: string,
|
||||||
@@ -79,25 +80,33 @@ export async function checkHandleAvailability(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
if (serviceDid === BSKY_SERVICE_DID) {
|
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
|
// entryway has a special API for handle availability
|
||||||
const {data} = await agent.com.atproto.temp.checkHandleAvailability({
|
const data = await client.call(com.atproto.temp.checkHandleAvailability, {
|
||||||
handle,
|
// the caller assembles this from a validated username and domain
|
||||||
birthDate,
|
handle: handle as HandleString,
|
||||||
|
// callers pass an ISO date string built from the birth-date picker
|
||||||
|
birthDate: birthDate as DatetimeString | undefined,
|
||||||
email,
|
email,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (
|
if (
|
||||||
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultAvailable>(
|
bsky.isType(
|
||||||
|
com.atproto.temp.checkHandleAvailability.resultAvailable,
|
||||||
data.result,
|
data.result,
|
||||||
ComAtprotoTempCheckHandleAvailability.isResultAvailable,
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return {available: true} as const
|
return {available: true} as const
|
||||||
} else if (
|
} else if (
|
||||||
bsky.dangerousIsType<ComAtprotoTempCheckHandleAvailability.ResultUnavailable>(
|
bsky.isType(
|
||||||
|
com.atproto.temp.checkHandleAvailability.resultUnavailable,
|
||||||
data.result,
|
data.result,
|
||||||
ComAtprotoTempCheckHandleAvailability.isResultUnavailable,
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
@@ -110,14 +119,18 @@ export async function checkHandleAvailability(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
try {
|
||||||
const res = await agent.resolveHandle({
|
const data = await client.call(com.atproto.identity.resolveHandle, {
|
||||||
handle,
|
handle: handle as HandleString,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.data.did) {
|
if (data.did) {
|
||||||
return {available: false} as const
|
return {available: false} as const
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {Agent} from '../session/agent'
|
import {createServiceClient} from '#/lib/lexClient'
|
||||||
|
import {com} from '#/lexicons'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'service'
|
const RQKEY_ROOT = 'service'
|
||||||
export const RQKEY = (serviceUrl: string) => [RQKEY_ROOT, serviceUrl]
|
export const RQKEY = (serviceUrl: string) => [RQKEY_ROOT, serviceUrl]
|
||||||
@@ -9,9 +10,12 @@ export function useServiceQuery(serviceUrl: string) {
|
|||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY(serviceUrl),
|
queryKey: RQKEY(serviceUrl),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const agent = new Agent(null, {service: serviceUrl})
|
/*
|
||||||
const res = await agent.com.atproto.server.describeServer()
|
* The host is whatever the user typed or picked, so this describes it
|
||||||
return res.data
|
* 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),
|
enabled: isValidUrl(serviceUrl),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user