feat: Migrate from NextAuth to Better Auth

Backend changes:
- Add better-auth and pg packages
- Create Better Auth instance with PostgreSQL adapter
- Add Better Auth route handler at /api/auth/*
- Create migration script for Better Auth database schema
- Update main index to use Better Auth routes instead of custom auth
- Configure email/password and OAuth (GitHub/Google) providers

Frontend changes:
- Add better-auth client
- Create Better Auth client instance configuration
- Update lib/auth.ts to use Better Auth session
- Rewrite sign-in page with Better Auth methods
- Rewrite sign-up page with Better Auth methods
- Remove NextAuth route handler

Documentation:
- Add comprehensive migration guide with setup instructions
- Document environment variables and API endpoints
- Include testing checklist and rollback plan

Benefits:
- Unified authentication for both Elysia backend and Next.js frontend
- Database-backed sessions (more secure than JWT)
- Better TypeScript support
- Extensible plugin system for future features
- Active development and frequent updates
This commit is contained in:
Francesco
2026-02-20 04:13:26 +00:00
parent 73282c71af
commit f8356e0945
12 changed files with 583 additions and 154 deletions

View File

@@ -1,12 +1,12 @@
import { auth } from '@/app/api/auth/[...nextauth]/route'
import { redirect } from 'next/navigation'
import { authClient } from '@/lib/better-auth';
import { redirect } from 'next/navigation';
export async function requireAuth() {
const session = await auth()
const { data: session } = await authClient.getSession();
if (!session || !session.user) {
redirect('/auth/signin')
redirect('/auth/signin');
}
return session
return session;
}