Replaced built-in auth with Better Auth#620
Conversation
|
Hey @ivoilic — thanks for putting this together. Replacing the built-in auth with Better Auth is a solid architectural direction, and we appreciate the effort here. Removing ~6K lines of custom auth code in favor of a well-maintained library with OAuth providers, session management, and 2FA out of the box is exactly the kind of modernization SonicJS needs. We've reviewed the PR and here's our assessment: What we like:
Current blockers:
Our plan: Would you be interested in rebasing this on latest main when we're ready to kick off that work? We'd love to collaborate on making this happen. 🙏 |
|
After further discussion, we've decided to take a different approach — instead of replacing the built-in auth entirely, we're going to build OAuth/social login as a plugin on top of the existing (now hardened) auth system. See issue #737 for the full plan. Your PR was genuinely helpful as inspiration for the provider abstraction patterns. We'll be referencing it as we build the OAuth plugin. We're closing this PR, but want to sincerely thank you @ivoilic for the work and the push toward better auth in SonicJS. If you're interested in contributing to the OAuth providers plugin (#737), we'd love to have you involved! 🙏 |
Research: Better Auth + Cloudflare Workers CompatibilityFor future reference, here's what we found when evaluating Better Auth for SonicJS's Cloudflare Workers runtime: 🚨 Critical:
|
Reopening: upstream blockers resolved, tentative v3 directionReopening this PR with updated context. @ivoilic — apologies for the long delay. Since this PR was closed on April 1, we've taken another look at the Better Auth + Cloudflare Workers landscape, and the picture has materially changed. The blockers cited at close time were actually already fixed when we wrote that comment. Upstream blocker status (re-checked)
In short: all four cited showstoppers were closed months before we cited them. That's on us — the closing comment should have held up better against re-verification. Currently open Workers-adjacent issues
No open Workers- or D1-blocking issues beyond these. The Better Auth itself is on v1.6.11 (May 12, 2026) with v1.7 in beta — shipping roughly weekly. Why we're reconsidering: BetterAuth vs current SonicJS authIssues like #803 (custom profile fields stripped from Feature matrixCore auth
Social / Enterprise SSO
MFA
Authorization / RBAC
Multi-tenancy
API & tokens
Admin
DX
Compliance
Tentative plan: target v3.0We're going to tentatively plan a Better Auth migration as part of v3.0. The reasoning:
Open questions we'll work through before merging:
@ivoilic — would love your collaborationYour original PR remains the right architectural direction. If you have bandwidth and interest in picking this back up against current Marking this as a v3 milestone target. Will leave the PR open while we scope and align. |
Description
Replaces custom JWT auth with Better Auth. Rather than maintain auth on top of Sonicjs itself this outsources that work to Better Auth and allows end users to setup a larger number of secure auth methods. This is a first pass for consideration and certainly needs more work. Sign-in/sign-up at
/auth/sign-in/emailand/auth/sign-up/email. Session in HTTP-only cookie. OTP Login and Magic Link plugins removed but magic link/email OTP can no be added viaauth.extendBetterAuth. RBAC and registration gating kept via Better Auth hooks.Env:
BETTER_AUTH_SECRET,BETTER_AUTH_URLChanges
auth/config.ts(Better Auth + Drizzle, hooks). App mounts handler at/auth/*, session middleware setsc.set('user'). Auth middleware drops JWT/KV.requireAuth/requireRoleuse session. Login/register forms POST to Better Auth. Migrations 032 (Better Auth tables,users.name), 033 (drop otp/magic_link tables).Testing
npm testpasses (40 files, 1174 tests)npm run e2eornpm run e2e:smoke— run locally to confirmChecklist