Replace updateCurrentAccount() with refreshSession() (#3910)

Replace updateCurrentAccount() with resumeSession()
zio/stable
dan 2024-05-08 23:11:39 +01:00 committed by GitHub
parent f62b0458a7
commit 0c6bf276dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 15 additions and 257 deletions

View File

@ -1302,177 +1302,6 @@ describe('session', () => {
`)
})
it('updates current account', () => {
let state = getInitialState([])
const agent1 = new BskyAgent({service: 'https://alice.com'})
agent1.session = {
did: 'alice-did',
handle: 'alice.test',
accessJwt: 'alice-access-jwt-1',
refreshJwt: 'alice-refresh-jwt-1',
}
state = run(state, [
{
type: 'switched-to-account',
newAgent: agent1,
newAccount: agentToSessionAccountOrThrow(agent1),
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].accessJwt).toBe('alice-access-jwt-1')
expect(state.currentAgentState.did).toBe('alice-did')
state = run(state, [
{
type: 'updated-current-account',
updatedFields: {
email: 'alice@foo.bar',
emailConfirmed: false,
},
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].email).toBe('alice@foo.bar')
expect(state.accounts[0].emailConfirmed).toBe(false)
expect(state.currentAgentState.did).toBe('alice-did')
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": "alice@foo.bar",
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)
state = run(state, [
{
type: 'updated-current-account',
updatedFields: {
handle: 'alice-updated.test',
},
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].handle).toBe('alice-updated.test')
expect(state.currentAgentState.did).toBe('alice-did')
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": "alice@foo.bar",
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice-updated.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)
const agent2 = new BskyAgent({service: 'https://bob.com'})
agent2.session = {
did: 'bob-did',
handle: 'bob.test',
accessJwt: 'bob-access-jwt-1',
refreshJwt: 'bob-refresh-jwt-1',
}
state = run(state, [
{
// Switch to Bob.
type: 'switched-to-account',
newAgent: agent2,
newAccount: agentToSessionAccountOrThrow(agent2),
},
{
// Update Bob.
type: 'updated-current-account',
updatedFields: {
handle: 'bob-updated.test',
},
},
{
// Switch back to Alice.
type: 'switched-to-account',
newAgent: agent1,
newAccount: agentToSessionAccountOrThrow(agent1),
},
{
// Update Alice.
type: 'updated-current-account',
updatedFields: {
handle: 'alice-updated-2.test',
},
},
])
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": undefined,
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice-updated-2.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
{
"accessJwt": "bob-access-jwt-1",
"deactivated": false,
"did": "bob-did",
"email": undefined,
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "bob-updated.test",
"pdsUrl": undefined,
"refreshJwt": "bob-refresh-jwt-1",
"service": "https://bob.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)
})
it('replaces local accounts with synced accounts', () => {
let state = getInitialState([])

View File

@ -35,7 +35,6 @@ const ApiContext = React.createContext<SessionApiContext>({
logout: async () => {},
resumeSession: async () => {},
removeAccount: () => {},
updateCurrentAccount: () => {},
})
export function Provider({children}: React.PropsWithChildren<{}>) {
@ -149,15 +148,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[cancelPendingTask],
)
const updateCurrentAccount = React.useCallback<
SessionApiContext['updateCurrentAccount']
>(account => {
dispatch({
type: 'updated-current-account',
updatedFields: account,
})
}, [])
React.useEffect(() => {
if (state.needsPersist) {
state.needsPersist = false
@ -210,16 +200,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logout,
resumeSession,
removeAccount,
updateCurrentAccount,
}),
[
createAccount,
login,
logout,
resumeSession,
removeAccount,
updateCurrentAccount,
],
[createAccount, login, logout, resumeSession, removeAccount],
)
// @ts-ignore

View File

@ -36,15 +36,6 @@ export type Action =
newAgent: OpaqueBskyAgent
newAccount: SessionAccount
}
| {
type: 'updated-current-account'
updatedFields: Partial<
Pick<
SessionAccount,
'handle' | 'email' | 'emailConfirmed' | 'emailAuthFactor'
>
>
}
| {
type: 'removed-account'
accountDid: string
@ -134,23 +125,6 @@ export function reducer(state: State, action: Action): State {
needsPersist: true,
}
}
case 'updated-current-account': {
const {updatedFields} = action
return {
accounts: state.accounts.map(a => {
if (a.did === state.currentAgentState.did) {
return {
...a,
...updatedFields,
}
} else {
return a
}
}),
currentAgentState: state.currentAgentState,
needsPersist: true,
}
}
case 'removed-account': {
const {accountDid} = action
return {

View File

@ -37,12 +37,4 @@ export type SessionApiContext = {
logout: (logContext: LogEvents['account:loggedOut']['logContext']) => void
resumeSession: (account: SessionAccount) => Promise<void>
removeAccount: (account: SessionAccount) => void
updateCurrentAccount: (
account: Partial<
Pick<
SessionAccount,
'handle' | 'email' | 'emailConfirmed' | 'emailAuthFactor'
>
>,
) => void
}

View File

@ -4,7 +4,7 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useModalControls} from '#/state/modals'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {usePalette} from 'lib/hooks/usePalette'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {cleanError} from 'lib/strings/errors'
@ -28,7 +28,6 @@ export function Component() {
const pal = usePalette('default')
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const {updateCurrentAccount} = useSessionApi()
const {_} = useLingui()
const [stage, setStage] = useState<Stages>(Stages.InputEmail)
const [email, setEmail] = useState<string>(currentAccount?.email || '')
@ -51,10 +50,7 @@ export function Component() {
setStage(Stages.ConfirmCode)
} else {
await getAgent().com.atproto.server.updateEmail({email: email.trim()})
updateCurrentAccount({
email: email.trim(),
emailConfirmed: false,
})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email updated`))
setStage(Stages.Done)
}
@ -83,10 +79,7 @@ export function Component() {
email: email.trim(),
token: confirmationCode.trim(),
})
updateCurrentAccount({
email: email.trim(),
emailConfirmed: false,
})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email updated`))
setStage(Stages.Done)
} catch (e) {

View File

@ -15,12 +15,7 @@ import {logger} from '#/logger'
import {useModalControls} from '#/state/modals'
import {useFetchDid, useUpdateHandleMutation} from '#/state/queries/handle'
import {useServiceQuery} from '#/state/queries/service'
import {
SessionAccount,
useAgent,
useSession,
useSessionApi,
} from '#/state/session'
import {SessionAccount, useAgent, useSession} from '#/state/session'
import {useAnalytics} from 'lib/analytics/analytics'
import {usePalette} from 'lib/hooks/usePalette'
import {cleanError} from 'lib/strings/errors'
@ -73,10 +68,10 @@ export function Inner({
const {_} = useLingui()
const pal = usePalette('default')
const {track} = useAnalytics()
const {updateCurrentAccount} = useSessionApi()
const {closeModal} = useModalControls()
const {mutateAsync: updateHandle, isPending: isUpdateHandlePending} =
useUpdateHandleMutation()
const {getAgent} = useAgent()
const [error, setError] = useState<string>('')
@ -116,9 +111,7 @@ export function Inner({
await updateHandle({
handle: newHandle,
})
updateCurrentAccount({
handle: newHandle,
})
await getAgent().resumeSession(getAgent().session!)
closeModal()
onChanged()
} catch (err: any) {
@ -134,9 +127,9 @@ export function Inner({
onChanged,
track,
closeModal,
updateCurrentAccount,
updateHandle,
serviceInfo,
getAgent,
])
// rendering

View File

@ -13,7 +13,7 @@ import {useLingui} from '@lingui/react'
import {logger} from '#/logger'
import {useModalControls} from '#/state/modals'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {usePalette} from 'lib/hooks/usePalette'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {cleanError} from 'lib/strings/errors'
@ -43,7 +43,6 @@ export function Component({
const pal = usePalette('default')
const {getAgent} = useAgent()
const {currentAccount} = useSession()
const {updateCurrentAccount} = useSessionApi()
const {_} = useLingui()
const [stage, setStage] = useState<Stages>(
showReminder ? Stages.Reminder : Stages.Email,
@ -82,7 +81,7 @@ export function Component({
email: (currentAccount?.email || '').trim(),
token: confirmationCode.trim(),
})
updateCurrentAccount({emailConfirmed: true})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email verified`))
closeModal()
onSuccess?.()

