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

Replace updateCurrentAccount() with resumeSession()
This commit is contained in:
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
}