mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useMutation} from '@tanstack/react-query'
2
3import {useAgent, useSession} from '#/state/session'
4import {useUpdateAccountEmailStateQueryCache} from '#/components/dialogs/EmailDialog/data/useAccountEmailState'
5
6export function useManageEmail2FA() {
7 const agent = useAgent()
8 const {currentAccount} = useSession()
9 const updateAccountEmailStateQueryCache =
10 useUpdateAccountEmailStateQueryCache()
11
12 return useMutation({
13 mutationFn: async ({
14 enabled,
15 token,
16 }:
17 | {enabled: true; token?: undefined}
18 | {enabled: false; token: string}) => {
19 if (!currentAccount?.email) {
20 throw new Error('No email found for the current account')
21 }
22
23 await agent.com.atproto.server.updateEmail({
24 email: currentAccount.email,
25 emailAuthFactor: enabled,
26 token,
27 })
28 const {data} = await agent.resumeSession(agent.session!)
29 updateAccountEmailStateQueryCache({
30 isEmailVerified: !!data.emailConfirmed,
31 email2FAEnabled: !!data.emailAuthFactor,
32 })
33 },
34 })
35}