[PR #8694] fix(organization): filter null organizations in listUserInvitations #16397

Closed
opened 2026-04-13 10:30:42 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/8694

State: closed
Merged: Yes


Summary

  • Fixed a crash in listUserInvitations caused by invitations whose associated organization has been deleted (or is otherwise missing from a join result).
  • Added .filter(Boolean) to skip null/undefined entries before mapping.
  • Used optional chaining (organization?.name) to safely access the organization name.

Background

When listUserInvitations performs a joined query over the invitation model, it's possible for the resolved organization to be null — for example, if the organization was deleted after the invitation was created but before the invitations were listed. Previously this caused an uncaught runtime crash when destructuring { organization, ...inv } and then accessing organization.name on a null value.

Changes

packages/better-auth/src/plugins/organization/adapter.ts

  • listUserInvitations: added .filter(Boolean) before .map(...) to drop null entries returned from the join.
  • Changed organization.nameorganization?.name to safely handle any null organization that slips through.

Breaking Changes

None.

Deprecations

None.

Screenshots

N/A — no UI changes.

Fixes a runtime crash in the organization plugin when listing invitations for a user whose invitation's organization no longer exists.

Issue: https://github.com/better-auth/better-auth/issues/8691

Summary by cubic

Prevented crashes in listUserInvitations when an invitation’s organization is null. We now filter out falsy invitations and safely read the organization name with optional chaining.

Written for commit 6b52652fb1. Summary will update on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8694 **State:** closed **Merged:** Yes --- ## Summary - Fixed a crash in `listUserInvitations` caused by invitations whose associated organization has been deleted (or is otherwise missing from a join result). - Added `.filter(Boolean)` to skip null/undefined entries before mapping. - Used optional chaining (`organization?.name`) to safely access the organization name. ## Background When `listUserInvitations` performs a joined query over the `invitation` model, it's possible for the resolved `organization` to be `null` — for example, if the organization was deleted after the invitation was created but before the invitations were listed. Previously this caused an uncaught runtime crash when destructuring `{ organization, ...inv }` and then accessing `organization.name` on a null value. ## Changes **`packages/better-auth/src/plugins/organization/adapter.ts`** - `listUserInvitations`: added `.filter(Boolean)` before `.map(...)` to drop null entries returned from the join. - Changed `organization.name` → `organization?.name` to safely handle any null organization that slips through. ## Breaking Changes None. ## Deprecations None. ## Screenshots N/A — no UI changes. ## Related Issues Fixes a runtime crash in the organization plugin when listing invitations for a user whose invitation's organization no longer exists. Issue: https://github.com/better-auth/better-auth/issues/8691 <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Prevented crashes in `listUserInvitations` when an invitation’s organization is null. We now filter out falsy invitations and safely read the organization name with optional chaining. <sup>Written for commit 6b52652fb121f73d6e0d91eb62bd90d2102f92ad. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 10:30:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16397