feat: migrate from NextAuth to Better-Auth#6
Merged
Conversation
BREAKING CHANGE: Complete migration from NextAuth v4 to Better-Auth ### Auth Library Changes - Replace next-auth with better-auth (v1.4.16) - Replace @auth/core with better-auth/adapters/prisma - Add @prisma/adapter-pg for Prisma 7 PostgreSQL driver ### Database Setup - Add Prisma with PostgreSQL adapter - Create auth tables: user, account, session, verification - Configure DATABASE_URL for local development ### API Routes - Replace [...nextauth] route with [...all] for Better-Auth - Add toNextJsHandler adapter for Next.js App Router ### Client Changes - Remove SessionProvider (no longer needed) - Replace useSession() with authClient.useSession() - Update signIn/signOut to use authClient methods - Add auth-client.ts with createAuthClient configuration ### Component Updates - UserAvatar: migrate to authClient.useSession() - HeroSection: migrate to authClient.useSession() - Signin page: use authClient.signIn.social() - Dashboard pages: migrate to authClient.useSession() - Dashboard layout: fix authOptions bug, use auth.api.getSession() ### Configuration - Add skipTrailingSlashes: true for Better-Auth advanced config - Add basePath: /api/auth and secret for Better-Auth - Configure cookiePrefix: 'telos' ### Migration Docs - Add docs/migrate-to-better-auth.md with migration guide - Add docs/known-issues-better-auth-nextjs-trailing-slash.md Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
### Security Improvements - Remove hardcoded database credentials from db.ts and prisma.config.ts - Add production-safe secret validation in auth.ts - Throw error if BETTER_AUTH_SECRET not set in production - Show warning when using default dev secret ### Feature Improvements - Restore callbackURL parameter in signin page for custom redirects - Default callback URL changed from '/' to '/dashboard' ### Documentation - Add .env.example with comprehensive environment variable guide - Required vs optional variables - Development and production configuration examples - GitHub OAuth app setup instructions - Security best practices Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…generation client scripts
Add a mock DATABASE_URL environment variable to prevent potential build issues in CI when Prisma or Better Auth validates configuration. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add a mock BETTER_AUTH_SECRET to prevent build warnings in CI. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 概述
完成从 NextAuth v4 到 Better-Auth 的完整迁移,使用 Prisma + PostgreSQL 进行数据库持久化认证。
✨ 主要变更
🔑 认证库替换
next-auth,@auth/corebetter-auth(v1.4.16),@prisma/adapter-pg(v7.2.0)🗄️ 数据库配置
user,account,session,verificationDATABASE_URL环境变量🛣️ API 路由
[...nextauth]路由[...all]路由,使用toNextJsHandler💻 客户端迁移
useSession()authClient.useSession()status === 'loading'isPendingstatus === 'unauthenticated'!sessionsignIn('github')authClient.signIn.social({ provider: 'github' })signOut()authClient.signOut()🔧 组件更新
SessionProvider(不再需要)UserAvatar- 迁移到authClient.useSession()HeroSection- 迁移到authClient.useSession()SignIn页面 - 使用authClient.signIn.social()authClient.useSession()await authOptionsbug,改用auth.api.getSession()⚙️ 配置变更
📋 迁移步骤
/api/auth/[...nextauth]改为/api/auth/[...all]useSession()调用需改为authClient.useSession()🔍 测试检查清单
📚 文档
Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com