"use client" import { useEffect, useRef, useState } from "react" import { useRouter } from "next/navigation" import { useAuth } from "@/context/auth-context" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Separator } from "@/components/ui/separator" import { Wallet, Mail, Lock, Loader2, CheckCircle2 } from "lucide-react" import { toast } from "react-hot-toast" import Link from "next/link" import { MetaMaskEmailModal } from "@/components/metamask-email-modal" export default function SignupPage() { const { user, walletConnected, walletAddress, isLoadingAuth, authMethod, token, showMetaMaskEmailModal, setShowMetaMaskEmailModal, connectWallet, signupWithEmail } = useAuth() const router = useRouter() const hasRedirected = useRef(false) const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [username, setUsername] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) useEffect(() => { // If already authenticated, redirect to dashboard if ((walletConnected && walletAddress && user) || (user && authMethod === "email")) { if (!hasRedirected.current) { hasRedirected.current = true router.replace("/dashboard") } } }, [user, walletConnected, walletAddress, authMethod, router]) const handleSignup = async (e: React.FormEvent) => { e.preventDefault() if (!email.trim() || !password.trim() || !confirmPassword.trim()) { toast.error("Please fill in all fields") return } if (password !== confirmPassword) { toast.error("Passwords do not match") return } if (password.length < 6) { toast.error("Password must be at least 6 characters") return } setIsSubmitting(true) try { const success = await signupWithEmail(email, password, username || email.split("@")[0]) if (success) { // Auth context handles everything including token storage // Redirect will be handled by the useEffect setTimeout(() => { router.replace("/dashboard") }, 500) } } catch (error: any) { console.error("Signup error:", error) toast.error(error.message || "Signup failed") } finally { setIsSubmitting(false) } } const handleMetaMaskSignup = async () => { try { await connectWallet() toast.success("Connected with MetaMask!") setTimeout(() => { if (walletConnected && walletAddress && user) { router.replace("/dashboard") } }, 500) } catch (error: any) { console.error("MetaMask connection failed:", error) toast.error("MetaMask connection failed") } } return (
Join OpenLearnX

Create your account to start learning

{/* MetaMask Signup */}

Get blockchain features and Web3 verification

{/* Email Signup Form */}
setUsername(e.target.value)} placeholder="Choose a username" disabled={isSubmitting || isLoadingAuth} />
setEmail(e.target.value)} placeholder="Enter your email" disabled={isSubmitting || isLoadingAuth} required />
setPassword(e.target.value)} placeholder="At least 6 characters" disabled={isSubmitting || isLoadingAuth} required />

Minimum 6 characters

setConfirmPassword(e.target.value)} placeholder="Confirm your password" disabled={isSubmitting || isLoadingAuth} required />

Already have an account?{" "} Sign In

{/* MetaMask Email Modal */} {token && walletAddress && ( { setShowMetaMaskEmailModal(false) toast.success("Profile setup complete!") }} onCancel={() => { setShowMetaMaskEmailModal(false) // User can always add email later from dashboard }} /> )}
) }