"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { createClient } from "@/lib/supabase/client"; import { formatAuthNetworkError } from "@/lib/supabase/env"; import type { PortalRole } from "@/lib/auth/roles"; import { PORTAL_ROUTES } from "@/lib/auth/roles"; import { ensureUserProfile, isPortalRoleSchemaError, resolvePortalRole, } from "@/lib/auth/resolve-portal-role"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { PasswordInput } from "@/components/ui/password-input"; import { Label } from "@/components/ui/label"; export function LoginForm({ expectedRole }: { expectedRole: PortalRole }) { const router = useRouter(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(null); try { const supabase = createClient(); const { data: authData, error: authError } = await supabase.auth.signInWithPassword({ email, password }); if (authError) { setError(authError.message); return; } const userId = authData.user?.id; if (!userId) { setError("Sign in failed"); return; } const { data: { user }, } = await supabase.auth.getUser(); if (!user) { setError("Sign in failed"); return; } const { role, profileError } = await resolvePortalRole(supabase, user); if (role !== expectedRole) { await supabase.auth.signOut(); setError( expectedRole === "manager" ? "This account is not a Team Manager. Use the League Master sign-in URL if you are an admin." : "This account is not a League Master. Use the manager sign-in page." ); return; } if (profileError && isPortalRoleSchemaError(profileError)) { setError( "Database is missing portal_role. Run: npm run db:push — then try again." ); return; } if (profileError) { await ensureUserProfile(supabase, user, expectedRole); } router.push(PORTAL_ROUTES[expectedRole]); router.refresh(); } catch (err) { setError(formatAuthNetworkError(err)); } finally { setLoading(false); } } return (
setEmail(e.target.value)} required autoComplete="email" />
setPassword(e.target.value)} required autoComplete="current-password" />
{error && (

{error}

)}

Forgot password?

); } export function LoginPageShell({ title, subtitle, children, footer, }: { title: string; subtitle: string; children: React.ReactNode; footer?: React.ReactNode; }) { return (

Yaltopia FIFA

{title}

{subtitle}

{children}
{footer &&
{footer}
}
); }