created simple auth system using betterauth
This commit is contained in:
12
src/app/admin/page.tsx
Normal file
12
src/app/admin/page.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import AuthRedirect from "@/components/AuthRedirect";
|
||||
|
||||
export default function AdminPage() {
|
||||
|
||||
return (
|
||||
<div>
|
||||
<AuthRedirect />
|
||||
<h1>Admin Dashboard</h1>
|
||||
<p>Welcome to the admin dashboard. Here you can manage the application.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
3
src/app/api/auth/[...all]/route.ts
Normal file
3
src/app/api/auth/[...all]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { auth } from "@/lib/auth"; // path to your auth file
|
||||
import { toNextJsHandler } from "better-auth/next-js";
|
||||
export const { POST, GET } = toNextJsHandler(auth);
|
||||
@@ -1,3 +1,7 @@
|
||||
import './globals.css';
|
||||
import { Header } from '@/components/Header';
|
||||
import { Footer } from '@/components/Footer';
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
@@ -5,7 +9,13 @@ export default function RootLayout({
|
||||
}) {
|
||||
return (
|
||||
<html>
|
||||
<body>{children}</body>
|
||||
<body className="flex flex-col min-h-screen">
|
||||
<Header />
|
||||
<main className="flex-grow mt-4 mb-4 mr-8 ml-8">
|
||||
{children}
|
||||
</main>
|
||||
<Footer />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
"use client";
|
||||
|
||||
export default function Page() {
|
||||
return <h1>Hello Next.js!</h1>
|
||||
}
|
||||
5
src/app/user/sign-in/page.tsx
Normal file
5
src/app/user/sign-in/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { AuthForm } from '@/components/AuthForm';
|
||||
|
||||
export default function SignInPage() {
|
||||
return <AuthForm />;
|
||||
}
|
||||
235
src/components/AuthForm.tsx
Normal file
235
src/components/AuthForm.tsx
Normal file
@@ -0,0 +1,235 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { authClient } from '@/lib/auth-client';
|
||||
|
||||
type AuthMode = 'signin' | 'signup';
|
||||
|
||||
export function AuthForm() {
|
||||
const [mode, setMode] = useState<AuthMode>('signin');
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [confirmPassword, setConfirmPassword] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
if (mode === 'signin') {
|
||||
const response = await authClient.signIn.email({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
if (response.error) {
|
||||
setError(response.error.message || 'Authentifizierung fehlgeschlagen');
|
||||
}
|
||||
} else {
|
||||
if (password !== confirmPassword) {
|
||||
setError('Die Passwörter stimmen nicht überein');
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
const response = await authClient.signUp.email({
|
||||
email,
|
||||
name: email.split('@')[0],
|
||||
password,
|
||||
});
|
||||
if (response.error) {
|
||||
setError(response.error.message || 'Registrierung fehlgeschlagen');
|
||||
}
|
||||
}
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Ein Fehler ist aufgetreten');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<div className="w-full max-w-md">
|
||||
{/* Card */}
|
||||
<div className="bg-white rounded-lg shadow-lg overflow-hidden border border-gray-200">
|
||||
{/* Header */}
|
||||
<div className="bg-gradient-to-r from-blue-600 to-blue-700 px-6 py-8">
|
||||
<h1 className="text-3xl font-bold text-white text-center">
|
||||
Dialektannotation
|
||||
</h1>
|
||||
<p className="text-blue-100 text-center mt-2">
|
||||
TTS-Plattform
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="flex border-b border-gray-200">
|
||||
<button
|
||||
onClick={() => {
|
||||
setMode('signin');
|
||||
setError('');
|
||||
}}
|
||||
className={`flex-1 py-4 px-6 font-semibold text-center transition-colors ${
|
||||
mode === 'signin'
|
||||
? 'bg-blue-50 text-blue-600 border-b-2 border-blue-600'
|
||||
: 'text-gray-600 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
Anmelden
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
setMode('signup');
|
||||
setError('');
|
||||
}}
|
||||
className={`flex-1 py-4 px-6 font-semibold text-center transition-colors ${
|
||||
mode === 'signup'
|
||||
? 'bg-blue-50 text-blue-600 border-b-2 border-blue-600'
|
||||
: 'text-gray-600 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
Registrieren
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Form */}
|
||||
<form onSubmit={handleSubmit} className="p-6 space-y-4">
|
||||
{/* Error Message */}
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-300 text-red-700 px-4 py-3 rounded-lg flex items-start gap-3">
|
||||
<svg
|
||||
className="w-5 h-5 mt-0.5 flex-shrink-0"
|
||||
fill="currentColor"
|
||||
viewBox="0 0 20 20"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
<span className="text-sm">{error}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Email Input */}
|
||||
<div>
|
||||
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
E-Mail-Adresse
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="you@example.com"
|
||||
required
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Password Input */}
|
||||
<div>
|
||||
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Passwort
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors"
|
||||
/>
|
||||
{mode === 'signup' && (
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
Mindestens 8 Zeichen
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Confirm Password Input (Sign Up Only) */}
|
||||
{mode === 'signup' && (
|
||||
<div>
|
||||
<label htmlFor="confirmPassword" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Passwort bestätigen
|
||||
</label>
|
||||
<input
|
||||
id="confirmPassword"
|
||||
type="password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Submit Button */}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-blue-600 hover:bg-blue-700 disabled:bg-gray-400 text-white font-semibold py-2 px-4 rounded-lg transition-colors mt-6 flex items-center justify-center gap-2"
|
||||
>
|
||||
{loading && (
|
||||
<svg
|
||||
className="w-5 h-5 animate-spin"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M12 4v16m8-8H4"
|
||||
/>
|
||||
</svg>
|
||||
)}
|
||||
{loading ? 'Wird geladen...' : mode === 'signin' ? 'Anmelden' : 'Konto erstellen'}
|
||||
</button>
|
||||
|
||||
{/* Info Text */}
|
||||
<p className="text-center text-sm text-gray-600 mt-6">
|
||||
{mode === 'signin' ? (
|
||||
<>
|
||||
Sie haben noch kein Konto?{' '}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMode('signup');
|
||||
setError('');
|
||||
}}
|
||||
className="text-blue-600 hover:text-blue-700 font-semibold"
|
||||
>
|
||||
Registrieren
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
Sie haben bereits ein Konto?{' '}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMode('signin');
|
||||
setError('');
|
||||
}}
|
||||
className="text-blue-600 hover:text-blue-700 font-semibold"
|
||||
>
|
||||
Anmelden
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
16
src/components/AuthRedirect.tsx
Normal file
16
src/components/AuthRedirect.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
"use client";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { redirect } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function AuthRedirect() {
|
||||
const { data: session, isPending: loading } = authClient.useSession();
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading && !session) {
|
||||
redirect("/user/sign-in");
|
||||
}
|
||||
}, [loading, session]);
|
||||
|
||||
return <div></div>
|
||||
}
|
||||
35
src/components/Footer.tsx
Normal file
35
src/components/Footer.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
export function Footer() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
return (
|
||||
<footer className="bg-gray-800 text-gray-300 mt-12">
|
||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-8 mb-8">
|
||||
<div>
|
||||
<h3 className="text-white font-semibold mb-4">About</h3>
|
||||
<p className="text-sm">
|
||||
TTS Dialect Annotation Platform for Swiss German dialects.
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-white font-semibold mb-4">Quick Links</h3>
|
||||
<ul className="space-y-2 text-sm">
|
||||
<li><a href="/" className="hover:text-white transition-colors">Home</a></li>
|
||||
<li><a href="#" className="hover:text-white transition-colors">Documentation</a></li>
|
||||
<li><a href="#" className="hover:text-white transition-colors">Support</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-white font-semibold mb-4">Contact</h3>
|
||||
<p className="text-sm">
|
||||
ZHAW - Zurich University of Teacher Education
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="border-t border-gray-700 pt-8 text-center text-sm">
|
||||
<p>© {currentYear} TTS Dialect Annotation Platform. All rights reserved.</p>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
}
|
||||
54
src/components/Header.tsx
Normal file
54
src/components/Header.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
'use client';
|
||||
|
||||
import { authClient } from '@/lib/auth-client';
|
||||
import Link from 'next/link';
|
||||
|
||||
export function Header() {
|
||||
const { data: session, isPending: loading } = authClient.useSession();
|
||||
|
||||
const handleSignOut = async () => {
|
||||
try {
|
||||
await authClient.signOut({});
|
||||
} catch (error) {
|
||||
console.error('Sign out failed:', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-50 bg-gradient-to-r from-blue-600 to-blue-700 text-white shadow-lg">
|
||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||
<div className="flex justify-between items-center py-5">
|
||||
<Link href="/" className="text-2xl font-bold hover:text-blue-100 transition-colors">
|
||||
TTS Dialect Annotation Platform
|
||||
</Link>
|
||||
|
||||
<nav className="flex items-center gap-6">
|
||||
{loading ? (
|
||||
<span className="text-sm text-blue-100">Loading...</span>
|
||||
) : session ? (
|
||||
<div className="flex items-center gap-6">
|
||||
<div className="text-right">
|
||||
<p className="text-xs text-blue-100 uppercase tracking-wide">Willkommen zurück</p>
|
||||
<p className="text-sm font-semibold">{session.user?.email}</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleSignOut}
|
||||
className="px-4 py-2 bg-red-600 hover:bg-red-700 active:bg-red-800 text-white font-semibold rounded-lg transition-all duration-200 transform hover:scale-105"
|
||||
>
|
||||
Abmelden
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<Link
|
||||
href="/user/sign-in"
|
||||
className="px-5 py-2 bg-green-500 hover:bg-green-600 active:bg-green-700 text-white font-semibold rounded-lg transition-all duration-200 transform hover:scale-105"
|
||||
>
|
||||
Anmelden
|
||||
</Link>
|
||||
)}
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
5
src/lib/auth-client.ts
Normal file
5
src/lib/auth-client.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createAuthClient } from "better-auth/react"
|
||||
export const authClient = createAuthClient({
|
||||
/** The base URL of the server (optional if you're using the same domain) */
|
||||
baseURL: "http://localhost:3000"
|
||||
})
|
||||
18
src/lib/auth.ts
Normal file
18
src/lib/auth.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { betterAuth } from "better-auth";
|
||||
import Database from "better-sqlite3";
|
||||
import { localization } from "better-auth-localization";
|
||||
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: new Database("./sqlite_users.db"),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
},
|
||||
plugins: [
|
||||
|
||||
localization({
|
||||
defaultLocale: "de-DE", // Default to German
|
||||
fallbackLocale: "default" // Fallback to English
|
||||
})
|
||||
]
|
||||
});
|
||||
Reference in New Issue
Block a user