"use client"; import { useState, useEffect, Suspense } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; interface OAuthConfig { google?: { enabled: boolean }; github?: { enabled: boolean }; facebook?: { enabled: boolean }; discord?: { enabled: boolean }; } function SignUpContent() { const router = useRouter(); const searchParams = useSearchParams(); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(""); const [passwordStrength, setPasswordStrength] = useState<{ met: boolean; missing: string[]; }>({ met: false, missing: [] }); const [providers, setProviders] = useState({}); const [oauthLoading, setOAuthLoading] = useState(""); const [checking, setChecking] = useState(true); // Get redirect URL from query params or default to dashboard const redirectUrl = searchParams.get("redirect") || "/account/webinars"; // Check if user is already authenticated useEffect(() => { const checkAuth = async () => { try { const res = await fetch("/api/auth/me"); const data = await res.json(); if (data.user) { window.location.href = redirectUrl; } } catch (err) { console.error("Failed to check auth:", err); } finally { setChecking(false); } }; checkAuth(); }, [redirectUrl]); // Load OAuth provider configuration useEffect(() => { const loadProviders = async () => { try { const res = await fetch("/api/public/app-setup"); const data = await res.json(); if (data.setup?.data?.oauth) { setProviders(data.setup.data.oauth); } } catch (err) { console.error("Failed to load OAuth providers:", err); } }; loadProviders(); }, []); // Validate password strength useEffect(() => { const missing: string[] = []; if (password.length < 8 || password.length > 20) missing.push("8-20 characters"); if (!/[A-Z]/.test(password)) missing.push("uppercase letter"); if (!/\d/.test(password)) missing.push("number"); if (/\s/.test(password)) missing.push("no spaces"); setPasswordStrength({ met: missing.length === 0, missing }); }, [password]); const handleSignUp = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setMessage(""); try { if (!firstName.trim() || !lastName.trim()) { setMessage("First and last name are required"); setLoading(false); return; } if (password !== confirmPassword) { setMessage("Passwords do not match"); setLoading(false); return; } if (!passwordStrength.met) { setMessage(`Password must contain: ${passwordStrength.missing.join(", ")}`); setLoading(false); return; } const res = await fetch("/api/auth/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ firstName, lastName, email, password, confirmPassword, }), }); const data = await res.json(); if (!data.ok) { setMessage(data.message || "Sign up failed"); setLoading(false); return; } // Redirect to dashboard after successful signup setMessage("✅ Account created! Redirecting..."); setTimeout(() => { window.location.href = redirectUrl; }, 1500); } catch (err: any) { setMessage(err?.message || "Sign up failed. Please try again."); setLoading(false); } }; const handleOAuthSignUp = async (provider: "google" | "github" | "facebook" | "discord") => { try { setOAuthLoading(provider); window.location.href = `/api/auth/${provider}?redirect=${encodeURIComponent(redirectUrl)}`; } catch (err: any) { setMessage(`${provider} sign up failed. Please try again.`); setOAuthLoading(""); } }; return ( <> {/* Loading State */} {checking && (

Loading...

)} {/* Main Content */} {!checking && (
{/* Left Side - Form */}
{/* Logo/Header */}

Start your journey

🚀 Sign Up

Create an account and join our community.

{/* Error/Success Message */} {message && (
{message}
)} {/* Sign Up Form */}
setFirstName(e.target.value)} className="w-full px-4 py-2 border border-gray-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-gray-900 dark:text-white focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/30 dark:focus:border-blue-500" required />
setLastName(e.target.value)} className="w-full px-4 py-2 border border-gray-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-gray-900 dark:text-white focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/30 dark:focus:border-blue-500" required />
setEmail(e.target.value)} className="w-full px-4 py-2 border border-gray-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-gray-900 dark:text-white focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/30 dark:focus:border-blue-500" required />
setPassword(e.target.value)} className="w-full px-4 py-2 border border-gray-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-gray-900 dark:text-white focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/30 dark:focus:border-blue-500" required /> {password && (

{passwordStrength.met ? "✅ Password is strong" : `❌ Missing: ${passwordStrength.missing.join(", ")}`}

)}
setConfirmPassword(e.target.value)} className="w-full px-4 py-2 border border-gray-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-800 text-gray-900 dark:text-white focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/30 dark:focus:border-blue-500" required />
{/* OAuth Divider */} {Object.values(providers).some((p) => p?.enabled) && ( <>
Or sign up with
{/* OAuth Buttons */}
{providers.google?.enabled && ( )} {providers.github?.enabled && ( )} {providers.facebook?.enabled && ( )} {providers.discord?.enabled && ( )}
)} {/* Sign In Link */}

Already have an account?{" "} Sign In

{/* Right Side - Background Image */}
🌟

Join the Community

Sign up today and unlock access to exclusive features and content.

)} ); } export default function SignUpPage() { return (

Loading...

}> ); }