s''
This commit is contained in:
@@ -59,6 +59,7 @@ async function createPayOrder(
|
||||
) {
|
||||
const payload: Record<string, unknown> = {
|
||||
user_id,
|
||||
site_id: "vip",
|
||||
total_fee,
|
||||
type,
|
||||
channel,
|
||||
@@ -197,6 +198,7 @@ export async function POST(request: NextRequest) {
|
||||
// zpay:需要 redirect 获取 device 相关跳转
|
||||
const redirectPayload = {
|
||||
user_id,
|
||||
site_id: "vip",
|
||||
total_fee,
|
||||
type,
|
||||
channel,
|
||||
|
||||
@@ -39,6 +39,31 @@ export async function pbLogin(
|
||||
return { token: data.token, record: data.record };
|
||||
}
|
||||
|
||||
/** 从 PocketBase 错误响应中提取可读错误信息 */
|
||||
function parsePBError(err: { message?: string; data?: Record<string, { message?: string; code?: string }> }): string {
|
||||
const data = err?.data;
|
||||
if (data && typeof data === "object" && Object.keys(data).length > 0) {
|
||||
const first = Object.values(data)[0];
|
||||
if (first?.message) {
|
||||
const code = first.code;
|
||||
if (code === "validation_not_unique") {
|
||||
return "该邮箱已被注册,请直接登录";
|
||||
}
|
||||
if (code === "validation_required") {
|
||||
return "请填写必填项";
|
||||
}
|
||||
if (code?.startsWith("validation_")) {
|
||||
return first.message;
|
||||
}
|
||||
return first.message;
|
||||
}
|
||||
}
|
||||
if (err?.message === "Failed to create record.") {
|
||||
return "注册失败,该邮箱可能已被使用,请尝试直接登录";
|
||||
}
|
||||
return err?.message || "注册失败";
|
||||
}
|
||||
|
||||
export async function pbRegister(
|
||||
email: string,
|
||||
password: string,
|
||||
@@ -51,8 +76,8 @@ export async function pbRegister(
|
||||
body: JSON.stringify({ email, password, passwordConfirm }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
throw new Error(err?.message || "注册失败");
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(parsePBError(err));
|
||||
}
|
||||
return pbLogin(email, password);
|
||||
}
|
||||
@@ -69,7 +94,7 @@ export async function pbLogout(): Promise<void> {
|
||||
localStorage.removeItem(PB_STORAGE_KEYS.token);
|
||||
localStorage.removeItem(PB_STORAGE_KEYS.user);
|
||||
try {
|
||||
await fetch("/api/auth/logout", { method: "POST" });
|
||||
await fetch("/api/auth/logout", { method: "POST", credentials: "include" });
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
16
app/page.tsx
16
app/page.tsx
@@ -170,9 +170,9 @@ export default function Home() {
|
||||
}, []);
|
||||
|
||||
const getOrCreateUserId = useCallback(async () => {
|
||||
// 优先使用 PocketBase 用户 ID(已登录时)
|
||||
// 优先使用 PocketBase 用户 ID(已登录时,含跨站 Cookie)
|
||||
try {
|
||||
const meRes = await fetch("/api/auth/me");
|
||||
const meRes = await fetch("/api/auth/me", { credentials: "include", cache: "no-store" });
|
||||
const meData = await meRes.json();
|
||||
if (meData?.user?.id) {
|
||||
const pbUserId = meData.user.id;
|
||||
@@ -239,9 +239,10 @@ export default function Home() {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ token: result.token, record: result.record }),
|
||||
credentials: "include",
|
||||
});
|
||||
const userId = result.record.id;
|
||||
const vipRes = await fetch("/api/vip/check");
|
||||
const vipRes = await fetch("/api/vip/check", { credentials: "include", cache: "no-store" });
|
||||
const vipData = await vipRes.json();
|
||||
if (vipData?.vip) {
|
||||
setStorage("userId", userId);
|
||||
@@ -307,7 +308,7 @@ export default function Home() {
|
||||
|
||||
const handlePay = useCallback(async () => {
|
||||
try {
|
||||
const meRes = await fetch("/api/auth/me");
|
||||
const meRes = await fetch("/api/auth/me", { credentials: "include", cache: "no-store" });
|
||||
const meData = await meRes.json();
|
||||
if (meData?.user?.id) {
|
||||
doPay();
|
||||
@@ -368,16 +369,17 @@ export default function Home() {
|
||||
return;
|
||||
}
|
||||
|
||||
// SSO: 优先检查根域 Cookie(跨站登录态)
|
||||
// SSO: 优先检查根域 Cookie(跨站登录态,如从 digital 登录后跳转过来)
|
||||
try {
|
||||
const meRes = await fetch("/api/auth/me");
|
||||
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 });
|
||||
const vipRes = await fetch("/api/vip/check");
|
||||
const vipRes = await fetch("/api/vip/check", { credentials: "include", cache: "no-store" });
|
||||
const vipData = await vipRes.json();
|
||||
if (vipData?.vip) {
|
||||
setStorage("userId", meData.user.id);
|
||||
savePaidType("vip");
|
||||
saveUserName(meData.user.email || meData.user.id);
|
||||
setLoading(false);
|
||||
|
||||
@@ -43,6 +43,7 @@ export function AuthForm({ onSuccess, autoFocus }: AuthFormProps) {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ token: result.token, record: result.record }),
|
||||
credentials: "include",
|
||||
});
|
||||
onSuccess();
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user