41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { unregisterCurrentDevice } from '../features/devices/device-service'
|
|
import { detachPushRegistrationForLogout } from '../features/notifications/notification-service'
|
|
import { getMobileRuntimeConfig } from './native-config'
|
|
import { signOutWithLocalFallback, type LocalLogoutResult } from './logout'
|
|
|
|
export interface AccountLogoutInput {
|
|
userId: string | null
|
|
deviceId: string | null
|
|
purgeLocalSession: () => Promise<void>
|
|
}
|
|
|
|
export interface AccountLogoutOperations {
|
|
detachPush: (userId: string, deviceId: string | null) => Promise<void>
|
|
unregisterDevice: (userId: string, installationId: string) => Promise<unknown>
|
|
installationId: () => string
|
|
signOut: (purgeLocalSession: () => Promise<void>) => Promise<LocalLogoutResult>
|
|
}
|
|
|
|
const accountLogoutOperations: AccountLogoutOperations = {
|
|
detachPush: detachPushRegistrationForLogout,
|
|
unregisterDevice: unregisterCurrentDevice,
|
|
installationId: () => getMobileRuntimeConfig().installationId,
|
|
signOut: signOutWithLocalFallback,
|
|
}
|
|
|
|
/** Keeps authenticated server detach operations ahead of local session loss. */
|
|
export async function performAccountLogout(
|
|
input: AccountLogoutInput,
|
|
operations: AccountLogoutOperations = accountLogoutOperations,
|
|
): Promise<LocalLogoutResult> {
|
|
if (input.userId !== null) {
|
|
await operations.detachPush(input.userId, input.deviceId)
|
|
try {
|
|
await operations.unregisterDevice(input.userId, operations.installationId())
|
|
} catch {
|
|
// Device unregister is best effort after push detach; local privacy and
|
|
// refresh-token deletion must still proceed while offline.
|
|
}
|
|
}
|
|
return operations.signOut(input.purgeLocalSession)
|
|
}
|