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,67 +0,0 @@
import NextAuth from 'next-auth'
import GitHub from 'next-auth/providers/github'
import Google from 'next-auth/providers/google'
import Credentials from 'next-auth/providers/credentials'
import type { NextAuthConfig } from 'next-auth'
export const config: NextAuthConfig = {
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
Google({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
Credentials({
name: 'Credentials',
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
// Call backend API to verify credentials
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials)
})
const user = await res.json()
if (res.ok && user) {
return user
}
return null
}
})
],
pages: {
signIn: '/auth/signin',
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id
token.email = user.email
token.name = user.name
}
return token
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string
session.user.email = token.email as string
}
return session
}
},
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60, // 30 days
}
}
export const { handlers, auth, signIn, signOut } = NextAuth(config)
export { handlers as GET, handlers as POST }

View File

@@ -1,6 +1,6 @@
'use client';
import { signIn } from 'next-auth/react';
import { signIn } from '@/lib/better-auth';
import { useState } from 'react';
export default function SignIn() {
@@ -15,14 +15,13 @@ export default function SignIn() {
setError('');
try {
const result = await signIn('credentials', {
const result = await signIn.email({
email,
password,
redirect: false
});
if (result?.error) {
setError('Invalid credentials');
if (result.error) {
setError(result.error.message || 'Invalid credentials');
} else {
window.location.href = '/';
}
@@ -33,6 +32,17 @@ export default function SignIn() {
}
};
const handleSocialSignIn = async (provider: 'github' | 'google') => {
try {
await signIn.social({
provider,
callbackURL: '/',
});
} catch (err) {
setError(`Failed to sign in with ${provider}`);
}
};
return (
<div className="min-h-screen bg-gradient-to-br from-slate-900 to-slate-800 flex items-center justify-center p-4">
<div className="max-w-md w-full bg-slate-800/50 rounded-lg p-8 border border-slate-700">
@@ -99,16 +109,16 @@ export default function SignIn() {
<div className="mt-6 grid grid-cols-2 gap-4">
<button
onClick={() => signIn('github')}
onClick={() => handleSocialSignIn('github')}
className="bg-slate-700 hover:bg-slate-600 text-white font-semibold py-3 rounded-lg transition flex items-center justify-center gap-2"
>
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546 1.377-1.333 1.377-1.333 1.06 0 1.783.591 1.783.591.266 0 .494-.107.68-.297.107-.297.469-.936.469-1.684 0-1.251-1.006-2.261-2.261-2.261-.965 0-1.757.781-1.757 1.753 0 .286.08.526.212.743.265.265.265.265.673 0 .995-.265.323-.646.454-.646.454-.323 0-.543-.181-.699-.468-.156-.287-.234-.744-.234-1.364v-2.261c-3.37.726-4.148-1.417-4.148-1.417-.557 1.39-1.353 1.39-1.353 1.073 0 1.814.603 1.814.603.277 0 .516-.111.728-.296.212-.185.313-.61.313-1.303 0-1.258-1.018-2.274-2.274-2.274-.984 0-1.796.802-1.796 1.796 0 .29.095.536.26.758.26.26.26.669 0 .996-.266.327-.649.457-.649.457-.33 0-.556-.186-.713-.48-.157-.293-.236-.767-.236-1.404v-2.279c-3.404.741-4.242-1.447-4.242-1.447-.569 1.416-1.379 1.416-1.379 1.084 0 1.829.616 1.829.616.283 0 .523-.113.742-.301.22-.188.327-.626.327-1.323 0-1.265-1.03-2.29-2.29-2.29-1.006 0-1.831.825-1.831 1.831 0 .294.099.543.277.767.277.277.277.693 0 1.004-.27.311-.663.437-.663.437-.34 0-.571-.197-.736-.506-.165-.31-.248-.794-.248-1.447v-2.293c-3.432.748-4.338-1.48-4.338-1.48-.583 1.44-1.404 1.44-1.404 1.095 0 1.846.629 1.846.629.29 0 .537-.116.76-.308.223-.192.34-.648.34-1.35 0-1.271-1.044-2.304-2.304-2.304-1.029 0-1.867.839-1.867 1.867 0 .298.102.55.286.775.286.286.286.718 0 1.039-.278.316-.682.443-.682.443-.349 0-.597-.204-.761-.523-.165-.32-.248-.825-.248-1.491v-2.307c-3.462.756-4.432-1.514-4.432-1.514-.597 1.463-1.431 1.463-1.431 1.105 0 1.864.64 1.864.64.297 0 .55-.119.774-.313.224-.193.353-.672.353-1.377 0-1.277-1.059-2.318-2.318-2.318-1.053 0-1.904.865-1.904 1.904 0 .302.105.557.297.786.297.297.297.741 0 1.075-.284.32-.716.447-.716.447-.358 0-.622-.211-.788-.549-.167-.338-.25-.858-.25-1.536v-2.322c-3.49.764-4.525-1.549-4.525-1.549-.611 1.487-1.457 1.487-1.457 1.116 0 1.882.651 1.882.651.303 0 .562-.123.792-.319.23-.196.361-.696.361-1.405 0-1.283-1.074-2.332-2.332-2.332-1.078 0-1.94.881-1.94 1.94 0 .306.107.567.303.798.303.303.303.763 0 1.111-.29.325-.75.452-.75.452-.367 0-.646-.219-.814-.575-.168-.357-.254-.891-.254-1.582v-2.336c-3.52.772-4.617-1.585-4.617-1.585-.625 1.511-1.484 1.511-1.484 1.127 0 1.9.663 1.9.663.309 0 .574-.127.81-.326.236-.199.368-.721.368-1.432 0-1.29-1.089-2.346-2.346-2.346-1.103 0-1.976.904-1.976 1.976 0 .31.109.579.31.81.31.31.31.784 0 1.147-.298.331-.783.457-.783.457-.376 0-.67-.227-.842-.602-.172-.376-.259-.923-.259-1.628v-2.35z"/>
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546 1.377-1.333 1.377-1.333 1.06 0 1.783.591 1.783.591.266 0 .494-.107.68-.297.107-.297.469-.936.469-1.684 0-1.251-1.006-2.261-2.261-2.261-.965 0-1.757.781-1.757 1.753 0 .286.08.526.212.743.265.265.265.673 0 .995-.265.323-.646.454-.646.454-.323 0-.543-.181-.699-.468-.156-.287-.234-.744-.234-1.364v-2.261c-3.37.726-4.148-1.417-4.148-1.417-.557 1.39-1.353 1.39-1.353 1.073 0 1.814.603 1.814.603.277 0 .516-.111.728-.296.212-.185.313-.61.313-1.303 0-1.258-1.018-2.274-2.274-2.274-.984 0-1.796.802-1.796 1.796 0 .29.095.536.26.758.26.26.26.669 0 .996-.266.327-.649.457-.649.457-.33 0-.556-.186-.713-.48-.157-.293-.236-.767-.236-1.404v-2.279c-3.404.741-4.242-1.447-4.242-1.447-.569 1.416-1.379 1.416-1.379 1.084 0 1.829.616 1.829.616.283 0 .523-.113.742-.301.22-.188.327-.626.327-1.323 0-1.265-1.03-2.29-2.29-2.29-1.006 0-1.831.825-1.831 1.831 0 .294.099.543.277.767.277.277.277.693 0 1.004-.27.311-.663.437-.663.437-.34 0-.571-.197-.736-.506-.165-.31-.248-.794-.248-1.447v-2.293c-3.432.748-4.338-1.48-4.338-1.48-.583 1.44-1.404 1.44-1.404 1.095 0 1.846.629 1.846.629.29 0 .537-.116.76-.308.223-.192.34-.648.34-1.35 0-1.271-1.044-2.304-2.304-2.304-1.029 0-1.867.839-1.867 1.867 0 .298.102.55.286.775.286.286.286.718 0 1.039-.278.316-.682.443-.682.443-.349 0-.597-.204-.761-.523-.165-.32-.248-.825-.248-1.491v-2.307c-3.462.756-4.432-1.514-4.432-1.514-.597 1.463-1.431 1.463-1.431 1.105 0 1.864.64 1.864.64.297 0 .55-.119.774-.313.224-.193.353-.672.353-1.377 0-1.277-1.059-2.318-2.318-2.318-1.053 0-1.904.865-1.904 1.904 0 .302.105.557.297.786.297.297.297.741 0 1.075-.284.32-.716.447-.716.447-.358 0-.622-.211-.788-.549-.167-.338-.25-.858-.25-1.536v-2.322c-3.49.764-4.525-1.549-4.525-1.549-.611 1.487-1.457 1.487-1.457 1.116 0 1.882.651 1.882.651.303 0 .562-.123.792-.319.23-.196.361-.696.361-1.405 0-1.283-1.074-2.332-2.332-2.332-1.078 0-1.94.881-1.94 1.94 0 .306.107.567.303.798.303.303.303.763 0 1.111-.29.325-.75.452-.75.452-.367 0-.646-.219-.814-.575-.168-.357-.254-.891-.254-1.582v-2.336c-3.52.772-4.617-1.585-4.617-1.585-.625 1.511-1.484 1.511-1.484 1.127 0 1.9.663 1.9.663.309 0 .574-.127.81-.326.236-.199.368-.721.368-1.432 0-1.29-1.089-2.346-2.346-2.346-1.103 0-1.976.904-1.976 1.976 0 .31.109.579.31.81.31.31.31.784 0 1.147-.298.331-.783.457-.783.457-.376 0-.67-.227-.842-.602-.172-.376-.259-.923-.259-1.628v-2.35z"/>
</svg>
GitHub
</button>
<button
onClick={() => signIn('google')}
onClick={() => handleSocialSignIn('google')}
className="bg-slate-700 hover:bg-slate-600 text-white font-semibold py-3 rounded-lg transition flex items-center justify-center gap-2"
>
<svg className="w-5 h-5" viewBox="0 0 24 24">

View File

@@ -1,69 +1,39 @@
'use client';
import { signUp } from '@/lib/better-auth';
import { useState } from 'react';
export default function SignUp() {
const [formData, setFormData] = useState({
name: '',
email: '',
password: '',
confirmPassword: ''
});
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [success, setSuccess] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
const handleSignUp = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError('');
if (formData.password !== formData.confirmPassword) {
setError('Passwords do not match');
setLoading(false);
return;
}
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: formData.name,
email: formData.email,
password: formData.password
})
const result = await signUp.email({
email,
password,
name,
});
const data = await response.json();
if (!response.ok) {
setError(data.error || 'Registration failed');
if (result.error) {
setError(result.error.message || 'Sign up failed');
} else {
setSuccess(true);
setTimeout(() => {
window.location.href = '/auth/signin';
}, 2000);
window.location.href = '/';
}
} catch (err) {
setError('Registration failed');
setError('Sign up failed');
} finally {
setLoading(false);
}
};
if (success) {
return (
<div className="min-h-screen bg-gradient-to-br from-slate-900 to-slate-800 flex items-center justify-center p-4">
<div className="max-w-md w-full bg-slate-800/50 rounded-lg p-8 border border-slate-700 text-center">
<div className="text-green-400 text-6xl mb-4"></div>
<h2 className="text-2xl font-bold text-white mb-2">Account Created!</h2>
<p className="text-slate-400">Redirecting to sign in...</p>
</div>
</div>
);
}
return (
<div className="min-h-screen bg-gradient-to-br from-slate-900 to-slate-800 flex items-center justify-center p-4">
<div className="max-w-md w-full bg-slate-800/50 rounded-lg p-8 border border-slate-700">
@@ -78,17 +48,17 @@ export default function SignUp() {
</div>
)}
<form onSubmit={handleSubmit} className="space-y-4">
<form onSubmit={handleSignUp} className="space-y-4">
<div>
<label className="block text-sm font-medium text-slate-300 mb-2">
Name
</label>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData({...formData, name: e.target.value})}
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full bg-slate-700/50 border border-slate-600 rounded-lg px-4 py-3 text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="John Doe"
placeholder="Your name"
required
/>
</div>
@@ -99,8 +69,8 @@ export default function SignUp() {
</label>
<input
type="email"
value={formData.email}
onChange={(e) => setFormData({...formData, email: e.target.value})}
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full bg-slate-700/50 border border-slate-600 rounded-lg px-4 py-3 text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="you@example.com"
required
@@ -113,23 +83,8 @@ export default function SignUp() {
</label>
<input
type="password"
value={formData.password}
onChange={(e) => setFormData({...formData, password: e.target.value})}
className="w-full bg-slate-700/50 border border-slate-600 rounded-lg px-4 py-3 text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="•••••••••"
required
minLength={8}
/>
</div>
<div>
<label className="block text-sm font-medium text-slate-300 mb-2">
Confirm Password
</label>
<input
type="password"
value={formData.confirmPassword}
onChange={(e) => setFormData({...formData, confirmPassword: e.target.value})}
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full bg-slate-700/50 border border-slate-600 rounded-lg px-4 py-3 text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="•••••••••"
required
@@ -142,11 +97,11 @@ export default function SignUp() {
disabled={loading}
className="w-full bg-gradient-to-r from-blue-500 to-purple-500 hover:from-blue-600 hover:to-purple-600 text-white font-semibold py-3 rounded-lg transition disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? 'Creating Account...' : 'Create Account'}
{loading ? 'Creating account...' : 'Sign Up'}
</button>
</form>
<p className="mt-6 text-center text-sm text-slate-400">
<p className="mt-8 text-center text-sm text-slate-400">
Already have an account?{' '}
<a href="/auth/signin" className="text-blue-400 hover:text-blue-300">
Sign in

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;
}

View File

@@ -0,0 +1,7 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'
});
export const { signIn, signUp, signOut, useSession } = authClient;