[SDK] Add refreshSession and migrate the session-pinned infra (#11381)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import {useMutation} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {usePdsClient, useSession, useSessionApi} from '#/state/session'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
export function useConfirmEmail({
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {onSuccess?: () => void; onError?: () => void} = {}) {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const {currentAccount} = useSession()
|
||||
const {refreshSession} = useSessionApi()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({token}: {token: string}) => {
|
||||
@@ -15,12 +17,12 @@ export function useConfirmEmail({
|
||||
throw new Error('No email found for the current account')
|
||||
}
|
||||
|
||||
await agent.com.atproto.server.confirmEmail({
|
||||
await pdsClient.call(com.atproto.server.confirmEmail, {
|
||||
email: currentAccount.email.trim(),
|
||||
token: token.trim(),
|
||||
})
|
||||
// will update session state at root of app
|
||||
await agent.resumeSession(agent.session!)
|
||||
await refreshSession()
|
||||
},
|
||||
onSuccess,
|
||||
onError,
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import {useMutation} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {usePdsClient, useSession, useSessionApi} from '#/state/session'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
export function useManageEmail2FA() {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const {currentAccount} = useSession()
|
||||
const {refreshSession} = useSessionApi()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
@@ -17,13 +19,13 @@ export function useManageEmail2FA() {
|
||||
throw new Error('No email found for the current account')
|
||||
}
|
||||
|
||||
await agent.com.atproto.server.updateEmail({
|
||||
await pdsClient.call(com.atproto.server.updateEmail, {
|
||||
email: currentAccount.email,
|
||||
emailAuthFactor: enabled,
|
||||
token,
|
||||
})
|
||||
// will update session state at root of app
|
||||
await agent.resumeSession(agent.session!)
|
||||
await refreshSession()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
import {type Client} from '@atproto/lex'
|
||||
import {useMutation} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent} from '#/state/session'
|
||||
import {usePdsClient, useSessionApi} from '#/state/session'
|
||||
import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
async function updateEmailAndRefreshSession(
|
||||
agent: ReturnType<typeof useAgent>,
|
||||
pdsClient: Client,
|
||||
refreshSession: () => Promise<unknown>,
|
||||
email: string,
|
||||
token?: string,
|
||||
) {
|
||||
await agent.com.atproto.server.updateEmail({email: email.trim(), token})
|
||||
await agent.resumeSession(agent.session!)
|
||||
await pdsClient.call(com.atproto.server.updateEmail, {
|
||||
email: email.trim(),
|
||||
token,
|
||||
})
|
||||
await refreshSession()
|
||||
}
|
||||
|
||||
export function useUpdateEmail() {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const {refreshSession} = useSessionApi()
|
||||
const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate()
|
||||
|
||||
return useMutation<
|
||||
@@ -23,7 +30,12 @@ export function useUpdateEmail() {
|
||||
>({
|
||||
mutationFn: async ({email, token}: {email: string; token?: string}) => {
|
||||
if (token) {
|
||||
await updateEmailAndRefreshSession(agent, email, token)
|
||||
await updateEmailAndRefreshSession(
|
||||
pdsClient,
|
||||
refreshSession,
|
||||
email,
|
||||
token,
|
||||
)
|
||||
return {
|
||||
status: 'success',
|
||||
}
|
||||
@@ -34,7 +46,12 @@ export function useUpdateEmail() {
|
||||
status: 'tokenRequired',
|
||||
}
|
||||
} else {
|
||||
await updateEmailAndRefreshSession(agent, email, token)
|
||||
await updateEmailAndRefreshSession(
|
||||
pdsClient,
|
||||
refreshSession,
|
||||
email,
|
||||
token,
|
||||
)
|
||||
return {
|
||||
status: 'success',
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user