diff --git a/tone-page-web/components/app-sidebar.tsx b/tone-page-web/components/app-sidebar.tsx index cda2c5e..e94ec71 100644 --- a/tone-page-web/components/app-sidebar.tsx +++ b/tone-page-web/components/app-sidebar.tsx @@ -21,7 +21,6 @@ import { SidebarMenuItem, SidebarRail, } from "@/components/ui/sidebar" -import { useRouter } from "next/navigation" import Link from "next/link" // This is sample data. const data = { @@ -105,7 +104,7 @@ export function AppSidebar({ ...props }: React.ComponentProps) { - + diff --git a/tone-page-web/components/nav-user.tsx b/tone-page-web/components/nav-user.tsx index be3b87a..3135657 100644 --- a/tone-page-web/components/nav-user.tsx +++ b/tone-page-web/components/nav-user.tsx @@ -1,12 +1,10 @@ "use client" import { - BadgeCheck, - Bell, ChevronsUpDown, - CreditCard, + KeyRound, LogOut, - Sparkles, + UserRoundCog, } from "lucide-react" import { @@ -17,7 +15,6 @@ import { import { DropdownMenu, DropdownMenuContent, - DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, @@ -29,17 +26,30 @@ import { SidebarMenuItem, useSidebar, } from "@/components/ui/sidebar" +import useSWR from "swr" +import { UserApi } from "@/lib/api" +import { Skeleton } from "./ui/skeleton" +import { toast } from "sonner" +import { useRouter } from "next/navigation" + +export function NavUser({ }: {}) { + const { isMobile } = useSidebar(); + const router = useRouter(); + + const { data: user, isLoading, error } = useSWR( + '/api/user/me', + () => UserApi.me(), + { + onError: (error) => { + if (`${error}`.includes('Unauthorized')) { + localStorage.removeItem('token'); + toast.info('登录凭证已失效,请重新登录'); + router.replace('/console/login'); + } + } + } + ); -export function NavUser({ - user, -}: { - user: { - name: string - email: string - avatar: string - } -}) { - const { isMobile } = useSidebar() return ( @@ -50,14 +60,27 @@ export function NavUser({ size="lg" className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground" > - - - U - -
- {user.name} - {user.email} -
+ { + user && <> + + + U + +
+ {user.nickname} + {user.username} +
+ + } + { + isLoading &&
+ +
+ + +
+
+ } @@ -68,43 +91,42 @@ export function NavUser({ sideOffset={4} > -
- - - U - -
- {user.name} - {user.email} + { + user && +
+ + + U + +
+ {user.nickname} + {user.username} +
-
+ } + { + isLoading &&
+ +
+ + +
+
+ } - - - - Upgrade to Pro - - - - - - - Account - - - - Billing - - - - Notifications - - + + + 账户信息 + + + + 修改密码 + - Log out + 登出 diff --git a/tone-page-web/lib/api/index.ts b/tone-page-web/lib/api/index.ts index 16872be..26d5f04 100644 --- a/tone-page-web/lib/api/index.ts +++ b/tone-page-web/lib/api/index.ts @@ -1,4 +1,5 @@ export * as authApi from './auth/index'; export * as verificationApi from './verification/index'; export * as AdminApi from './admin/index'; -export * as ResourceApi from './resource/index'; \ No newline at end of file +export * as ResourceApi from './resource/index'; +export * as UserApi from './user/index'; \ No newline at end of file diff --git a/tone-page-web/lib/api/user/index.ts b/tone-page-web/lib/api/user/index.ts new file mode 100644 index 0000000..57dfe1e --- /dev/null +++ b/tone-page-web/lib/api/user/index.ts @@ -0,0 +1 @@ +export * from './me'; \ No newline at end of file diff --git a/tone-page-web/lib/api/user/me.ts b/tone-page-web/lib/api/user/me.ts new file mode 100644 index 0000000..c26b9da --- /dev/null +++ b/tone-page-web/lib/api/user/me.ts @@ -0,0 +1,6 @@ +import { User } from "@/lib/types/user"; +import fetcher from "../fetcher"; + +export async function me() { + return fetcher('/api/user/me'); +} \ No newline at end of file