"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 SignInContent() { const router = useRouter(); const searchParams = useSearchParams(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(""); 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) { // User is already authenticated, redirect to dashboard 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(); }, []); const handleSignIn = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setMessage(""); try { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }), }); const data = await res.json(); if (!data.ok) { setMessage(data.message || "Sign in failed"); setLoading(false); return; } // Redirect based on user role or to specified redirect URL const userRole = data.user?.role; if (userRole === "ADMIN") { window.location.href = "/admin"; } else { window.location.href = redirectUrl; } } catch (err: any) { setMessage(err?.message || "Sign in failed. Please try again."); setLoading(false); } }; const handleOAuthSignIn = async (provider: "google" | "github" | "facebook" | "discord") => { try { setOAuthLoading(provider); // Redirect to OAuth provider with callback URL window.location.href = `/api/auth/${provider}?redirect=${encodeURIComponent(redirectUrl)}`; } catch (err: any) { setMessage(`${provider} sign in failed. Please try again.`); setOAuthLoading(""); } }; if (checking) { return (

Loading...

); } return (
{/* Left Side - Form */}
{/* Logo/Header */}

👤 Sign In

Welcome back! Sign in to access your account.

{/* Error/Success Message */} {message && (
{message}
)} {/* Email/Password Form */}
setEmail(e.target.value)} required className="w-full px-4 py-3 rounded-lg border border-gray-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-gray-900 dark:text-white placeholder:text-gray-400 dark:placeholder:text-gray-500 focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent transition" disabled={loading} />
Forgot password?
setPassword(e.target.value)} required className="w-full px-4 py-3 rounded-lg border border-gray-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-gray-900 dark:text-white placeholder:text-gray-400 dark:placeholder:text-gray-500 focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent transition" disabled={loading} />
{/* OAuth Divider */} {Object.values(providers).some((p) => p?.enabled) && ( <>
Or continue with
{/* OAuth Buttons */}
{providers.google?.enabled && ( )} {providers.github?.enabled && ( )} {providers.facebook?.enabled && ( )} {providers.discord?.enabled && ( )}
)} {/* Sign Up Link */}

Don't have an account?{" "} Sign Up

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

Welcome Back

Access your account and explore amazing features designed for your success.

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

Loading...

}> ); }