From a2eb15c002b87f8a9fbeca1a98e9acdb47a5768a Mon Sep 17 00:00:00 2001 From: Clas Wen Date: Wed, 15 Oct 2025 20:13:59 +0800 Subject: [PATCH] Improve authentication callback UI and error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Enhanced callback.astro with loading states and user feedback - Fixed React prop naming in HeaderNavigation (stroke-width -> strokeWidth) - Added safe utility for promise error handling - Improved TicketForm authentication check using safe wrapper 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/components/header/HeaderNavigation.tsx | 8 +- src/pages/callback.astro | 127 ++++++++++++++++++++- src/pages/repair/TicketForm.tsx | 6 +- src/utils/safe.ts | 11 ++ 4 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 src/utils/safe.ts diff --git a/src/components/header/HeaderNavigation.tsx b/src/components/header/HeaderNavigation.tsx index 9c79f0e..3691ee0 100644 --- a/src/components/header/HeaderNavigation.tsx +++ b/src/components/header/HeaderNavigation.tsx @@ -65,8 +65,8 @@ export default function App() { { item.target == "_blank" && ( - - + + ) } @@ -103,8 +103,8 @@ export default function App() { { item.target == "_blank" && ( - - + + ) } diff --git a/src/pages/callback.astro b/src/pages/callback.astro index e8a4260..8789729 100644 --- a/src/pages/callback.astro +++ b/src/pages/callback.astro @@ -1,21 +1,134 @@ -
+--- +import BaseLayout from "../layouts/BaseLayout.astro" +import RepairHeader from "../components/header/RepairHeader.astro" +--- + + + +
+
+
+
+
处理登录中...
+
+ 正在验证身份 +
+
+
+
+
+
+
+
+
+ +
+
+
+ + diff --git a/src/pages/repair/TicketForm.tsx b/src/pages/repair/TicketForm.tsx index 9e0f4a7..5b03089 100644 --- a/src/pages/repair/TicketForm.tsx +++ b/src/pages/repair/TicketForm.tsx @@ -3,6 +3,7 @@ import { makeLogtoClient } from "../../utils/auth" import type { UserInfoResponse } from "@logto/browser" import { Alert, Form, Input, Button, Textarea } from "@heroui/react" import { saturdayClient } from "../../utils/client" +import { safe } from "../../utils/safe" type TicketFormData = { model?: string @@ -275,12 +276,11 @@ export default function App() { const check = async () => { const createRepairPath = "/repair/create-ticket" try { - const authenticated = await makeLogtoClient().isAuthenticated() - if (!authenticated) { + const [res, err] = await safe(makeLogtoClient().getIdTokenClaims()) + if (err) { window.location.href = `/repair/login-hint?redirectUrl=${createRepairPath}` return } - const res = await makeLogtoClient().getIdTokenClaims() setUserInfo(res) } catch (error) { diff --git a/src/utils/safe.ts b/src/utils/safe.ts new file mode 100644 index 0000000..aed1dd3 --- /dev/null +++ b/src/utils/safe.ts @@ -0,0 +1,11 @@ +export async function safe( + promise: Promise | (() => T), +): Promise<[T | null, Error | null]> { + try { + const result = promise instanceof Promise ? await promise : promise() + return [result, null] + } + catch (err) { + return [null, err instanceof Error ? err : new Error(String(err))] + } +}