feat: add duplicate email detection via auth hook with fallback
- Add before-user-created hook function for server-side duplicate email detection - Implement fallback detection using identities array check (Supabase limitation) - Update signUpWithPassword to detect duplicate emails via hook errors or empty identities - Add error handling in login page to show duplicate email errors and auto-switch to login mode - Add auth hook setup documentation and test scripts - Add password reset and update password flows - Add email templates for signup confirmation and password reset
This commit is contained in:
@@ -18,9 +18,18 @@ export async function GET(request: NextRequest) {
|
||||
const supabase = await createClient()
|
||||
|
||||
// Exchange code for session
|
||||
const { error } = await supabase.auth.exchangeCodeForSession(code)
|
||||
const { data, error } = await supabase.auth.exchangeCodeForSession(code)
|
||||
|
||||
if (!error && data.user) {
|
||||
// Check if new user (first login)
|
||||
const isNewUser = new Date(data.user.created_at).getTime() ===
|
||||
new Date(data.user.last_sign_in_at || '').getTime()
|
||||
|
||||
if (isNewUser) {
|
||||
// Redirect new users to set-password page
|
||||
return NextResponse.redirect(new URL('/set-password', request.url))
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
// Redirect to the specified next URL or default to /clients
|
||||
return NextResponse.redirect(new URL(next, request.url))
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { loginWithMagicLink, loginWithPassword } from '@/lib/auth/client'
|
||||
import { Brain, Zap, Target, FileText, Clock, TrendingDown } from 'lucide-react'
|
||||
import { loginWithPassword, signUpWithPassword } from '@/lib/auth/client'
|
||||
import { Brain, Zap, Target, Clock } from 'lucide-react'
|
||||
import { BentoGrid, BentoCard } from '@/components/ui/bento-grid'
|
||||
import Link from 'next/link'
|
||||
|
||||
// Bento grid features with different sizes for visual interest
|
||||
const bentoFeatures = [
|
||||
@@ -56,70 +57,106 @@ const bentoFeatures = [
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter()
|
||||
const [mode, setMode] = useState<'login' | 'signup'>('login')
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [confirmPassword, setConfirmPassword] = useState('') // Only for signup
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [message, setMessage] = useState<{
|
||||
type: 'success' | 'error'
|
||||
text: string
|
||||
} | null>(null)
|
||||
const [showDemoLogin, setShowDemoLogin] = useState(false)
|
||||
|
||||
// Magic link login
|
||||
const handleMagicLinkLogin = async (e: React.FormEvent) => {
|
||||
// Handle form submission
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setMessage(null)
|
||||
|
||||
try {
|
||||
const result = await loginWithMagicLink(email)
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: result.message
|
||||
})
|
||||
setEmail('')
|
||||
} catch (error: any) {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: error.message || 'Er ging iets mis. Probeer opnieuw.'
|
||||
})
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
if (mode === 'signup') {
|
||||
// Signup Flow
|
||||
if (password !== confirmPassword) {
|
||||
throw new Error('Wachtwoorden komen niet overeen')
|
||||
}
|
||||
|
||||
// Sign up
|
||||
const result = await signUpWithPassword(email, password)
|
||||
|
||||
// Check if user got a session immediately (email confirmation disabled)
|
||||
if (result.session) {
|
||||
// User is logged in immediately
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: 'Account aangemaakt! Je wordt doorgestuurd...'
|
||||
})
|
||||
setTimeout(() => {
|
||||
router.push('/epd/clients')
|
||||
}, 1000)
|
||||
} else {
|
||||
// Email confirmation required - user needs to check inbox
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: 'Check je inbox voor een verificatie link om je account te activeren.'
|
||||
})
|
||||
}
|
||||
|
||||
} else {
|
||||
// Login Flow
|
||||
await loginWithPassword(email, password)
|
||||
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: 'Ingelogd! Redirect naar EPD...'
|
||||
})
|
||||
|
||||
// Password login (demo accounts)
|
||||
const handlePasswordLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setMessage(null)
|
||||
|
||||
try {
|
||||
await loginWithPassword(email, password)
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: 'Ingelogd! Redirect naar EPD...'
|
||||
})
|
||||
|
||||
// Redirect to EPD
|
||||
setTimeout(() => {
|
||||
router.push('/epd/clients')
|
||||
}, 1000)
|
||||
} catch (error: any) {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: error.message || 'Ongeldige credentials.'
|
||||
})
|
||||
} finally {
|
||||
// Redirect to EPD
|
||||
setTimeout(() => {
|
||||
router.push('/epd/clients')
|
||||
}, 1000)
|
||||
}
|
||||
} catch (error: any) {
|
||||
// Check for duplicate email error with auto-login
|
||||
if (error.code === 'user_already_registered' && error.data?.session) {
|
||||
// User was auto-logged in (client-side detection succeeded)
|
||||
setMessage({
|
||||
type: 'success',
|
||||
text: 'Dit emailadres bestaat al. Je bent nu ingelogd!'
|
||||
})
|
||||
setTimeout(() => {
|
||||
router.push('/epd/clients')
|
||||
}, 1000)
|
||||
}
|
||||
// Check for duplicate email error (from Auth Hook or client-side)
|
||||
else if (error.code === 'user_already_registered') {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: error.message || 'Dit emailadres is al geregistreerd. Probeer in te loggen.'
|
||||
})
|
||||
// Switch to login mode after 2 seconds
|
||||
setTimeout(() => {
|
||||
setMode('login')
|
||||
setPassword('')
|
||||
setConfirmPassword('')
|
||||
}, 2000)
|
||||
}
|
||||
// Generic error handling
|
||||
else {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: error.message || 'Er ging iets mis. Probeer opnieuw.'
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Quick demo login
|
||||
const handleQuickDemoLogin = async () => {
|
||||
setMode('login')
|
||||
setEmail('demo@mini-ecd.demo')
|
||||
setPassword('Demo2024!')
|
||||
setShowDemoLogin(true)
|
||||
|
||||
// Auto-submit
|
||||
setLoading(true)
|
||||
@@ -187,19 +224,46 @@ export default function LoginPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right Side - Login Form (40%) */}
|
||||
{/* Right Side - Login/Signup Form (40%) */}
|
||||
<div className="md:w-2/5 bg-white p-8 md:p-12 flex flex-col justify-center">
|
||||
<div className="w-full max-w-md mx-auto">
|
||||
{/* Header */}
|
||||
<div className="text-center mb-8">
|
||||
<h2 className="text-2xl md:text-3xl font-bold text-slate-900 mb-2">
|
||||
Login
|
||||
{mode === 'login' ? 'Welkom terug' : 'Maak een account'}
|
||||
</h2>
|
||||
<p className="text-slate-600">
|
||||
Toegang tot EPD prototype
|
||||
{mode === 'login'
|
||||
? 'Log in om toegang te krijgen tot het EPD'
|
||||
: 'Start vandaag nog met snellere rapportages'
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Mode Toggle */}
|
||||
<div className="flex bg-slate-100 p-1 rounded-lg mb-6">
|
||||
<button
|
||||
className={`flex-1 py-2 text-sm font-medium rounded-md transition-all ${
|
||||
mode === 'login'
|
||||
? 'bg-white text-slate-900 shadow-sm'
|
||||
: 'text-slate-500 hover:text-slate-900'
|
||||
}`}
|
||||
onClick={() => setMode('login')}
|
||||
>
|
||||
Inloggen
|
||||
</button>
|
||||
<button
|
||||
className={`flex-1 py-2 text-sm font-medium rounded-md transition-all ${
|
||||
mode === 'signup'
|
||||
? 'bg-white text-slate-900 shadow-sm'
|
||||
: 'text-slate-500 hover:text-slate-900'
|
||||
}`}
|
||||
onClick={() => setMode('signup')}
|
||||
>
|
||||
Registreren
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Message Display */}
|
||||
{message && (
|
||||
<div
|
||||
@@ -213,149 +277,106 @@ export default function LoginPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Magic Link Login */}
|
||||
{!showDemoLogin && (
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-slate-900 mb-4">
|
||||
📧 Login met Magic Link
|
||||
</h3>
|
||||
|
||||
<form onSubmit={handleMagicLinkLogin} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="email"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
>
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="jouw@email.nl"
|
||||
required
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
<p className="mt-1 text-xs text-slate-500">
|
||||
Nieuw? Account wordt automatisch aangemaakt!
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? 'Verzenden...' : 'Stuur Magic Link'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Divider */}
|
||||
<div className="relative my-6">
|
||||
<div className="absolute inset-0 flex items-center">
|
||||
<div className="w-full border-t border-slate-300" />
|
||||
</div>
|
||||
<div className="relative flex justify-center text-sm">
|
||||
<span className="px-2 bg-white text-slate-500">of</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Demo Account Toggle */}
|
||||
<button
|
||||
onClick={() => setShowDemoLogin(true)}
|
||||
className="w-full bg-slate-100 hover:bg-slate-200 text-slate-700 font-medium py-2.5 px-4 rounded-lg transition-colors"
|
||||
<label
|
||||
htmlFor="email"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
>
|
||||
🎯 Login met Demo Account
|
||||
</button>
|
||||
|
||||
{/* Quick Demo Button */}
|
||||
<button
|
||||
onClick={handleQuickDemoLogin}
|
||||
disabled={loading}
|
||||
className="mt-3 w-full bg-amber-50 hover:bg-amber-100 text-amber-800 text-sm font-medium py-2 px-4 rounded-lg border border-amber-200 transition-colors disabled:opacity-50"
|
||||
>
|
||||
⚡ Snelle Demo Login
|
||||
</button>
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="jouw@email.nl"
|
||||
required
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Demo Password Login */}
|
||||
{showDemoLogin && (
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-slate-900">
|
||||
🎯 Demo Account Login
|
||||
</h3>
|
||||
<button
|
||||
onClick={() => {
|
||||
setShowDemoLogin(false)
|
||||
setPassword('')
|
||||
}}
|
||||
className="text-sm text-slate-600 hover:text-slate-900"
|
||||
>
|
||||
← Terug
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Demo Credentials Info */}
|
||||
<div className="bg-amber-50 border border-amber-200 rounded-lg p-4 mb-4">
|
||||
<p className="text-sm font-medium text-amber-800 mb-2">
|
||||
📋 Demo Credentials:
|
||||
</p>
|
||||
<div className="text-xs text-amber-700 font-mono space-y-1">
|
||||
<p>Email: demo@mini-ecd.demo</p>
|
||||
<p>Password: Demo2024!</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handlePasswordLogin} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="demo-email"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
>
|
||||
Wachtwoord
|
||||
</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.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
{mode === 'login' && (
|
||||
<div className="mt-1 text-right">
|
||||
<Link
|
||||
href="/reset-password"
|
||||
className="text-xs text-slate-500 hover:text-teal-600"
|
||||
>
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
id="demo-email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="demo@mini-ecd.demo"
|
||||
required
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
Wachtwoord vergeten?
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? 'Inloggen...' : 'Login'}
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{mode === 'signup' && (
|
||||
<div>
|
||||
<label
|
||||
htmlFor="confirmPassword"
|
||||
className="block text-sm font-medium text-slate-700 mb-1"
|
||||
>
|
||||
Bevestig Wachtwoord
|
||||
</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.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-slate-400 focus:border-slate-400"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading
|
||||
? 'Laden...'
|
||||
: mode === 'login' ? 'Inloggen' : 'Registreren'
|
||||
}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Divider */}
|
||||
<div className="relative my-6">
|
||||
<div className="absolute inset-0 flex items-center">
|
||||
<div className="w-full border-t border-slate-300" />
|
||||
</div>
|
||||
<div className="relative flex justify-center text-sm">
|
||||
<span className="px-2 bg-white text-slate-500">of</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Quick Demo Button */}
|
||||
<button
|
||||
onClick={handleQuickDemoLogin}
|
||||
disabled={loading}
|
||||
className="w-full bg-amber-50 hover:bg-amber-100 text-amber-800 text-sm font-medium py-2 px-4 rounded-lg border border-amber-200 transition-colors disabled:opacity-50 flex items-center justify-center gap-2"
|
||||
>
|
||||
<span>⚡</span>
|
||||
Demo Account Proberen
|
||||
</button>
|
||||
|
||||
{/* Footer */}
|
||||
<p className="text-center text-xs text-slate-500 mt-8">
|
||||
|
||||
105
app/reset-password/page.tsx
Normal file
105
app/reset-password/page.tsx
Normal file
@@ -0,0 +1,105 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { resetPasswordForEmail } from '@/lib/auth/client'
|
||||
import Link from 'next/link'
|
||||
|
||||
export default function ResetPasswordPage() {
|
||||
const [email, setEmail] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [sent, setSent] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
async function handleResetRequest(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setError('')
|
||||
|
||||
try {
|
||||
await resetPasswordForEmail(email)
|
||||
setSent(true)
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Er ging iets mis')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (sent) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
|
||||
<div className="max-w-md w-full bg-white p-8 rounded-lg shadow-sm text-center space-y-4">
|
||||
<div className="text-5xl mb-4">✉️</div>
|
||||
<h1 className="text-2xl font-bold text-slate-900">Email Verstuurd!</h1>
|
||||
<p className="text-slate-600">
|
||||
Check je inbox voor de reset link. De link is 1 uur geldig.
|
||||
</p>
|
||||
<p className="text-sm text-slate-500">
|
||||
Niet ontvangen? Check je spam folder.
|
||||
</p>
|
||||
<Link
|
||||
href="/login"
|
||||
className="inline-block mt-4 text-teal-600 hover:text-teal-700 font-medium"
|
||||
>
|
||||
← Terug naar login
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
|
||||
<div className="max-w-md w-full bg-white p-8 rounded-lg shadow-sm">
|
||||
<div className="mb-6">
|
||||
<h1 className="text-2xl font-bold text-slate-900 mb-2">
|
||||
Wachtwoord Vergeten?
|
||||
</h1>
|
||||
<p className="text-slate-600">
|
||||
Geen probleem! Vul je email in en we sturen je een reset link.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-800 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleResetRequest} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="jouw@email.nl"
|
||||
required
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-teal-500 focus:border-teal-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors disabled:opacity-50"
|
||||
>
|
||||
{loading ? 'Verzenden...' : 'Stuur Reset Link'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div className="mt-6 text-center">
|
||||
<Link
|
||||
href="/login"
|
||||
className="text-sm text-slate-600 hover:text-slate-900"
|
||||
>
|
||||
← Terug naar login
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
108
app/set-password/page.tsx
Normal file
108
app/set-password/page.tsx
Normal file
@@ -0,0 +1,108 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { updateUserPassword } from '@/lib/auth/client'
|
||||
|
||||
export default function SetPasswordPage() {
|
||||
const router = useRouter()
|
||||
const [password, setPassword] = useState('')
|
||||
const [confirmPassword, setConfirmPassword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
async function handleSetPassword(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
setError('Wachtwoorden komen niet overeen')
|
||||
return
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
setError('Wachtwoord minimaal 8 tekens')
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
|
||||
try {
|
||||
await updateUserPassword(password)
|
||||
router.push('/epd/clients')
|
||||
} catch (err: any) {
|
||||
setError(err.message)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
function handleSkip() {
|
||||
router.push('/epd/clients')
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
|
||||
<div className="max-w-md w-full bg-white p-8 rounded-lg shadow-sm">
|
||||
<h1 className="text-2xl font-bold text-slate-900 mb-2">
|
||||
Stel een Wachtwoord In (Optioneel)
|
||||
</h1>
|
||||
<p className="text-slate-600 mb-6">
|
||||
Met een wachtwoord kun je sneller inloggen zonder magic link.
|
||||
</p>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-800 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSetPassword} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">
|
||||
Wachtwoord
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="Min. 8 tekens"
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-teal-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">
|
||||
Bevestig Wachtwoord
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-teal-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg disabled:opacity-50"
|
||||
>
|
||||
{loading ? 'Instellen...' : 'Wachtwoord Instellen'}
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSkip}
|
||||
className="w-full text-slate-600 hover:text-slate-900 text-sm font-medium py-2"
|
||||
>
|
||||
Sla over (blijf magic link gebruiken)
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
118
app/update-password/page.tsx
Normal file
118
app/update-password/page.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { updateUserPassword } from '@/lib/auth/client'
|
||||
|
||||
export default function UpdatePasswordPage() {
|
||||
const router = useRouter()
|
||||
const [password, setPassword] = useState('')
|
||||
const [confirmPassword, setConfirmPassword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
const [success, setSuccess] = useState(false)
|
||||
|
||||
async function handleUpdatePassword(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
|
||||
// Validation
|
||||
if (password !== confirmPassword) {
|
||||
setError('Wachtwoorden komen niet overeen')
|
||||
return
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
setError('Wachtwoord moet minimaal 8 tekens zijn')
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
|
||||
try {
|
||||
await updateUserPassword(password)
|
||||
setSuccess(true)
|
||||
setTimeout(() => router.push('/login'), 2000)
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Er ging iets mis')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (success) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
|
||||
<div className="max-w-md w-full bg-white p-8 rounded-lg shadow-sm text-center space-y-4">
|
||||
<div className="text-5xl mb-4">✅</div>
|
||||
<h1 className="text-2xl font-bold text-slate-900">Wachtwoord Gewijzigd!</h1>
|
||||
<p className="text-slate-600">
|
||||
Je wordt doorgestuurd naar de login pagina...
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
|
||||
<div className="max-w-md w-full bg-white p-8 rounded-lg shadow-sm">
|
||||
<div className="mb-6">
|
||||
<h1 className="text-2xl font-bold text-slate-900 mb-2">
|
||||
Nieuw Wachtwoord Instellen
|
||||
</h1>
|
||||
<p className="text-slate-600">
|
||||
Kies een sterk wachtwoord (minimaal 8 tekens).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-800 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleUpdatePassword} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">
|
||||
Nieuw Wachtwoord
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-teal-500 focus:border-teal-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">
|
||||
Bevestig Wachtwoord
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
required
|
||||
minLength={8}
|
||||
className="w-full px-4 py-2.5 border border-slate-300 rounded-lg focus:ring-2 focus:ring-teal-500 focus:border-teal-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-teal-600 hover:bg-teal-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors disabled:opacity-50"
|
||||
>
|
||||
{loading ? 'Wachtwoord Wijzigen...' : 'Wachtwoord Wijzigen'}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user