Bluesky app fork with some witchin' additions 💫 witchsky.app
bluesky fork client

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