[PR #88] feat: migrate from NextAuth to Better Auth #88

Open
opened 2026-10-27 21:09:27 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Comfy-Org/Comfy-PR/pull/88
Author: @snomiao
Created: 10/20/2025
Status: 🔄 Open

Base: main ← Head: sno-better-auth


📝 Commits (10+)

  • 2a25dd5 feat: migrate from NextAuth to Better Auth
  • 4a0d9d4 fix: update import paths and fix headers in getAuthUser
  • 8304618 fix: correct better-auth integration issues
  • f785396 fix: ensure type safety in getAuthUser without using 'as any'
  • 7d3e350 fix: add null checks for user in auth-protected pages
  • ffac697 fix: add backward compatibility for NextAuth environment variables
  • 6939f22 fix: remove conflicting NextAuth route and fix Better Auth API endpoint
  • dd2c3aa fix: add CORS support and fix MongoDB adapter for Better Auth
  • 89f5957 fix: type assertion for MongoDB adapter compatibility
  • ae0923c fix: address PR review comments - improve type safety and env var handling

📊 Changes

18 files changed (+425 additions, -159 deletions)

View changed files

📝 .env.example (+19 -0)
➕ MIGRATION.md (+122 -0)
📝 app/(dashboard)/followup/actions/send-gmail/page.tsx (+7 -11)
📝 app/(dashboard)/rules/layout.tsx (+2 -2)
➕ app/api/auth/[...all]/route.ts (+4 -0)
➖ app/api/auth/[...nextauth]/Users.tsx (+0 -12)
➖ app/api/auth/[...nextauth]/auth.ts (+0 -23)
➖ app/api/auth/[...nextauth]/getAuthUser.tsx (+0 -19)
➖ app/api/auth/[...nextauth]/route.ts (+0 -2)
➖ app/auth/login/page-bak.tsx (+0 -30)
📝 app/auth/login/page.tsx (+4 -6)
📝 app/tasks/github-action-update/actions.tsx (+1 -1)
📝 app/tasks/github-action-update/page.tsx (+2 -2)
📝 bun.lock (+118 -51)
➕ lib/auth-client.ts (+36 -0)
➕ lib/auth.ts (+64 -0)
➕ lib/getAuthUser.ts (+45 -0)
📝 package.json (+1 -0)

📄 Description

Summary

This PR migrates the authentication system from NextAuth v5 to Better Auth, a more modern and actively maintained authentication library.

Changes Made

New Files

  • lib/auth.ts: Better Auth server configuration with MongoDB adapter
  • lib/auth-client.ts: Client-side auth exports (signIn, signOut, useSession)
  • lib/getAuthUser.ts: Migrated auth user utility with Better Auth session API
  • app/api/auth/[...all]/route.ts: Better Auth API route handler
  • MIGRATION.md: Comprehensive migration documentation

Modified Files

  • app/auth/login/page.tsx: Updated to use Better Auth client methods
  • .env.example: Added Better Auth environment variable documentation
  • package.json: Added better-auth@^1.3.28 dependency

Features Preserved

  • ✅ GitHub OAuth authentication
  • ✅ Google OAuth authentication
  • ✅ MongoDB user storage and adapter
  • ✅ Admin role assignment (@comfy.org and @drip.art emails)
  • ✅ Session management

Test Plan

  • Verify GitHub OAuth login flow
  • Verify Google OAuth login flow
  • Confirm session persistence across page refreshes
  • Validate admin role assignment for @comfy.org emails
  • Test sign out functionality
  • Verify protected routes still work correctly
  • Check user data association in MongoDB

Breaking Changes

API Changes

  1. Session Object Structure: Better Auth may have a different session object structure. All places where session.user is accessed should be reviewed.

  2. Server-side Session Access:

    // Before
    const session = await auth();
    
    // After
    const session = await auth.api.getSession({ headers });
    
  3. Client-side Sign In:

    // Before
    signIn("google")
    
    // After
    signIn.social({ provider: "google" })
    

Environment Variables

Better Auth uses the same OAuth provider environment variables as NextAuth:

  • AUTH_GITHUB_ID / AUTH_GITHUB_SECRET
  • AUTH_GOOGLE_ID / AUTH_GOOGLE_SECRET

Additional optional variables:

  • BETTER_AUTH_SECRET - Session encryption key
  • BETTER_AUTH_URL - Application base URL
  • NEXT_PUBLIC_APP_URL - Public URL for client-side auth

Next Steps

  1. Test all authentication flows in staging
  2. Verify no regressions in user experience
  3. Remove old NextAuth files after successful migration:
    • app/api/auth/[...nextauth]/auth.ts
    • app/api/auth/[...nextauth]/route.ts
    • app/api/auth/[...nextauth]/getAuthUser.tsx
    • app/api/auth/[...nextauth]/Users.tsx
  4. Remove next-auth from dependencies

Documentation

See MIGRATION.md for detailed migration documentation including rollback plan.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Comfy-Org/Comfy-PR/pull/88 **Author:** [@snomiao](https://github.com/snomiao) **Created:** 10/20/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `sno-better-auth` --- ### 📝 Commits (10+) - [`2a25dd5`](https://github.com/Comfy-Org/Comfy-PR/commit/2a25dd562180ec0bccba559294e0a2d797de6eb0) feat: migrate from NextAuth to Better Auth - [`4a0d9d4`](https://github.com/Comfy-Org/Comfy-PR/commit/4a0d9d40ca35b0b65db462b22911547e6b3b45b9) fix: update import paths and fix headers in getAuthUser - [`8304618`](https://github.com/Comfy-Org/Comfy-PR/commit/8304618e6aa1f4e183c2dd0de109ae91c7c1eb99) fix: correct better-auth integration issues - [`f785396`](https://github.com/Comfy-Org/Comfy-PR/commit/f7853966c00e8b150af95b9f4be54d4efb4692c0) fix: ensure type safety in getAuthUser without using 'as any' - [`7d3e350`](https://github.com/Comfy-Org/Comfy-PR/commit/7d3e35099e1e348e053e6cd044d38bb50308511e) fix: add null checks for user in auth-protected pages - [`ffac697`](https://github.com/Comfy-Org/Comfy-PR/commit/ffac69726091fd86ed4008374407051e1e2b1d46) fix: add backward compatibility for NextAuth environment variables - [`6939f22`](https://github.com/Comfy-Org/Comfy-PR/commit/6939f22a7aa88300869993a3748bfd957285e94b) fix: remove conflicting NextAuth route and fix Better Auth API endpoint - [`dd2c3aa`](https://github.com/Comfy-Org/Comfy-PR/commit/dd2c3aa8a0bc6e4f1809b4dcb5eafa4afb00bb06) fix: add CORS support and fix MongoDB adapter for Better Auth - [`89f5957`](https://github.com/Comfy-Org/Comfy-PR/commit/89f59572ac1c3aed96f8f73cbe17f64373da3bcc) fix: type assertion for MongoDB adapter compatibility - [`ae0923c`](https://github.com/Comfy-Org/Comfy-PR/commit/ae0923c490aab868e4f8de2bae52b581dc3d6385) fix: address PR review comments - improve type safety and env var handling ### 📊 Changes **18 files changed** (+425 additions, -159 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+19 -0) ➕ `MIGRATION.md` (+122 -0) 📝 `app/(dashboard)/followup/actions/send-gmail/page.tsx` (+7 -11) 📝 `app/(dashboard)/rules/layout.tsx` (+2 -2) ➕ `app/api/auth/[...all]/route.ts` (+4 -0) ➖ `app/api/auth/[...nextauth]/Users.tsx` (+0 -12) ➖ `app/api/auth/[...nextauth]/auth.ts` (+0 -23) ➖ `app/api/auth/[...nextauth]/getAuthUser.tsx` (+0 -19) ➖ `app/api/auth/[...nextauth]/route.ts` (+0 -2) ➖ `app/auth/login/page-bak.tsx` (+0 -30) 📝 `app/auth/login/page.tsx` (+4 -6) 📝 `app/tasks/github-action-update/actions.tsx` (+1 -1) 📝 `app/tasks/github-action-update/page.tsx` (+2 -2) 📝 `bun.lock` (+118 -51) ➕ `lib/auth-client.ts` (+36 -0) ➕ `lib/auth.ts` (+64 -0) ➕ `lib/getAuthUser.ts` (+45 -0) 📝 `package.json` (+1 -0) </details> ### 📄 Description ## Summary This PR migrates the authentication system from NextAuth v5 to Better Auth, a more modern and actively maintained authentication library. ## Changes Made ### New Files - **`lib/auth.ts`**: Better Auth server configuration with MongoDB adapter - **`lib/auth-client.ts`**: Client-side auth exports (`signIn`, `signOut`, `useSession`) - **`lib/getAuthUser.ts`**: Migrated auth user utility with Better Auth session API - **`app/api/auth/[...all]/route.ts`**: Better Auth API route handler - **`MIGRATION.md`**: Comprehensive migration documentation ### Modified Files - **`app/auth/login/page.tsx`**: Updated to use Better Auth client methods - **`.env.example`**: Added Better Auth environment variable documentation - **`package.json`**: Added `better-auth@^1.3.28` dependency ## Features Preserved - ✅ GitHub OAuth authentication - ✅ Google OAuth authentication - ✅ MongoDB user storage and adapter - ✅ Admin role assignment (@comfy.org and @drip.art emails) - ✅ Session management ## Test Plan - [ ] Verify GitHub OAuth login flow - [ ] Verify Google OAuth login flow - [ ] Confirm session persistence across page refreshes - [ ] Validate admin role assignment for @comfy.org emails - [ ] Test sign out functionality - [ ] Verify protected routes still work correctly - [ ] Check user data association in MongoDB ## Breaking Changes ### API Changes 1. **Session Object Structure**: Better Auth may have a different session object structure. All places where `session.user` is accessed should be reviewed. 2. **Server-side Session Access**: ```ts // Before const session = await auth(); // After const session = await auth.api.getSession({ headers }); ``` 3. **Client-side Sign In**: ```ts // Before signIn("google") // After signIn.social({ provider: "google" }) ``` ## Environment Variables Better Auth uses the same OAuth provider environment variables as NextAuth: - `AUTH_GITHUB_ID` / `AUTH_GITHUB_SECRET` - `AUTH_GOOGLE_ID` / `AUTH_GOOGLE_SECRET` Additional optional variables: - `BETTER_AUTH_SECRET` - Session encryption key - `BETTER_AUTH_URL` - Application base URL - `NEXT_PUBLIC_APP_URL` - Public URL for client-side auth ## Next Steps 1. Test all authentication flows in staging 2. Verify no regressions in user experience 3. Remove old NextAuth files after successful migration: - `app/api/auth/[...nextauth]/auth.ts` - `app/api/auth/[...nextauth]/route.ts` - `app/api/auth/[...nextauth]/getAuthUser.tsx` - `app/api/auth/[...nextauth]/Users.tsx` 4. Remove `next-auth` from dependencies ## Documentation See [MIGRATION.md](./MIGRATION.md) for detailed migration documentation including rollback plan. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Comfy-Org/Comfy-PR#88
No description provided.