"use client"; import { FormEvent, useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { ArrowRight, ChevronLeft, Chrome, Cloud, CloudRain, Lock, Mail, Sun, } from "lucide-react"; import { getSupabaseBrowserClient, hasSupabasePublicEnv, } from "@/lib/supabase/client"; type Mode = "login" | "signup"; type LoginClientProps = { nextPath: string; }; export function LoginClient({ nextPath }: LoginClientProps) { const router = useRouter(); const [mode, setMode] = useState("login"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [errorText, setErrorText] = useState(""); const [infoText, setInfoText] = useState(""); const supabaseReady = hasSupabasePublicEnv(); useEffect(() => { if (!supabaseReady) return; const run = async () => { const supabase = getSupabaseBrowserClient(); const { data: { session }, } = await supabase.auth.getSession(); if (session?.user) { router.replace(nextPath); } }; void run(); }, [nextPath, router, supabaseReady]); const onGoogleSignIn = async () => { setErrorText(""); setInfoText(""); if (!supabaseReady) { setErrorText("Supabase 未配置,无法使用登录"); return; } setLoading(true); try { const supabase = getSupabaseBrowserClient(); const redirectTo = `${window.location.origin}/auth/callback?next=${encodeURIComponent( nextPath, )}`; const { error } = await supabase.auth.signInWithOAuth({ provider: "google", options: { redirectTo, }, }); if (error) { setErrorText(error.message); } } finally { setLoading(false); } }; const onEmailSubmit = async (event: FormEvent) => { event.preventDefault(); setErrorText(""); setInfoText(""); if (!supabaseReady) { setErrorText("Supabase 未配置,无法使用登录"); return; } if (!email.trim() || !password.trim()) { setErrorText("请输入邮箱和密码"); return; } setLoading(true); try { const supabase = getSupabaseBrowserClient(); if (mode === "login") { const { error } = await supabase.auth.signInWithPassword({ email: email.trim(), password, }); if (error) { setErrorText(error.message); return; } router.replace(nextPath); return; } const emailRedirectTo = `${window.location.origin}/auth/callback?next=${encodeURIComponent( nextPath, )}`; const { data, error } = await supabase.auth.signUp({ email: email.trim(), password, options: { emailRedirectTo, }, }); if (error) { setErrorText(error.message); return; } if (data.session?.user) { router.replace(nextPath); return; } setInfoText("注册成功,请检查邮箱并完成验证后登录。"); } finally { setLoading(false); } }; const isLogin = mode === "login"; return (

PolyWeather

探索世界每一个角落的气象细节

或使用邮箱
void onEmailSubmit(event)} className="space-y-4">
setEmail(event.target.value)} placeholder="you@example.com" className="w-full rounded-xl border border-white/10 bg-white/5 py-3.5 pl-12 pr-4 text-white placeholder:text-slate-600 transition-all focus:border-blue-500/50 focus:outline-none focus:ring-2 focus:ring-blue-500/50" />
setPassword(event.target.value)} placeholder={isLogin ? "输入密码" : "设置至少 6 位密码"} className="w-full rounded-xl border border-white/10 bg-white/5 py-3.5 pl-12 pr-4 text-white placeholder:text-slate-600 transition-all focus:border-blue-500/50 focus:outline-none focus:ring-2 focus:ring-blue-500/50" />
{errorText ?

{errorText}

: null} {infoText ?

{infoText}

: null}

{isLogin ? "登录后将为您个性化定制首页数据" : "注册即代表同意我们的服务条款"}

{!supabaseReady ? (

Supabase 未配置,无法使用登录

) : null}
实时数据 高精度预测
); }