fix email not updating in session state after email change
This commit is contained in:
committed by
Samuel Newman
parent
9746dd8e4c
commit
9ec3f4b6a3
@@ -1,6 +1,6 @@
|
|||||||
import {useMutation} from '@tanstack/react-query'
|
import {useMutation} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAgent, useSession, useSessionApi} from '#/state/session'
|
||||||
|
|
||||||
export function useConfirmEmail({
|
export function useConfirmEmail({
|
||||||
onSuccess,
|
onSuccess,
|
||||||
@@ -8,6 +8,7 @@ export function useConfirmEmail({
|
|||||||
}: {onSuccess?: () => void; onError?: () => void} = {}) {
|
}: {onSuccess?: () => void; onError?: () => void} = {}) {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
const {partialRefreshSession} = useSessionApi()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({token}: {token: string}) => {
|
mutationFn: async ({token}: {token: string}) => {
|
||||||
@@ -19,8 +20,7 @@ export function useConfirmEmail({
|
|||||||
email: currentAccount.email.trim(),
|
email: currentAccount.email.trim(),
|
||||||
token: token.trim(),
|
token: token.trim(),
|
||||||
})
|
})
|
||||||
// will update session state at root of app
|
await partialRefreshSession()
|
||||||
await agent.resumeSession(agent.session!)
|
|
||||||
},
|
},
|
||||||
onSuccess,
|
onSuccess,
|
||||||
onError,
|
onError,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import {useMutation} from '@tanstack/react-query'
|
import {useMutation} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {useAgent, useSession, useSessionApi} from '#/state/session'
|
||||||
|
|
||||||
export function useManageEmail2FA() {
|
export function useManageEmail2FA() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
const {partialRefreshSession} = useSessionApi()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({
|
mutationFn: async ({
|
||||||
@@ -22,8 +23,7 @@ export function useManageEmail2FA() {
|
|||||||
emailAuthFactor: enabled,
|
emailAuthFactor: enabled,
|
||||||
token,
|
token,
|
||||||
})
|
})
|
||||||
// will update session state at root of app
|
await partialRefreshSession()
|
||||||
await agent.resumeSession(agent.session!)
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
import {useMutation} from '@tanstack/react-query'
|
import {useMutation} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent, useSessionApi} from '#/state/session'
|
||||||
import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate'
|
import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate'
|
||||||
|
|
||||||
async function updateEmailAndRefreshSession(
|
async function updateEmailAndRefreshSession(
|
||||||
agent: ReturnType<typeof useAgent>,
|
agent: ReturnType<typeof useAgent>,
|
||||||
|
partialRefreshSession: () => Promise<void>,
|
||||||
email: string,
|
email: string,
|
||||||
token?: string,
|
token?: string,
|
||||||
) {
|
) {
|
||||||
await agent.com.atproto.server.updateEmail({email: email.trim(), token})
|
await agent.com.atproto.server.updateEmail({email: email.trim(), token})
|
||||||
await agent.resumeSession(agent.session!)
|
await partialRefreshSession()
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useUpdateEmail() {
|
export function useUpdateEmail() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const {partialRefreshSession} = useSessionApi()
|
||||||
const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate()
|
const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate()
|
||||||
|
|
||||||
return useMutation<
|
return useMutation<
|
||||||
@@ -23,7 +25,12 @@ export function useUpdateEmail() {
|
|||||||
>({
|
>({
|
||||||
mutationFn: async ({email, token}: {email: string; token?: string}) => {
|
mutationFn: async ({email, token}: {email: string; token?: string}) => {
|
||||||
if (token) {
|
if (token) {
|
||||||
await updateEmailAndRefreshSession(agent, email, token)
|
await updateEmailAndRefreshSession(
|
||||||
|
agent,
|
||||||
|
partialRefreshSession,
|
||||||
|
email,
|
||||||
|
token,
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
}
|
}
|
||||||
@@ -34,7 +41,12 @@ export function useUpdateEmail() {
|
|||||||
status: 'tokenRequired',
|
status: 'tokenRequired',
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await updateEmailAndRefreshSession(agent, email, token)
|
await updateEmailAndRefreshSession(
|
||||||
|
agent,
|
||||||
|
partialRefreshSession,
|
||||||
|
email,
|
||||||
|
token,
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -284,6 +284,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
type: 'partial-refresh-session',
|
type: 'partial-refresh-session',
|
||||||
accountDid: agent.session!.did,
|
accountDid: agent.session!.did,
|
||||||
patch: {
|
patch: {
|
||||||
|
email: data.email,
|
||||||
emailConfirmed: data.emailConfirmed,
|
emailConfirmed: data.emailConfirmed,
|
||||||
emailAuthFactor: data.emailAuthFactor,
|
emailAuthFactor: data.emailAuthFactor,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -58,7 +58,10 @@ export type Action =
|
|||||||
| {
|
| {
|
||||||
type: 'partial-refresh-session'
|
type: 'partial-refresh-session'
|
||||||
accountDid: string
|
accountDid: string
|
||||||
patch: Pick<SessionAccount, 'emailConfirmed' | 'emailAuthFactor'>
|
patch: Pick<
|
||||||
|
SessionAccount,
|
||||||
|
'email' | 'emailConfirmed' | 'emailAuthFactor'
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPublicAgentState(): AgentState {
|
function createPublicAgentState(): AgentState {
|
||||||
@@ -239,6 +242,7 @@ let reducer = (state: State, action: Action): State => {
|
|||||||
* Only mutating values that are safe. Be very careful with this.
|
* Only mutating values that are safe. Be very careful with this.
|
||||||
*/
|
*/
|
||||||
if (agent.session) {
|
if (agent.session) {
|
||||||
|
agent.session.email = patch.email ?? agent.session.email
|
||||||
agent.session.emailConfirmed =
|
agent.session.emailConfirmed =
|
||||||
patch.emailConfirmed ?? agent.session.emailConfirmed
|
patch.emailConfirmed ?? agent.session.emailConfirmed
|
||||||
agent.session.emailAuthFactor =
|
agent.session.emailAuthFactor =
|
||||||
@@ -255,6 +259,7 @@ let reducer = (state: State, action: Action): State => {
|
|||||||
if (a.did === accountDid) {
|
if (a.did === accountDid) {
|
||||||
return {
|
return {
|
||||||
...a,
|
...a,
|
||||||
|
email: patch.email ?? a.email,
|
||||||
emailConfirmed: patch.emailConfirmed ?? a.emailConfirmed,
|
emailConfirmed: patch.emailConfirmed ?? a.emailConfirmed,
|
||||||
emailAuthFactor: patch.emailAuthFactor ?? a.emailAuthFactor,
|
emailAuthFactor: patch.emailAuthFactor ?? a.emailAuthFactor,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user