84 lines
3.5 KiB
TypeScript
84 lines
3.5 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { signIn } from 'next-auth/react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Link2, Mail, Lock, User } from 'lucide-react'
|
|
import Link from 'next/link'
|
|
import { toast } from 'sonner'
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
export function SignupClient() {
|
|
const [name, setName] = useState('')
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const router = useRouter()
|
|
|
|
const handleSignup = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
if (password.length < 6) { toast.error('Password must be at least 6 characters'); return }
|
|
setLoading(true)
|
|
try {
|
|
const res = await fetch('/api/signup', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password, name }),
|
|
})
|
|
const data = await res.json()
|
|
if (res.ok) {
|
|
// Auto sign in
|
|
const result = await signIn('credentials', { email, password, redirect: false })
|
|
if (result?.ok) {
|
|
router.push('/pages')
|
|
} else {
|
|
toast.success('Account created! Please sign in.')
|
|
router.push('/login')
|
|
}
|
|
} else {
|
|
toast.error(data?.error ?? 'Signup failed')
|
|
}
|
|
} catch {
|
|
toast.error('Error creating account')
|
|
}
|
|
setLoading(false)
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-background p-4">
|
|
<div className="w-full max-w-sm space-y-8">
|
|
<div className="text-center">
|
|
<div className="flex justify-center mb-4">
|
|
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-primary text-primary-foreground">
|
|
<Link2 className="h-7 w-7" />
|
|
</div>
|
|
</div>
|
|
<h1 className="font-display text-3xl font-bold tracking-tight">Create Account</h1>
|
|
<p className="text-sm text-muted-foreground mt-2">Join computer to start organizing your links</p>
|
|
</div>
|
|
|
|
<form onSubmit={handleSignup} className="space-y-3">
|
|
<div className="relative">
|
|
<User className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input placeholder="Name" value={name} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setName(e.target.value)} className="pl-10" />
|
|
</div>
|
|
<div className="relative">
|
|
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input type="email" placeholder="Email" value={email} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setEmail(e.target.value)} className="pl-10" required />
|
|
</div>
|
|
<div className="relative">
|
|
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input type="password" placeholder="Password (min 8 chars)" value={password} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setPassword(e.target.value)} className="pl-10" minLength={8} required />
|
|
</div>
|
|
<Button type="submit" className="w-full" loading={loading}>Create Account</Button>
|
|
</form>
|
|
|
|
<p className="text-center text-xs text-muted-foreground">
|
|
Already have an account? <Link href="/login" className="text-primary hover:underline">Sign in</Link>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|