| 1 | "use client"; |
| 2 | |
| 3 | import type { MultiSessionAuthClient } from "@better-auth-ui/core/plugins/multi-session"; |
| 4 | import { useAuth, useSession |
| 5 | import { useSetActiveSession } |
| 6 | import { |
| 7 | ChevronsUpDown, |
| 8 | LogIn, |
| 9 | LogOut, |
| 10 | Settings, |
| 11 | UserPlus2, |
| 12 | } from "lucide-react"; |
| 13 | import { isValidElement, type |
| 14 | |
| 15 | import { buttonVariants } |
| 16 | import { |
| 17 | DropdownMenu, |
| 18 | DropdownMenuContent, |
| 19 | DropdownMenuGroup, |
| 20 | DropdownMenuItem, |
| 21 | DropdownMenuLabel, |
| 22 | DropdownMenuSeparator, |
| 23 | DropdownMenuTrigger, |
| 24 | } from "@/components/ui/dropdown-menu"; |
| 25 | import { cn } |
| 26 | import { UserAvatar } |
| 27 | import { UserView } |
| 28 | |
| 29 | /** Auth states a `UserButton` link can be visible in. */ |
| 30 | export type UserButtonLinkVisibility |
| 31 | | "authenticated" |
| 32 | | "unauthenticated" |
| 33 | | "always" |
| 34 | |
| 35 | /** A simple link entry rendered as a `DropdownMenuItem` in the `UserButton` menu. */ |
| 36 | export type UserButtonLink |
| 37 | /** Visible label. */ |
| 38 | label: ReactNode |
| 39 | /** Destination URL. */ |
| 40 | href: string |
| 41 | /** Optional leading icon. Sized/coloured to match built-in items. */ |
| 42 | icon?: ReactNode |
| 43 | /** Forwarded to the underlying `DropdownMenuItem`. */ |
| 44 | variant?: " |
| 45 | /** |
| 46 | * When this link is visible based on auth state. |
| 47 | * @default |
| 48 | */ |
| 49 | visibility?: UserButtonLinkVisibility |
| 50 | }; |
| 51 | |
| 52 | export type UserButtonProps |
| 53 | className?: string |
| 54 | align?: " |
| 55 | side?: " |
| 56 | sideOffset?: number |
| 57 | size?: " |
| 58 | variant?: |
| 59 | | "default" |
| 60 | | "destructive" |
| 61 | | "ghost" |
| 62 | | "link" |
| 63 | | "outline" |
| 64 | | "secondary" |
| 65 | /** Additional menu entries rendered above the built-in items. */ |
| 66 | links?: ( |
| 67 | /** Hide the built-in "Settings" link. Useful when replacing it via `links`. */ |
| 68 | hideSettings?: boolean |
| 69 | }; |
| 70 | |
| 71 | function renderUserLink( |
| 72 | link: UserButtonLink |
| 73 | navigate: (options |
| 74 | fallbackKey: string, |
| 75 | ): ReactNode { |
| 76 | if (isValidElement(link |
| 77 | |
| 78 | const { label, |
| 79 | return ( |
| 80 | <DropdownMenuItem |
| 81 | key={fallbackKey} |
| 82 | variant={variant} |
| 83 | onClick={() => |
| 84 | > |
| 85 | {icon} |
| 86 | {label} |
| 87 | </DropdownMenuItem> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** Render a user dropdown button that shows user info, settings, theme controls, and authentication actions. */ |
| 92 | export function UserButton({ |
| 93 | className, |
| 94 | align, |
| 95 | side, |
| 96 | sideOffset, |
| 97 | size = "default" |
| 98 | variant = "ghost" |
| 99 | links, |
| 100 | hideSettings = false, |
| 101 | }: UserButtonProps) |
| 102 | const { authClient, |
| 103 | useAuth<MultiSessionAuthClient> |
| 104 | |
| 105 | const { isPending: |
| 106 | const { data: |
| 107 | |
| 108 | const userLinks = |
| 109 | if (!isValidElement(link |
| 110 | const visibility = |
| 111 | if (visibility === |
| 112 | if (visibility === |
| 113 | } |
| 114 | return [ |
| 115 | renderUserLink(link, navigate |
| 116 | ]; |
| 117 | }); |
| 118 | |
| 119 | // Whether anything renders between the user info label and the sign-out item, so the leading separator isn't shown with nothing to separate (see #439). |
| 120 | const hasSessionMenuItems = |
| 121 | (userLinks?.length ?? |
| 122 | !hideSettings || |
| 123 | plugins.some((plugin |
| 124 | |
| 125 | return ( |
| 126 | <DropdownMenu> |
| 127 | <DropdownMenuTrigger |
| 128 | aria-label={size === |
| 129 | className={ |
| 130 | size === "icon" |
| 131 | ? cn("rounded-full" |
| 132 | : cn( |
| 133 | buttonVariants({ variant, |
| 134 | "py-2.5 h-auto font-normal", |
| 135 | className, |
| 136 | ) |
| 137 | } |
| 138 | > |
| 139 | {size === "icon |
| 140 | <UserAvatar /> |
| 141 | ) : ( |
| 142 | <> |
| 143 | {session || sessionPending |
| 144 | <UserView isPending={ |
| 145 | ) : ( |
| 146 | <> |
| 147 | <UserAvatar /> |
| 148 | |
| 149 | <div className="grid flex-1 text-left text-sm leading-tight |
| 150 | {localization.auth.account |
| 151 | </div> |
| 152 | </> |
| 153 | )} |
| 154 | |
| 155 | <ChevronsUpDown className=" |
| 156 | </> |
| 157 | )} |
| 158 | </DropdownMenuTrigger> |
| 159 | |
| 160 | <DropdownMenuContent |
| 161 | className="min-w-40 md:min-w-56 max-w-[48svw]" |
| 162 | side={side} |
| 163 | sideOffset={sideOffset} |
| 164 | align={align} |
| 165 | > |
| 166 | {session && ( |
| 167 | <> |
| 168 | <DropdownMenuGroup> |
| 169 | <DropdownMenuLabel className=" |
| 170 | <UserView /> |
| 171 | </DropdownMenuLabel> |
| 172 | </DropdownMenuGroup> |
| 173 | |
| 174 | {hasSessionMenuItems && <DropdownMenuSeparator |
| 175 | </> |
| 176 | )} |
| 177 | |
| 178 | {session ? ( |
| 179 | <> |
| 180 | {userLinks} |
| 181 | |
| 182 | {!hideSettings && ( |
| 183 | <DropdownMenuItem |
| 184 | onClick={() => |
| 185 | navigate({ |
| 186 | to: `${basePaths. |
| 187 | }) |
| 188 | } |
| 189 | > |
| 190 | <Settings className=" |
| 191 | |
| 192 | {localization.settings.settings |
| 193 | </DropdownMenuItem> |
| 194 | )} |
| 195 | |
| 196 | {plugins.flatMap(( |
| 197 | plugin.userMenuItems?.map |
| 198 | <Item key={ |
| 199 | )), |
| 200 | )} |
| 201 | |
| 202 | <DropdownMenuSeparator /> |
| 203 | |
| 204 | <DropdownMenuItem |
| 205 | onClick={() => |
| 206 | navigate({ |
| 207 | to: `${basePaths. |
| 208 | }) |
| 209 | } |
| 210 | > |
| 211 | <LogOut className=" |
| 212 | |
| 213 | {localization.auth.signOut |
| 214 | </DropdownMenuItem> |
| 215 | </> |
| 216 | ) : ( |
| 217 | <> |
| 218 | {userLinks} |
| 219 | |
| 220 | <DropdownMenuItem |
| 221 | onClick={() => |
| 222 | navigate({ |
| 223 | to: `${basePaths. |
| 224 | }) |
| 225 | } |
| 226 | > |
| 227 | <LogIn className=" |
| 228 | |
| 229 | {localization.auth.signIn |
| 230 | </DropdownMenuItem> |
| 231 | |
| 232 | <DropdownMenuItem |
| 233 | onClick={() => |
| 234 | navigate({ |
| 235 | to: `${basePaths. |
| 236 | }) |
| 237 | } |
| 238 | > |
| 239 | <UserPlus2 className=" |
| 240 | |
| 241 | {localization.auth.signUp |
| 242 | </DropdownMenuItem> |
| 243 | |
| 244 | {plugins.flatMap(( |
| 245 | plugin.userMenuItems?.map |
| 246 | <Item key={ |
| 247 | )), |
| 248 | )} |
| 249 | </> |
| 250 | )} |
| 251 | </DropdownMenuContent> |
| 252 | </DropdownMenu> |
| 253 | ); |
| 254 | } |