Unregister push token on signout (#8661)
* unregister push - WIP * create agent and submit push token revokation * mock unregisterpush * fix import * Add proxy headers --------- Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
@@ -12,6 +12,11 @@ jest.mock('jwt-decode', () => ({
|
||||
|
||||
jest.mock('../../birthdate')
|
||||
jest.mock('../../../ageAssurance/data')
|
||||
jest.mock('#/lib/notifications/notifications', () => ({
|
||||
unregisterPushToken(_agents: BskyAgent[]) {
|
||||
return Promise.resolve()
|
||||
},
|
||||
}))
|
||||
|
||||
describe('session', () => {
|
||||
it('can log in and out', () => {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import {type AtpSessionEvent, type BskyAgent} from '@atproto/api'
|
||||
import {type AtpAgent, type AtpSessionEvent} from '@atproto/api'
|
||||
|
||||
import {unregisterPushToken} from '#/lib/notifications/notifications'
|
||||
import {logger} from '#/lib/notifications/util'
|
||||
import {createPublicAgent} from './agent'
|
||||
import {wrapSessionReducerForLogging} from './logging'
|
||||
import {type SessionAccount} from './types'
|
||||
import {createTemporaryAgentsAndResume} from './util'
|
||||
|
||||
// A hack so that the reducer can't read anything from the agent.
|
||||
// From the reducer's point of view, it should be a completely opaque object.
|
||||
@@ -137,6 +140,23 @@ let reducer = (state: State, action: Action): State => {
|
||||
}
|
||||
case 'removed-account': {
|
||||
const {accountDid} = action
|
||||
|
||||
// side effect
|
||||
const account = state.accounts.find(a => a.did === accountDid)
|
||||
if (account) {
|
||||
createTemporaryAgentsAndResume([account])
|
||||
.then(agents => unregisterPushToken(agents))
|
||||
.then(() =>
|
||||
logger.debug('Push token unregistered', {did: accountDid}),
|
||||
)
|
||||
.catch(err => {
|
||||
logger.error('Failed to unregister push token', {
|
||||
did: accountDid,
|
||||
error: err,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
accounts: state.accounts.filter(a => a.did !== accountDid),
|
||||
currentAgentState:
|
||||
@@ -148,9 +168,26 @@ let reducer = (state: State, action: Action): State => {
|
||||
}
|
||||
case 'logged-out-current-account': {
|
||||
const {currentAgentState} = state
|
||||
const accountDid = currentAgentState.did
|
||||
// side effect
|
||||
const account = state.accounts.find(a => a.did === accountDid)
|
||||
if (account && accountDid) {
|
||||
createTemporaryAgentsAndResume([account])
|
||||
.then(agents => unregisterPushToken(agents))
|
||||
.then(() =>
|
||||
logger.debug('Push token unregistered', {did: accountDid}),
|
||||
)
|
||||
.catch(err => {
|
||||
logger.error('Failed to unregister push token', {
|
||||
did: accountDid,
|
||||
error: err,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
accounts: state.accounts.map(a =>
|
||||
a.did === currentAgentState.did
|
||||
a.did === accountDid
|
||||
? {
|
||||
...a,
|
||||
refreshJwt: undefined,
|
||||
@@ -163,6 +200,15 @@ let reducer = (state: State, action: Action): State => {
|
||||
}
|
||||
}
|
||||
case 'logged-out-every-account': {
|
||||
createTemporaryAgentsAndResume(state.accounts)
|
||||
.then(agents => unregisterPushToken(agents))
|
||||
.then(() => logger.debug('Push token unregistered'))
|
||||
.catch(err => {
|
||||
logger.error('Failed to unregister push token', {
|
||||
error: err,
|
||||
})
|
||||
})
|
||||
|
||||
return {
|
||||
accounts: state.accounts.map(a => ({
|
||||
...a,
|
||||
@@ -187,7 +233,7 @@ let reducer = (state: State, action: Action): State => {
|
||||
}
|
||||
case 'partial-refresh-session': {
|
||||
const {accountDid, patch} = action
|
||||
const agent = state.currentAgentState.agent as BskyAgent
|
||||
const agent = state.currentAgentState.agent as AtpAgent
|
||||
|
||||
/*
|
||||
* Only mutating values that are safe. Be very careful with this.
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import AtpAgent from '@atproto/api'
|
||||
import {jwtDecode} from 'jwt-decode'
|
||||
|
||||
import {isJwtExpired} from '#/lib/jwt'
|
||||
import {hasProp} from '#/lib/type-guards'
|
||||
import * as persisted from '#/state/persisted'
|
||||
import {sessionAccountToSession} from './agent'
|
||||
import {type SessionAccount} from './types'
|
||||
|
||||
export function readLastActiveAccount() {
|
||||
@@ -28,3 +30,32 @@ export function isSessionExpired(account: SessionAccount) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and attempted to resumeSession for every stored session.
|
||||
* Intended to be used to send push token revokations just before logout.
|
||||
*/
|
||||
export async function createTemporaryAgentsAndResume(
|
||||
accounts: SessionAccount[],
|
||||
) {
|
||||
const agents = await Promise.allSettled(
|
||||
accounts.map(async account => {
|
||||
const agent: AtpAgent = new AtpAgent({service: account.service})
|
||||
if (account.pdsUrl) {
|
||||
agent.sessionManager.pdsUrl = new URL(account.pdsUrl)
|
||||
}
|
||||
|
||||
const session = sessionAccountToSession(account)
|
||||
const res = await agent.resumeSession(session)
|
||||
if (!res.success) throw new Error('Failed to resume session')
|
||||
|
||||
agent.assertAuthenticated() // confirm auth success
|
||||
|
||||
return agent
|
||||
}),
|
||||
)
|
||||
|
||||
return agents
|
||||
.filter(x => x.status === 'fulfilled')
|
||||
.map(promise => promise.value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user