From 5b7ccf0b671e2999b62befc729a3e517a0433728 Mon Sep 17 00:00:00 2001 From: Bertrand Yuan Date: Mon, 15 Dec 2025 23:48:10 +0800 Subject: initial commit -- the front-end prototype The initial code is base on Anirudh's work. More to see at: https://github.com/techwithanirudh/shadcn-blog Therefore, the code in this commit is under MIT license. --- src/components/auth/user-avatar.tsx | 56 ++++++++++++++++ src/components/auth/user-button.tsx | 127 ++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 src/components/auth/user-avatar.tsx create mode 100644 src/components/auth/user-button.tsx (limited to 'src/components/auth') diff --git a/src/components/auth/user-avatar.tsx b/src/components/auth/user-avatar.tsx new file mode 100644 index 0000000..53b20b5 --- /dev/null +++ b/src/components/auth/user-avatar.tsx @@ -0,0 +1,56 @@ +import type { ComponentProps } from 'react'; + +import { Icons } from '@/components/icons/icons'; +import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; +import type { User } from '@/lib/auth-client'; +import { cn } from '@/lib/utils'; + +export interface UserAvatarClassNames { + base?: string; + image?: string; + fallback?: string; + fallbackIcon?: string; +} + +export interface UserAvatarProps { + user?: User | null; + classNames?: UserAvatarClassNames; +} + +export function UserAvatar({ + user, + classNames, + className, + ...props +}: UserAvatarProps & ComponentProps) { + const name = user?.name || user?.email; + const src = user?.image; + + return ( + + + + + {firstTwoCharacters(name) ?? ( + + )} + + + ); +} + +const firstTwoCharacters = (name?: string | null) => name?.slice(0, 2); diff --git a/src/components/auth/user-button.tsx b/src/components/auth/user-button.tsx new file mode 100644 index 0000000..7caea4c --- /dev/null +++ b/src/components/auth/user-button.tsx @@ -0,0 +1,127 @@ +'use client'; + +import Link from 'next/link'; + +import { Icons } from '@/components/icons/icons'; +import { Button } from '@/components/ui/button'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { Skeleton } from '@/components/ui/skeleton'; +import { signOut, useSession } from '@/lib/auth-client'; +import { cn } from '@/lib/utils'; + +import type { UserAvatarClassNames } from './user-avatar'; +import { UserAvatar } from './user-avatar'; + +export interface UserButtonClassNames { + base?: string; + skeleton?: string; + trigger?: { + base?: string; + avatar?: UserAvatarClassNames; + skeleton?: string; + }; + content?: { + base?: string; + avatar?: UserAvatarClassNames; + menuItem?: string; + separator?: string; + }; +} + +export interface UserButtonProps { + className?: string; + classNames?: UserButtonClassNames; +} + +export function UserButton({ className, classNames }: UserButtonProps) { + const { data: sessionData, isPending } = useSession(); + const user = sessionData?.user ?? null; + + return ( + + + + + + e.preventDefault()} + align='end' + > + {user ? ( +
+ + +
+
+ {user.name || user.email} +
+ + {user.name && ( +
+ {user.email} +
+ )} +
+
+ ) : ( +
Account
+ )} + + + + {!user ? ( + <> + + + + Sign In + + + + ) : ( + <> + signOut()} + > + + Log Out + + + )} +
+
+ ); +} -- cgit v1.2.3