/** * IS THIS SESSION SIMPLY AT THE WRONG DOOR, OR IS IT BROKEN? * * `AuthService.fetchUserData` pairs each identity with the experience it belongs to: a practitioner on * the staff mount, a patient or related person on the patient mount. When none of those pairs match it * used to fall straight through to `logout()` - which is correct for a session that resolves nobody, and * badly wrong for a perfectly valid staff account that happens to have been opened on the patient portal. * Measured in both directions on 2026-08-19: every auth key destroyed, refresh token included. * * SO THE ANSWER TURNS ON ONE QUESTION, AND IT IS WORTH BEING PURE ABOUT: does this account hold an * identity that belongs to the OTHER portal? If yes, the situation is recoverable and the person gets a * choice. If no, there is nothing to keep and the session still ends immediately - offering to preserve a * session that cannot resolve anybody would be an invitation to a loop. * * IT DELIBERATELY DOES NOT ASK WHETHER THE IDENTITY IS USABLE HERE - only whether one exists * elsewhere. Widening it to "any identity at all" would keep broken sessions alive; narrowing it to "the * identity we wanted" would never fire, because that is precisely what was just found missing. */ export type CrossPortalHolding = 'staff' | 'patient'; export const crossPortalHolding = (input: { /** True on the patient portal mount, false on the staff/admin mounts. */ isPatientMount: boolean; practitionerIds: string[]; patientIds: string[]; relatedPersonId?: string; }): CrossPortalHolding | undefined => { const { isPatientMount, practitionerIds, patientIds, relatedPersonId } = input; if (isPatientMount) { // On the patient portal, a practitioner identity is the other portal's. return practitionerIds.length > 0 ? 'staff' : undefined; } // On a staff mount, either patient-side identity is the other portal's. A RelatedPerson counts: they // sign in through the patient portal too, and losing that session costs the same emailed code. return patientIds.length > 0 || Boolean(relatedPersonId) ? 'patient' : undefined; };