"use client"
import { useRouter, useSearchParams } from "next/navigation"
import { Suspense, useEffect, useRef } from "react"
import { toast } from "sonner"
import { exchangeWxWorkTicket } from "@/lib/api/auth"
import { useI18n } from "@/i18n/provider"
export default function WxWorkLoginCallbackPage() {
return (
}>
)
}
function WxWorkLoginCallbackContent() {
const t = useI18n()
const router = useRouter()
const searchParams = useSearchParams()
const ranRef = useRef(false)
useEffect(() => {
if (ranRef.current) {
return
}
ranRef.current = true
const ticket = searchParams.get("ticket")?.trim() ?? ""
const next = searchParams.get("next")
const nextPath = next && next.startsWith("/") ? next : "/dashboard"
if (!ticket) {
toast.error(t("auth.wxworkMissingTicket"))
router.replace("/dashboard/login")
return
}
void exchangeWxWorkTicket(ticket)
.then(() => {
toast.success(t("auth.loginSuccess"))
router.replace(nextPath)
})
.catch((error) => {
toast.error(error instanceof Error ? error.message : t("auth.wxworkFailed"))
router.replace("/dashboard/login")
})
}, [router, searchParams, t])
return (
)
}
function WxWorkLoginCallbackFallback() {
const t = useI18n()
return (
{t("auth.wxworkSigningIn")}
{t("auth.checkingTicket")}
)
}