's'
This commit is contained in:
61
app/page.tsx
61
app/page.tsx
@@ -155,8 +155,10 @@ function removeStorage(key: string): void {
|
||||
export default function Home() {
|
||||
const [paidType, setPaidType] = useState<string | null>(null);
|
||||
const [userName, setUserName] = useState<string | null>(null);
|
||||
const [userEmail, setUserEmail] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [payPendingOrderId, setPayPendingOrderId] = useState<string | null>(null);
|
||||
const [payAuthModalOpen, setPayAuthModalOpen] = useState(false);
|
||||
|
||||
const savePaidType = useCallback((type: string | null) => {
|
||||
if (type) setStorage("paidType", type);
|
||||
@@ -281,8 +283,6 @@ export default function Home() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const [authForPayOpen, setAuthForPayOpen] = useState(false);
|
||||
|
||||
const doPay = useCallback(async () => {
|
||||
const userId = await getOrCreateUserId();
|
||||
const returnUrl = `${window.location.origin}/paid`;
|
||||
@@ -317,7 +317,12 @@ export default function Home() {
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
setAuthForPayOpen(true);
|
||||
setPayAuthModalOpen(true);
|
||||
}, [doPay]);
|
||||
|
||||
const handlePayAuthSuccess = useCallback(() => {
|
||||
setPayAuthModalOpen(false);
|
||||
doPay();
|
||||
}, [doPay]);
|
||||
|
||||
const handleVerifySuccess = useCallback(() => {
|
||||
@@ -358,24 +363,36 @@ export default function Home() {
|
||||
}
|
||||
|
||||
const init = async () => {
|
||||
await getOrCreateUserId();
|
||||
|
||||
// 本地已有 VIP 标识时优先使用,不因跨站登录态覆盖
|
||||
const localType = getStorage("paidType");
|
||||
const localUserName = getStorage("userName");
|
||||
if (localType && localUserName) {
|
||||
const localUserId = getStorage("userId");
|
||||
if (localType === "vip" && localUserName && localUserId) {
|
||||
setPaidType(localType);
|
||||
setUserName(localUserName);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// SSO: 优先检查根域 Cookie(跨站登录态,如从 digital 登录后跳转过来)
|
||||
await getOrCreateUserId();
|
||||
|
||||
const afterLocalType = getStorage("paidType");
|
||||
const afterLocalUserName = getStorage("userName");
|
||||
if (afterLocalType && afterLocalUserName) {
|
||||
setPaidType(afterLocalType);
|
||||
setUserName(afterLocalUserName);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查本站 Cookie 登录态
|
||||
try {
|
||||
const meRes = await fetch("/api/auth/me", { credentials: "include", cache: "no-store" });
|
||||
const meData = await meRes.json();
|
||||
if (meData?.user && meData?.token) {
|
||||
const { pbSaveAuth } = await import("@/app/lib/pocketbase");
|
||||
pbSaveAuth(meData.token, { id: meData.user.id, email: meData.user.email });
|
||||
setUserEmail(meData.user.email || null);
|
||||
const vipRes = await fetch("/api/vip/check", { credentials: "include", cache: "no-store" });
|
||||
const vipData = await vipRes.json();
|
||||
if (vipData?.vip) {
|
||||
@@ -429,6 +446,7 @@ export default function Home() {
|
||||
const handleLogout = useCallback(async () => {
|
||||
const { pbLogout } = await import("@/app/lib/pocketbase");
|
||||
await pbLogout();
|
||||
setUserEmail(null);
|
||||
clearAllStorage();
|
||||
}, [clearAllStorage]);
|
||||
|
||||
@@ -445,26 +463,25 @@ export default function Home() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<VipLayout isLoggedIn={isLoggedIn} userName={userName} onLogout={handleLogout}>
|
||||
<VipLayout isLoggedIn={isLoggedIn} userName={userName} userEmail={userEmail} onLogout={handleLogout}>
|
||||
{isLoggedIn ? (
|
||||
<DashboardPage userName={userName} />
|
||||
) : (
|
||||
<LandingPage
|
||||
onJoin={handlePay}
|
||||
onVerify={checkPaymentFromPB}
|
||||
onVerifyByEmail={checkVerifyByEmail}
|
||||
onVerifySuccess={handleVerifySuccess}
|
||||
/>
|
||||
<>
|
||||
<LandingPage
|
||||
onJoin={handlePay}
|
||||
onVerify={checkPaymentFromPB}
|
||||
onVerifyByEmail={checkVerifyByEmail}
|
||||
onVerifySuccess={handleVerifySuccess}
|
||||
/>
|
||||
<PayAuthModal
|
||||
open={payAuthModalOpen}
|
||||
onClose={() => setPayAuthModalOpen(false)}
|
||||
onSuccess={handlePayAuthSuccess}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</VipLayout>
|
||||
<PayAuthModal
|
||||
open={authForPayOpen}
|
||||
onClose={() => setAuthForPayOpen(false)}
|
||||
onSuccess={() => {
|
||||
setAuthForPayOpen(false);
|
||||
doPay();
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user