Bluesky app fork with some witchin' additions 💫

[APP-1882] fix email not updating in session state after email change (#9953)

authored by

Spence Pope and committed by
GitHub
9c29b286 9746dd8e

+29 -11
+3 -3
src/components/dialogs/EmailDialog/data/useConfirmEmail.ts
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 - import {useAgent, useSession} from '#/state/session' 4 5 export function useConfirmEmail({ 6 onSuccess, ··· 8 }: {onSuccess?: () => void; onError?: () => void} = {}) { 9 const agent = useAgent() 10 const {currentAccount} = useSession() 11 12 return useMutation({ 13 mutationFn: async ({token}: {token: string}) => { ··· 19 email: currentAccount.email.trim(), 20 token: token.trim(), 21 }) 22 - // will update session state at root of app 23 - await agent.resumeSession(agent.session!) 24 }, 25 onSuccess, 26 onError,
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 + import {useAgent, useSession, useSessionApi} from '#/state/session' 4 5 export function useConfirmEmail({ 6 onSuccess, ··· 8 }: {onSuccess?: () => void; onError?: () => void} = {}) { 9 const agent = useAgent() 10 const {currentAccount} = useSession() 11 + const {partialRefreshSession} = useSessionApi() 12 13 return useMutation({ 14 mutationFn: async ({token}: {token: string}) => { ··· 20 email: currentAccount.email.trim(), 21 token: token.trim(), 22 }) 23 + await partialRefreshSession() 24 }, 25 onSuccess, 26 onError,
+3 -3
src/components/dialogs/EmailDialog/data/useManageEmail2FA.ts
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 - import {useAgent, useSession} from '#/state/session' 4 5 export function useManageEmail2FA() { 6 const agent = useAgent() 7 const {currentAccount} = useSession() 8 9 return useMutation({ 10 mutationFn: async ({ ··· 22 emailAuthFactor: enabled, 23 token, 24 }) 25 - // will update session state at root of app 26 - await agent.resumeSession(agent.session!) 27 }, 28 }) 29 }
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 + import {useAgent, useSession, useSessionApi} from '#/state/session' 4 5 export function useManageEmail2FA() { 6 const agent = useAgent() 7 const {currentAccount} = useSession() 8 + const {partialRefreshSession} = useSessionApi() 9 10 return useMutation({ 11 mutationFn: async ({ ··· 23 emailAuthFactor: enabled, 24 token, 25 }) 26 + await partialRefreshSession() 27 }, 28 }) 29 }
+16 -4
src/components/dialogs/EmailDialog/data/useUpdateEmail.ts
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 - import {useAgent} from '#/state/session' 4 import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate' 5 6 async function updateEmailAndRefreshSession( 7 agent: ReturnType<typeof useAgent>, 8 email: string, 9 token?: string, 10 ) { 11 await agent.com.atproto.server.updateEmail({email: email.trim(), token}) 12 - await agent.resumeSession(agent.session!) 13 } 14 15 export function useUpdateEmail() { 16 const agent = useAgent() 17 const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate() 18 19 return useMutation< ··· 23 >({ 24 mutationFn: async ({email, token}: {email: string; token?: string}) => { 25 if (token) { 26 - await updateEmailAndRefreshSession(agent, email, token) 27 return { 28 status: 'success', 29 } ··· 34 status: 'tokenRequired', 35 } 36 } else { 37 - await updateEmailAndRefreshSession(agent, email, token) 38 return { 39 status: 'success', 40 }
··· 1 import {useMutation} from '@tanstack/react-query' 2 3 + import {useAgent, useSessionApi} from '#/state/session' 4 import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate' 5 6 async function updateEmailAndRefreshSession( 7 agent: ReturnType<typeof useAgent>, 8 + partialRefreshSession: () => Promise<void>, 9 email: string, 10 token?: string, 11 ) { 12 await agent.com.atproto.server.updateEmail({email: email.trim(), token}) 13 + await partialRefreshSession() 14 } 15 16 export function useUpdateEmail() { 17 const agent = useAgent() 18 + const {partialRefreshSession} = useSessionApi() 19 const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate() 20 21 return useMutation< ··· 25 >({ 26 mutationFn: async ({email, token}: {email: string; token?: string}) => { 27 if (token) { 28 + await updateEmailAndRefreshSession( 29 + agent, 30 + partialRefreshSession, 31 + email, 32 + token, 33 + ) 34 return { 35 status: 'success', 36 } ··· 41 status: 'tokenRequired', 42 } 43 } else { 44 + await updateEmailAndRefreshSession( 45 + agent, 46 + partialRefreshSession, 47 + email, 48 + token, 49 + ) 50 return { 51 status: 'success', 52 }
+1
src/state/session/index.tsx
··· 284 type: 'partial-refresh-session', 285 accountDid: agent.session!.did, 286 patch: { 287 emailConfirmed: data.emailConfirmed, 288 emailAuthFactor: data.emailAuthFactor, 289 },
··· 284 type: 'partial-refresh-session', 285 accountDid: agent.session!.did, 286 patch: { 287 + email: data.email, 288 emailConfirmed: data.emailConfirmed, 289 emailAuthFactor: data.emailAuthFactor, 290 },
+6 -1
src/state/session/reducer.ts
··· 58 | { 59 type: 'partial-refresh-session' 60 accountDid: string 61 - patch: Pick<SessionAccount, 'emailConfirmed' | 'emailAuthFactor'> 62 } 63 64 function createPublicAgentState(): AgentState { ··· 239 * Only mutating values that are safe. Be very careful with this. 240 */ 241 if (agent.session) { 242 agent.session.emailConfirmed = 243 patch.emailConfirmed ?? agent.session.emailConfirmed 244 agent.session.emailAuthFactor = ··· 255 if (a.did === accountDid) { 256 return { 257 ...a, 258 emailConfirmed: patch.emailConfirmed ?? a.emailConfirmed, 259 emailAuthFactor: patch.emailAuthFactor ?? a.emailAuthFactor, 260 }
··· 58 | { 59 type: 'partial-refresh-session' 60 accountDid: string 61 + patch: Pick< 62 + SessionAccount, 63 + 'email' | 'emailConfirmed' | 'emailAuthFactor' 64 + > 65 } 66 67 function createPublicAgentState(): AgentState { ··· 242 * Only mutating values that are safe. Be very careful with this. 243 */ 244 if (agent.session) { 245 + agent.session.email = patch.email ?? agent.session.email 246 agent.session.emailConfirmed = 247 patch.emailConfirmed ?? agent.session.emailConfirmed 248 agent.session.emailAuthFactor = ··· 259 if (a.did === accountDid) { 260 return { 261 ...a, 262 + email: patch.email ?? a.email, 263 emailConfirmed: patch.emailConfirmed ?? a.emailConfirmed, 264 emailAuthFactor: patch.emailAuthFactor ?? a.emailAuthFactor, 265 }