Handle wallet session expiration
This commit is contained in:
@@ -551,6 +551,9 @@ export function AccountCenter() {
|
|||||||
? "Wallet unbound. New primary: {address}"
|
? "Wallet unbound. New primary: {address}"
|
||||||
: "钱包已解绑,新的主钱包:{address}",
|
: "钱包已解绑,新的主钱包:{address}",
|
||||||
unbindFailed: isEn ? "Failed to unbind wallet" : "解绑钱包失败",
|
unbindFailed: isEn ? "Failed to unbind wallet" : "解绑钱包失败",
|
||||||
|
authExpired: isEn
|
||||||
|
? "Session expired. Please sign out and sign in again."
|
||||||
|
: "登录会话已失效,请退出后重新登录。",
|
||||||
payNow: isEn ? "Subscribe & Activate" : "立即订阅并激活服务",
|
payNow: isEn ? "Subscribe & Activate" : "立即订阅并激活服务",
|
||||||
connectAndPay: isEn ? "Connect Wallet & Pay" : "连接钱包并支付",
|
connectAndPay: isEn ? "Connect Wallet & Pay" : "连接钱包并支付",
|
||||||
loginBeforeBind: isEn
|
loginBeforeBind: isEn
|
||||||
@@ -623,7 +626,19 @@ export function AccountCenter() {
|
|||||||
data: { session: cached },
|
data: { session: cached },
|
||||||
} = await client.auth.getSession();
|
} = await client.auth.getSession();
|
||||||
const cachedToken = String(cached?.access_token || "").trim();
|
const cachedToken = String(cached?.access_token || "").trim();
|
||||||
if (cachedToken) return cachedToken;
|
const expiresAtSec = Number(cached?.expires_at || 0);
|
||||||
|
const nowSec = Math.floor(Date.now() / 1000);
|
||||||
|
const refreshLeadSec = 90;
|
||||||
|
if (
|
||||||
|
cachedToken &&
|
||||||
|
Number.isFinite(expiresAtSec) &&
|
||||||
|
expiresAtSec > nowSec + refreshLeadSec
|
||||||
|
) {
|
||||||
|
return cachedToken;
|
||||||
|
}
|
||||||
|
if (cachedToken && (!Number.isFinite(expiresAtSec) || expiresAtSec <= 0)) {
|
||||||
|
return cachedToken;
|
||||||
|
}
|
||||||
// Session missing or expired — force a refresh.
|
// Session missing or expired — force a refresh.
|
||||||
const {
|
const {
|
||||||
data: { session: refreshed },
|
data: { session: refreshed },
|
||||||
@@ -643,14 +658,18 @@ export function AccountCenter() {
|
|||||||
}, [isEn, supabaseReady]);
|
}, [isEn, supabaseReady]);
|
||||||
|
|
||||||
const buildAuthedHeaders = useCallback(
|
const buildAuthedHeaders = useCallback(
|
||||||
async (withJson = false): Promise<Record<string, string>> => {
|
async (
|
||||||
|
withJson = false,
|
||||||
|
requireAuth = false,
|
||||||
|
): Promise<Record<string, string>> => {
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
if (withJson) headers["Content-Type"] = "application/json";
|
if (withJson) headers["Content-Type"] = "application/json";
|
||||||
if (!supabaseReady) return headers;
|
if (!supabaseReady) return headers;
|
||||||
try {
|
try {
|
||||||
const token = await getValidAccessToken();
|
const token = await getValidAccessToken();
|
||||||
headers.Authorization = `Bearer ${token}`;
|
headers.Authorization = `Bearer ${token}`;
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
if (requireAuth) throw error;
|
||||||
// Non-authenticated page load — silently skip.
|
// Non-authenticated page load — silently skip.
|
||||||
}
|
}
|
||||||
return headers;
|
return headers;
|
||||||
@@ -1308,7 +1327,7 @@ export function AccountCenter() {
|
|||||||
setPaymentError("");
|
setPaymentError("");
|
||||||
setPaymentInfo("");
|
setPaymentInfo("");
|
||||||
try {
|
try {
|
||||||
const headers = await buildAuthedHeaders(true);
|
const headers = await buildAuthedHeaders(true, true);
|
||||||
const res = await fetch("/api/payments/wallets", {
|
const res = await fetch("/api/payments/wallets", {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers,
|
headers,
|
||||||
@@ -1320,6 +1339,14 @@ export function AccountCenter() {
|
|||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(raw);
|
const parsed = JSON.parse(raw);
|
||||||
detail = String(parsed?.detail || parsed?.error || raw);
|
detail = String(parsed?.detail || parsed?.error || raw);
|
||||||
|
if (detail.trim().startsWith("{")) {
|
||||||
|
try {
|
||||||
|
const nested = JSON.parse(detail);
|
||||||
|
detail = String(nested?.detail || nested?.error || detail);
|
||||||
|
} catch {
|
||||||
|
// ignore nested parse failure
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
@@ -1354,6 +1381,15 @@ export function AccountCenter() {
|
|||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = normalizePaymentError(error).message;
|
const message = normalizePaymentError(error).message;
|
||||||
|
const lower = String(message || "").toLowerCase();
|
||||||
|
if (
|
||||||
|
lower.includes("unauthorized") ||
|
||||||
|
lower.includes("session required") ||
|
||||||
|
lower.includes("401")
|
||||||
|
) {
|
||||||
|
setPaymentError(`${copy.unbindFailed}: ${copy.authExpired}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
setPaymentError(`${copy.unbindFailed}: ${message}`);
|
setPaymentError(`${copy.unbindFailed}: ${message}`);
|
||||||
} finally {
|
} finally {
|
||||||
setPaymentBusy(false);
|
setPaymentBusy(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user