Skip to content

fix(auth): sort org memberships by creation date#1268

Merged
saddlepaddle merged 1 commit into
mainfrom
sort-orgs-by-creation-date
Feb 7, 2026
Merged

fix(auth): sort org memberships by creation date#1268
saddlepaddle merged 1 commit into
mainfrom
sort-orgs-by-creation-date

Conversation

@saddlepaddle
Copy link
Copy Markdown
Collaborator

@saddlepaddle saddlepaddle commented Feb 7, 2026

Summary

  • Adds orderBy: desc(members.createdAt) to membership queries so the most recently created org is selected by default when no active org is set on the session
  • Fixes the issue where users get signed into older test orgs instead of their primary org

Changes

  • packages/auth/src/server.tscustomSession plugin now picks newest membership first
  • packages/trpc/src/router/user/user.tsmyOrganization and myOrganizations both return orgs newest-first

Test plan

  • Sign out and sign back in — should land on the most recently created org
  • Org switcher list should show newest orgs first

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved membership selection consistency to reliably choose the most recently created record when multiple options are available, ensuring more predictable behavior across user account and organization features.

…ected by default

When activeOrganizationId is unset, findFirst() returned whichever
membership the database happened to return first (typically oldest).
This caused users to be signed into older test orgs instead of their
primary org.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 7, 2026

📝 Walkthrough

Walkthrough

This PR adds deterministic ordering to membership queries by importing the desc function from drizzle-orm and applying orderBy: desc(members.createdAt) to membership lookups across two files. This ensures that when multiple memberships match the query criteria, the most recently created membership is consistently selected.

Changes

Cohort / File(s) Summary
Membership Query Ordering
packages/auth/src/server.ts, packages/trpc/src/router/user/user.ts
Added desc import from drizzle-orm and applied orderBy: desc(members.createdAt) to membership queries: customSession flow in server.ts, and myOrganization/myOrganizations in user.ts to ensure deterministic selection of the most recently created membership.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 ✨

Fresh memberships hop into sight,
Ordered newest, oh what delight!
desc(createdAt) guides the way,
Latest wins, deterministic day! 🎯

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: sorting organization memberships by creation date to resolve membership selection issues.
Description check ✅ Passed The description includes a clear summary of changes and test plan, though it omits several template sections like Related Issues and Type of Change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sort-orgs-by-creation-date

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 7, 2026

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch
  • ✅ Electric Fly.io app

Thank you for your contribution! 🎉

@saddlepaddle saddlepaddle merged commit 7ef0827 into main Feb 7, 2026
14 checks passed
@Kitenite Kitenite deleted the sort-orgs-by-creation-date branch February 7, 2026 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant