"use client" import Image from "next/image" import { useRouter, useSearchParams } from "next/navigation" import { startTransition, useEffect, useState } from "react" import { toast } from "sonner" import { useAuth } from "@/components/auth-provider" import { loginWithPassword } from "@/lib/api/auth" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Field, FieldGroup, FieldLabel, } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { KeyRoundIcon } from "lucide-react" function detectWxWorkEnvironment() { if (typeof navigator === "undefined") { return false } const userAgent = navigator.userAgent.toLowerCase() return userAgent.includes("wxwork") } export function LoginForm({ className, ...props }: React.ComponentProps<"form">) { const router = useRouter() const searchParams = useSearchParams() const { session } = useAuth() const [isPending, setIsPending] = useState(false) const [isWxWorkEnv, setIsWxWorkEnv] = useState(false) const nextPath = searchParams.get("next") const wxworkError = searchParams.get("wxworkError") const oidcError = searchParams.get("oidcError") const redirectPath = nextPath && nextPath.startsWith("/") ? nextPath : "/dashboard" useEffect(() => { if (session) { router.replace(redirectPath) } }, [redirectPath, router, session]) useEffect(() => { if (wxworkError) { toast.error(wxworkError) } }, [wxworkError]) useEffect(() => { if (oidcError) { toast.error(oidcError) } }, [oidcError]) useEffect(() => { setIsWxWorkEnv(detectWxWorkEnvironment()) }, []) async function handleSubmit(event: React.FormEvent) { event.preventDefault() const formData = new FormData(event.currentTarget) const username = formData.get("username")?.toString().trim() ?? "" const password = formData.get("password")?.toString() ?? "" setIsPending(true) try { await loginWithPassword({ username, password }) toast.success("登录成功,正在进入系统") startTransition(() => { router.push(redirectPath) }) } catch (error) { toast.error(error instanceof Error ? error.message : "登录失败") } finally { setIsPending(false) } } return (
贝壳AI

欢迎使用贝壳 AI 客服平台

用户名
密码
) }