View File

@ -5,7 +5,7 @@ import {useLingui} from '@lingui/react'
import {cleanError} from '#/lib/strings/errors'
import {isNative} from '#/platform/detection'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a, useBreakpoints, useTheme} from '#/alf'
@ -30,7 +30,6 @@ export function DisableEmail2FADialog({
const t = useTheme()
const {gtMobile} = useBreakpoints()
const {currentAccount} = useSession()
const {updateCurrentAccount} = useSessionApi()
const {getAgent} = useAgent()
const [stage, setStage] = useState<Stages>(Stages.Email)
@ -61,7 +60,7 @@ export function DisableEmail2FADialog({
token: confirmationCode.trim(),
emailAuthFactor: false,
})
updateCurrentAccount({emailAuthFactor: false})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email 2FA disabled`))
}
control.close()

View File

@ -3,7 +3,7 @@ import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useModalControls} from '#/state/modals'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {ToggleButton} from 'view/com/util/forms/ToggleButton'
import {useDialogControl} from '#/components/Dialog'
import {DisableEmail2FADialog} from './DisableEmail2FADialog'
@ -11,7 +11,6 @@ import {DisableEmail2FADialog} from './DisableEmail2FADialog'
export function Email2FAToggle() {
const {_} = useLingui()
const {currentAccount} = useSession()
const {updateCurrentAccount} = useSessionApi()
const {openModal} = useModalControls()
const disableDialogCtrl = useDialogControl()
const {getAgent} = useAgent()
@ -22,11 +21,9 @@ export function Email2FAToggle() {
email: currentAccount.email,
emailAuthFactor: true,
})
updateCurrentAccount({
emailAuthFactor: true,
})
await getAgent().resumeSession(getAgent().session!)
}
}, [currentAccount, updateCurrentAccount, getAgent])
}, [currentAccount, getAgent])
const onToggle = React.useCallback(() => {
if (!currentAccount) {