@better-auth-ui/heroui 1.6.3 → 1.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/auth/additional-field.d.ts +4 -0
- package/dist/components/auth/additional-field.js +270 -0
- package/dist/components/auth/auth-provider.d.ts +4 -6
- package/dist/components/auth/auth.d.ts +1 -1
- package/dist/components/auth/auth.js +54 -28
- package/dist/components/{settings/security → auth/delete-user}/danger-zone.d.ts +1 -1
- package/dist/components/{settings/security → auth/delete-user}/danger-zone.js +1 -1
- package/dist/components/auth/delete-user/delete-user.js +95 -0
- package/dist/components/{email → auth/email}/email-changed-email.d.ts +2 -2
- package/dist/components/{email → auth/email}/email-changed-email.js +7 -7
- package/dist/components/{email → auth/email}/email-verification-email.d.ts +2 -2
- package/dist/components/{email → auth/email}/email-verification-email.js +7 -7
- package/dist/components/auth/email/magic-link-email.d.ts +3 -0
- package/dist/components/{email → auth/email}/magic-link-email.js +7 -7
- package/dist/components/auth/email/new-device-email.d.ts +3 -0
- package/dist/components/{email → auth/email}/new-device-email.js +7 -7
- package/dist/components/auth/email/otp-email.d.ts +3 -0
- package/dist/components/{email → auth/email}/otp-email.js +6 -6
- package/dist/components/{email → auth/email}/password-changed-email.d.ts +2 -2
- package/dist/components/{email → auth/email}/password-changed-email.js +7 -7
- package/dist/components/{email → auth/email}/reset-password-email.d.ts +2 -2
- package/dist/components/{email → auth/email}/reset-password-email.js +7 -7
- package/dist/components/auth/forgot-password.d.ts +1 -1
- package/dist/components/auth/forgot-password.js +70 -48
- package/dist/components/auth/magic-link/magic-link-button.d.ts +11 -0
- package/dist/components/auth/magic-link/magic-link-button.js +20 -0
- package/dist/components/auth/magic-link/magic-link.d.ts +16 -0
- package/dist/components/auth/magic-link/magic-link.js +87 -0
- package/dist/components/{settings/account → auth/multi-session}/manage-account.d.ts +2 -6
- package/dist/components/auth/multi-session/manage-account.js +49 -0
- package/dist/components/auth/multi-session/manage-accounts.js +34 -0
- package/dist/components/{user/switch-account-menu.d.ts → auth/multi-session/switch-account-submenu-content.d.ts} +3 -3
- package/dist/components/auth/multi-session/switch-account-submenu-content.js +29 -0
- package/dist/components/{user/switch-account-item.d.ts → auth/multi-session/switch-account-submenu-item.d.ts} +2 -2
- package/dist/components/auth/multi-session/switch-account-submenu-item.js +20 -0
- package/dist/components/auth/multi-session/switch-account-submenu.d.ts +13 -0
- package/dist/components/auth/multi-session/switch-account-submenu.js +24 -0
- package/dist/components/auth/passkey/passkey-button.d.ts +6 -0
- package/dist/components/auth/passkey/passkey-button.js +24 -0
- package/dist/components/auth/passkey/passkey.js +45 -0
- package/dist/components/auth/passkey/passkeys.js +106 -0
- package/dist/components/auth/provider-button.d.ts +6 -8
- package/dist/components/auth/provider-button.js +16 -20
- package/dist/components/auth/provider-buttons.d.ts +3 -4
- package/dist/components/auth/provider-buttons.js +9 -10
- package/dist/components/auth/reset-password.js +110 -107
- package/dist/components/{settings → auth/settings}/account/account-settings.d.ts +5 -2
- package/dist/components/auth/settings/account/account-settings.js +20 -0
- package/dist/components/auth/settings/account/change-avatar.js +89 -0
- package/dist/components/{settings → auth/settings}/account/change-email.js +17 -17
- package/dist/components/{settings → auth/settings}/account/user-profile.d.ts +2 -3
- package/dist/components/auth/settings/account/user-profile.js +86 -0
- package/dist/components/auth/settings/security/active-session.js +59 -0
- package/dist/components/{settings → auth/settings}/security/active-sessions.js +4 -4
- package/dist/components/auth/settings/security/change-password.js +189 -0
- package/dist/components/auth/settings/security/linked-account.js +56 -0
- package/dist/components/{settings → auth/settings}/security/linked-accounts.js +5 -5
- package/dist/components/{settings → auth/settings}/security/security-settings.d.ts +1 -1
- package/dist/components/auth/settings/security/security-settings.js +22 -0
- package/dist/components/auth/settings/settings.js +45 -0
- package/dist/components/auth/sign-in.js +66 -85
- package/dist/components/auth/sign-out.js +11 -11
- package/dist/components/auth/sign-up.js +128 -148
- package/dist/components/{settings/account → auth/theme}/appearance.d.ts +3 -6
- package/dist/components/auth/theme/appearance.js +72 -0
- package/dist/components/auth/theme/theme-toggle-item.d.ts +5 -0
- package/dist/components/auth/theme/theme-toggle-item.js +58 -0
- package/dist/components/auth/user/user-avatar.js +29 -0
- package/dist/components/{user → auth/user}/user-button.d.ts +2 -6
- package/dist/components/auth/user/user-button.js +55 -0
- package/dist/components/auth/user/user-view.js +37 -0
- package/dist/components/auth/username/sign-in-username.d.ts +14 -0
- package/dist/components/auth/username/sign-in-username.js +148 -0
- package/dist/components/auth/username/username-field.d.ts +8 -0
- package/dist/components/auth/username/username-field.js +53 -0
- package/dist/email.d.ts +7 -0
- package/dist/email.js +9 -0
- package/dist/index.d.ts +12 -27
- package/dist/index.js +20 -35
- package/dist/lib/auth/auth-plugin.d.ts +44 -0
- package/dist/lib/auth/delete-user-plugin.d.ts +17 -0
- package/dist/lib/auth/delete-user-plugin.js +10 -0
- package/dist/lib/auth/magic-link-plugin.d.ts +31 -0
- package/dist/lib/auth/magic-link-plugin.js +13 -0
- package/dist/lib/auth/multi-session-plugin.d.ts +18 -0
- package/dist/lib/auth/multi-session-plugin.js +12 -0
- package/dist/lib/auth/passkey-plugin.d.ts +20 -0
- package/dist/lib/auth/passkey-plugin.js +12 -0
- package/dist/lib/auth/theme-plugin.d.ts +68 -0
- package/dist/lib/auth/theme-plugin.js +23 -0
- package/dist/lib/auth/username-plugin.d.ts +63 -0
- package/dist/lib/auth/username-plugin.js +18 -0
- package/dist/plugins.d.ts +20 -0
- package/dist/plugins.js +21 -0
- package/package.json +19 -21
- package/src/components/auth/additional-field.tsx +554 -0
- package/src/components/auth/auth-provider.tsx +5 -6
- package/src/components/auth/auth.tsx +99 -35
- package/src/components/{settings/security → auth/delete-user}/danger-zone.tsx +1 -1
- package/src/components/{settings/security → auth/delete-user}/delete-user.tsx +30 -20
- package/src/components/{email → auth/email}/email-changed-email.tsx +2 -2
- package/src/components/{email → auth/email}/email-verification-email.tsx +2 -2
- package/src/components/{email → auth/email}/magic-link-email.tsx +2 -2
- package/src/components/{email → auth/email}/new-device-email.tsx +2 -2
- package/src/components/{email → auth/email}/otp-email.tsx +2 -2
- package/src/components/{email → auth/email}/password-changed-email.tsx +2 -2
- package/src/components/{email → auth/email}/reset-password-email.tsx +2 -2
- package/src/components/auth/forgot-password.tsx +42 -17
- package/src/components/auth/magic-link/magic-link-button.tsx +61 -0
- package/src/components/auth/{magic-link.tsx → magic-link/magic-link.tsx} +55 -40
- package/src/components/{settings/account → auth/multi-session}/manage-account.tsx +16 -13
- package/src/components/{settings/account → auth/multi-session}/manage-accounts.tsx +14 -5
- package/src/components/{user/switch-account-menu.tsx → auth/multi-session/switch-account-submenu-content.tsx} +18 -11
- package/src/components/{user/switch-account-item.tsx → auth/multi-session/switch-account-submenu-item.tsx} +17 -5
- package/src/components/auth/multi-session/switch-account-submenu.tsx +50 -0
- package/src/components/auth/passkey/passkey-button.tsx +56 -0
- package/src/components/{settings/security → auth/passkey}/passkey.tsx +20 -6
- package/src/components/auth/passkey/passkeys.tsx +196 -0
- package/src/components/auth/provider-button.tsx +27 -29
- package/src/components/auth/provider-buttons.tsx +6 -11
- package/src/components/auth/reset-password.tsx +13 -7
- package/src/components/{settings → auth/settings}/account/account-settings.tsx +15 -13
- package/src/components/{settings → auth/settings}/account/change-avatar.tsx +4 -3
- package/src/components/{settings → auth/settings}/account/change-email.tsx +3 -3
- package/src/components/auth/settings/account/user-profile.tsx +171 -0
- package/src/components/{settings → auth/settings}/security/active-session.tsx +8 -5
- package/src/components/{settings → auth/settings}/security/active-sessions.tsx +3 -3
- package/src/components/{settings → auth/settings}/security/change-password.tsx +14 -9
- package/src/components/{settings → auth/settings}/security/linked-account.tsx +12 -8
- package/src/components/{settings → auth/settings}/security/linked-accounts.tsx +2 -2
- package/src/components/{settings → auth/settings}/security/security-settings.tsx +7 -6
- package/src/components/{settings → auth/settings}/settings.tsx +2 -2
- package/src/components/auth/sign-in.tsx +50 -61
- package/src/components/auth/sign-out.tsx +2 -2
- package/src/components/auth/sign-up.tsx +119 -131
- package/src/components/{settings/account → auth/theme}/appearance.tsx +17 -24
- package/src/components/auth/theme/theme-toggle-item.tsx +110 -0
- package/src/components/{user → auth/user}/user-avatar.tsx +12 -4
- package/src/components/{user → auth/user}/user-button.tsx +15 -88
- package/src/components/{user → auth/user}/user-view.tsx +12 -4
- package/src/components/auth/username/sign-in-username.tsx +299 -0
- package/src/components/auth/username/username-field.tsx +124 -0
- package/src/email.ts +9 -0
- package/src/index.tsx +12 -27
- package/src/lib/auth/auth-plugin.ts +67 -0
- package/src/lib/auth/delete-user-plugin.ts +15 -0
- package/src/lib/auth/magic-link-plugin.ts +26 -0
- package/src/lib/auth/multi-session-plugin.ts +17 -0
- package/src/lib/auth/passkey-plugin.ts +17 -0
- package/src/lib/auth/theme-plugin.ts +86 -0
- package/src/lib/auth/username-plugin.tsx +30 -0
- package/src/plugins.ts +23 -0
- package/dist/components/auth/magic-link-button.d.ts +0 -13
- package/dist/components/auth/magic-link-button.js +0 -16
- package/dist/components/auth/magic-link.d.ts +0 -17
- package/dist/components/auth/magic-link.js +0 -99
- package/dist/components/auth/passkey-button.d.ts +0 -4
- package/dist/components/auth/passkey-button.js +0 -21
- package/dist/components/email/magic-link-email.d.ts +0 -3
- package/dist/components/email/new-device-email.d.ts +0 -3
- package/dist/components/email/otp-email.d.ts +0 -3
- package/dist/components/settings/account/account-settings.js +0 -23
- package/dist/components/settings/account/appearance.js +0 -71
- package/dist/components/settings/account/change-avatar.js +0 -89
- package/dist/components/settings/account/manage-account.js +0 -48
- package/dist/components/settings/account/manage-accounts.js +0 -33
- package/dist/components/settings/account/user-profile.js +0 -103
- package/dist/components/settings/security/active-session.js +0 -59
- package/dist/components/settings/security/change-password.js +0 -189
- package/dist/components/settings/security/delete-user.js +0 -92
- package/dist/components/settings/security/linked-account.js +0 -56
- package/dist/components/settings/security/passkey.js +0 -43
- package/dist/components/settings/security/passkeys.js +0 -53
- package/dist/components/settings/security/security-settings.js +0 -25
- package/dist/components/settings/settings.js +0 -45
- package/dist/components/user/switch-account-item.js +0 -20
- package/dist/components/user/switch-account-menu.js +0 -28
- package/dist/components/user/user-avatar.js +0 -29
- package/dist/components/user/user-button.js +0 -91
- package/dist/components/user/user-view.js +0 -37
- package/dist/core.d.ts +0 -1
- package/dist/core.js +0 -1
- package/dist/react-exports.d.ts +0 -1
- package/dist/react-exports.js +0 -1
- package/src/components/auth/magic-link-button.tsx +0 -43
- package/src/components/auth/passkey-button.tsx +0 -35
- package/src/components/settings/account/user-profile.tsx +0 -217
- package/src/components/settings/security/passkeys.tsx +0 -96
- package/src/core.ts +0 -1
- package/src/react-exports.ts +0 -1
- /package/dist/components/{settings/security → auth/delete-user}/delete-user.d.ts +0 -0
- /package/dist/components/{settings/account → auth/multi-session}/manage-accounts.d.ts +0 -0
- /package/dist/components/{settings/security → auth/passkey}/passkey.d.ts +0 -0
- /package/dist/components/{settings/security → auth/passkey}/passkeys.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/account/change-avatar.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/account/change-email.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/security/active-session.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/security/active-sessions.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/security/change-password.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/security/linked-account.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/security/linked-accounts.d.ts +0 -0
- /package/dist/components/{settings → auth/settings}/settings.d.ts +0 -0
- /package/dist/components/{user → auth/user}/user-avatar.d.ts +0 -0
- /package/dist/components/{user → auth/user}/user-view.d.ts +0 -0
|
@@ -3,12 +3,8 @@ import {
|
|
|
3
3
|
ArrowRightFromSquare,
|
|
4
4
|
ArrowRightToSquare,
|
|
5
5
|
ChevronsExpandVertical,
|
|
6
|
-
Display,
|
|
7
6
|
Gear,
|
|
8
|
-
|
|
9
|
-
PersonPlus,
|
|
10
|
-
Persons,
|
|
11
|
-
Sun
|
|
7
|
+
PersonPlus
|
|
12
8
|
} from "@gravity-ui/icons"
|
|
13
9
|
import {
|
|
14
10
|
Button,
|
|
@@ -17,11 +13,9 @@ import {
|
|
|
17
13
|
Dropdown,
|
|
18
14
|
type DropdownPopoverProps,
|
|
19
15
|
Label,
|
|
20
|
-
Separator
|
|
21
|
-
Tabs
|
|
16
|
+
Separator
|
|
22
17
|
} from "@heroui/react"
|
|
23
18
|
|
|
24
|
-
import { SwitchAccountMenu } from "./switch-account-menu"
|
|
25
19
|
import { UserAvatar } from "./user-avatar"
|
|
26
20
|
import { UserView } from "./user-view"
|
|
27
21
|
|
|
@@ -33,39 +27,34 @@ export type UserButtonProps = {
|
|
|
33
27
|
* @default "bottom"
|
|
34
28
|
*/
|
|
35
29
|
placement?: DropdownPopoverProps["placement"]
|
|
36
|
-
themeToggle?: boolean
|
|
37
30
|
variant?: ButtonProps["variant"]
|
|
38
31
|
}
|
|
39
32
|
|
|
40
33
|
/**
|
|
41
|
-
* Render a user account dropdown button that shows account actions
|
|
42
|
-
*
|
|
43
|
-
* Renders either an icon-only trigger or a full button showing the current user state; its dropdown contains account settings, multi-session switching (when enabled), theme selection (when enabled), and sign-in/sign-out flows depending on authentication state.
|
|
34
|
+
* Render a user account dropdown button that shows account actions.
|
|
44
35
|
*
|
|
45
36
|
* @param className - Additional CSS classes applied to the trigger element
|
|
46
37
|
* @param placement - Dropdown popover placement (e.g., "bottom", "top-start", "bottom-end")
|
|
47
38
|
* @param size - "icon" renders an avatar-only trigger; "default" renders a button with label and chevron
|
|
48
39
|
* @param variant - Button visual variant passed to the underlying Button component
|
|
49
|
-
* @param themeToggle - When true and theming is available, show theme selection controls in the menu
|
|
50
40
|
* @returns The user button and its dropdown menu as a JSX element
|
|
51
41
|
*/
|
|
52
42
|
export function UserButton({
|
|
53
43
|
className,
|
|
54
44
|
placement = "bottom",
|
|
55
45
|
size = "default",
|
|
56
|
-
variant = "ghost"
|
|
57
|
-
themeToggle = true
|
|
46
|
+
variant = "ghost"
|
|
58
47
|
}: UserButtonProps) {
|
|
59
|
-
const {
|
|
60
|
-
basePaths,
|
|
61
|
-
viewPaths,
|
|
62
|
-
localization,
|
|
63
|
-
multiSession,
|
|
64
|
-
appearance: { theme, setTheme, themes }
|
|
65
|
-
} = useAuth()
|
|
48
|
+
const { authClient, basePaths, viewPaths, localization, plugins } = useAuth()
|
|
66
49
|
|
|
67
|
-
const { data: session, isPending: sessionPending } = useSession()
|
|
50
|
+
const { data: session, isPending: sessionPending } = useSession(authClient)
|
|
68
51
|
|
|
52
|
+
const userMenuItems = plugins.flatMap(
|
|
53
|
+
(plugin) =>
|
|
54
|
+
plugin.userMenuItems?.map((Item, index) => (
|
|
55
|
+
<Item key={`${plugin.id}-${index.toString()}`} />
|
|
56
|
+
)) ?? []
|
|
57
|
+
)
|
|
69
58
|
return (
|
|
70
59
|
<Dropdown>
|
|
71
60
|
{size === "icon" ? (
|
|
@@ -116,71 +105,7 @@ export function UserButton({
|
|
|
116
105
|
<Label>{localization.settings.settings}</Label>
|
|
117
106
|
</Dropdown.Item>
|
|
118
107
|
|
|
119
|
-
{
|
|
120
|
-
<Dropdown.SubmenuTrigger>
|
|
121
|
-
<Dropdown.Item textValue={localization.auth.switchAccount}>
|
|
122
|
-
<Persons className="text-muted" />
|
|
123
|
-
|
|
124
|
-
<Label>{localization.auth.switchAccount}</Label>
|
|
125
|
-
|
|
126
|
-
<Dropdown.SubmenuIndicator />
|
|
127
|
-
</Dropdown.Item>
|
|
128
|
-
|
|
129
|
-
<Dropdown.Popover className="min-w-40 md:min-w-56 max-w-[48svw]">
|
|
130
|
-
<SwitchAccountMenu />
|
|
131
|
-
</Dropdown.Popover>
|
|
132
|
-
</Dropdown.SubmenuTrigger>
|
|
133
|
-
)}
|
|
134
|
-
|
|
135
|
-
{themeToggle && theme && setTheme && !!themes?.length && (
|
|
136
|
-
<Dropdown.Item className="py-1 pe-2">
|
|
137
|
-
<Label>{localization.settings.theme}</Label>
|
|
138
|
-
|
|
139
|
-
<Tabs
|
|
140
|
-
className="ml-auto"
|
|
141
|
-
selectedKey={theme}
|
|
142
|
-
onSelectionChange={(key) => setTheme(key as string)}
|
|
143
|
-
>
|
|
144
|
-
<Tabs.ListContainer>
|
|
145
|
-
<Tabs.List
|
|
146
|
-
aria-label={localization.settings.theme}
|
|
147
|
-
className="*:h-5 *:w-5 *:p-0"
|
|
148
|
-
>
|
|
149
|
-
{themes.includes("system") && (
|
|
150
|
-
<Tabs.Tab
|
|
151
|
-
id="system"
|
|
152
|
-
aria-label={localization.settings.system}
|
|
153
|
-
>
|
|
154
|
-
<Display className="size-3" />
|
|
155
|
-
|
|
156
|
-
<Tabs.Indicator />
|
|
157
|
-
</Tabs.Tab>
|
|
158
|
-
)}
|
|
159
|
-
{themes.includes("light") && (
|
|
160
|
-
<Tabs.Tab
|
|
161
|
-
id="light"
|
|
162
|
-
aria-label={localization.settings.light}
|
|
163
|
-
>
|
|
164
|
-
<Sun className="size-3" />
|
|
165
|
-
|
|
166
|
-
<Tabs.Indicator />
|
|
167
|
-
</Tabs.Tab>
|
|
168
|
-
)}
|
|
169
|
-
{themes.includes("dark") && (
|
|
170
|
-
<Tabs.Tab
|
|
171
|
-
id="dark"
|
|
172
|
-
aria-label={localization.settings.dark}
|
|
173
|
-
>
|
|
174
|
-
<Moon className="size-3" />
|
|
175
|
-
|
|
176
|
-
<Tabs.Indicator />
|
|
177
|
-
</Tabs.Tab>
|
|
178
|
-
)}
|
|
179
|
-
</Tabs.List>
|
|
180
|
-
</Tabs.ListContainer>
|
|
181
|
-
</Tabs>
|
|
182
|
-
</Dropdown.Item>
|
|
183
|
-
)}
|
|
108
|
+
{userMenuItems}
|
|
184
109
|
|
|
185
110
|
<Separator />
|
|
186
111
|
|
|
@@ -212,6 +137,8 @@ export function UserButton({
|
|
|
212
137
|
|
|
213
138
|
<Label>{localization.auth.signUp}</Label>
|
|
214
139
|
</Dropdown.Item>
|
|
140
|
+
|
|
141
|
+
{userMenuItems}
|
|
215
142
|
</>
|
|
216
143
|
)}
|
|
217
144
|
</Dropdown.Menu>
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type UsernameAuthClient,
|
|
3
|
+
useAuth,
|
|
4
|
+
useSession
|
|
5
|
+
} from "@better-auth-ui/react"
|
|
2
6
|
import { type AvatarProps, cn, Skeleton } from "@heroui/react"
|
|
3
7
|
import type { User } from "better-auth"
|
|
4
8
|
import type { ComponentProps } from "react"
|
|
@@ -27,9 +31,13 @@ export function UserView({
|
|
|
27
31
|
user,
|
|
28
32
|
...props
|
|
29
33
|
}: UserViewProps & ComponentProps<"div">) {
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
const { authClient } = useAuth()
|
|
35
|
+
const { data: session, isPending: sessionPending } = useSession(
|
|
36
|
+
authClient as UsernameAuthClient,
|
|
37
|
+
{
|
|
38
|
+
enabled: !user && !isPending
|
|
39
|
+
}
|
|
40
|
+
)
|
|
33
41
|
|
|
34
42
|
const resolvedUser = user ?? session?.user
|
|
35
43
|
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type UsernameAuthClient,
|
|
4
|
+
useAuth,
|
|
5
|
+
useAuthPlugin,
|
|
6
|
+
useFetchOptions,
|
|
7
|
+
useSendVerificationEmail,
|
|
8
|
+
useSignInEmail,
|
|
9
|
+
useSignInUsername
|
|
10
|
+
} from "@better-auth-ui/react"
|
|
11
|
+
import {
|
|
12
|
+
Button,
|
|
13
|
+
Card,
|
|
14
|
+
type CardProps,
|
|
15
|
+
Checkbox,
|
|
16
|
+
cn,
|
|
17
|
+
Description,
|
|
18
|
+
FieldError,
|
|
19
|
+
Form,
|
|
20
|
+
Input,
|
|
21
|
+
Label,
|
|
22
|
+
Link,
|
|
23
|
+
Spinner,
|
|
24
|
+
TextField,
|
|
25
|
+
toast
|
|
26
|
+
} from "@heroui/react"
|
|
27
|
+
import { useIsMutating } from "@tanstack/react-query"
|
|
28
|
+
import { type SyntheticEvent, useState } from "react"
|
|
29
|
+
import { usernamePlugin } from "../../../lib/auth/username-plugin"
|
|
30
|
+
import { FieldSeparator } from "../field-separator"
|
|
31
|
+
import { ProviderButtons, type SocialLayout } from "../provider-buttons"
|
|
32
|
+
|
|
33
|
+
export interface SignInUsernameProps {
|
|
34
|
+
className?: string
|
|
35
|
+
socialLayout?: SocialLayout
|
|
36
|
+
socialPosition?: "top" | "bottom"
|
|
37
|
+
variant?: CardProps["variant"]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Render the username-based sign-in UI.
|
|
42
|
+
*
|
|
43
|
+
* @returns The sign-in JSX element containing username/password fields, optional magic-link button, and social provider buttons.
|
|
44
|
+
*/
|
|
45
|
+
export function SignInUsername({
|
|
46
|
+
className,
|
|
47
|
+
socialLayout,
|
|
48
|
+
socialPosition = "bottom",
|
|
49
|
+
variant,
|
|
50
|
+
...props
|
|
51
|
+
}: SignInUsernameProps & Omit<CardProps, "children">) {
|
|
52
|
+
const {
|
|
53
|
+
authClient,
|
|
54
|
+
basePaths,
|
|
55
|
+
baseURL,
|
|
56
|
+
emailAndPassword,
|
|
57
|
+
localization,
|
|
58
|
+
plugins,
|
|
59
|
+
redirectTo,
|
|
60
|
+
socialProviders,
|
|
61
|
+
viewPaths,
|
|
62
|
+
navigate
|
|
63
|
+
} = useAuth()
|
|
64
|
+
|
|
65
|
+
const { fetchOptions, resetFetchOptions } = useFetchOptions()
|
|
66
|
+
|
|
67
|
+
const { localization: usernameLocalization } = useAuthPlugin(usernamePlugin)
|
|
68
|
+
|
|
69
|
+
const [password, setPassword] = useState("")
|
|
70
|
+
|
|
71
|
+
const { mutate: sendVerificationEmail } = useSendVerificationEmail(
|
|
72
|
+
authClient,
|
|
73
|
+
{
|
|
74
|
+
onSuccess: () => toast.success(localization.auth.verificationEmailSent)
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
function isEmail(value: string): boolean {
|
|
79
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const { mutate: signInEmail, isPending: isSignInEmailPending } =
|
|
83
|
+
useSignInEmail(authClient, {
|
|
84
|
+
onError: (error, { email }) => {
|
|
85
|
+
setPassword("")
|
|
86
|
+
|
|
87
|
+
if (error.error?.code === "EMAIL_NOT_VERIFIED") {
|
|
88
|
+
toast.danger(error.error?.message || error.message, {
|
|
89
|
+
actionProps: {
|
|
90
|
+
children: localization.auth.resend,
|
|
91
|
+
onClick: () =>
|
|
92
|
+
sendVerificationEmail({
|
|
93
|
+
email,
|
|
94
|
+
callbackURL: `${baseURL}${redirectTo}`
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
} else {
|
|
99
|
+
toast.danger(error.error?.message || error.message)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
resetFetchOptions()
|
|
103
|
+
},
|
|
104
|
+
onSuccess: () => navigate({ to: redirectTo })
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const { mutate: signInUsername, isPending: isSignInUsernamePending } =
|
|
108
|
+
useSignInUsername(authClient as UsernameAuthClient, {
|
|
109
|
+
onError: (error) => {
|
|
110
|
+
setPassword("")
|
|
111
|
+
toast.danger(error.error?.message || error.message)
|
|
112
|
+
resetFetchOptions()
|
|
113
|
+
},
|
|
114
|
+
onSuccess: () => navigate({ to: redirectTo })
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const handleSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
|
|
118
|
+
e.preventDefault()
|
|
119
|
+
|
|
120
|
+
const formData = new FormData(e.currentTarget)
|
|
121
|
+
const email = formData.get("email") as string
|
|
122
|
+
const rememberMe = formData.get("rememberMe") === "on"
|
|
123
|
+
|
|
124
|
+
if (isEmail(email)) {
|
|
125
|
+
signInEmail({
|
|
126
|
+
email,
|
|
127
|
+
password,
|
|
128
|
+
...(emailAndPassword?.rememberMe ? { rememberMe } : {}),
|
|
129
|
+
fetchOptions
|
|
130
|
+
})
|
|
131
|
+
} else {
|
|
132
|
+
signInUsername({
|
|
133
|
+
username: email,
|
|
134
|
+
password,
|
|
135
|
+
...(emailAndPassword?.rememberMe ? { rememberMe } : {}),
|
|
136
|
+
fetchOptions
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const signInMutating = useIsMutating({
|
|
142
|
+
mutationKey: authMutationKeys.signIn.all
|
|
143
|
+
})
|
|
144
|
+
const signUpMutating = useIsMutating({
|
|
145
|
+
mutationKey: authMutationKeys.signUp.all
|
|
146
|
+
})
|
|
147
|
+
const isPending = signInMutating + signUpMutating > 0
|
|
148
|
+
const isSignInPending = isSignInEmailPending || isSignInUsernamePending
|
|
149
|
+
|
|
150
|
+
const Captcha = plugins.find(
|
|
151
|
+
(plugin) => plugin.captchaComponent
|
|
152
|
+
)?.captchaComponent
|
|
153
|
+
|
|
154
|
+
const showSeparator = emailAndPassword?.enabled && !!socialProviders?.length
|
|
155
|
+
|
|
156
|
+
return (
|
|
157
|
+
<Card
|
|
158
|
+
className={cn("w-full max-w-sm gap-4 md:p-6", className)}
|
|
159
|
+
variant={variant}
|
|
160
|
+
{...props}
|
|
161
|
+
>
|
|
162
|
+
<Card.Header>
|
|
163
|
+
<Card.Title className="text-xl font-semibold mb-1">
|
|
164
|
+
{localization.auth.signIn}
|
|
165
|
+
</Card.Title>
|
|
166
|
+
</Card.Header>
|
|
167
|
+
|
|
168
|
+
<Card.Content className="gap-4">
|
|
169
|
+
{socialPosition === "top" && (
|
|
170
|
+
<>
|
|
171
|
+
{!!socialProviders?.length && (
|
|
172
|
+
<ProviderButtons socialLayout={socialLayout} />
|
|
173
|
+
)}
|
|
174
|
+
|
|
175
|
+
{showSeparator && (
|
|
176
|
+
<FieldSeparator>{localization.auth.or}</FieldSeparator>
|
|
177
|
+
)}
|
|
178
|
+
</>
|
|
179
|
+
)}
|
|
180
|
+
|
|
181
|
+
{emailAndPassword?.enabled && (
|
|
182
|
+
<Form onSubmit={handleSubmit} className="flex flex-col gap-4">
|
|
183
|
+
<TextField
|
|
184
|
+
name="email"
|
|
185
|
+
type="text"
|
|
186
|
+
autoComplete="username email"
|
|
187
|
+
isDisabled={isPending}
|
|
188
|
+
>
|
|
189
|
+
<Label>{usernameLocalization.username}</Label>
|
|
190
|
+
|
|
191
|
+
<Input
|
|
192
|
+
placeholder={usernameLocalization.usernameOrEmailPlaceholder}
|
|
193
|
+
variant={variant === "transparent" ? "primary" : "secondary"}
|
|
194
|
+
required
|
|
195
|
+
/>
|
|
196
|
+
|
|
197
|
+
<FieldError />
|
|
198
|
+
</TextField>
|
|
199
|
+
|
|
200
|
+
<TextField
|
|
201
|
+
minLength={emailAndPassword?.minPasswordLength}
|
|
202
|
+
maxLength={emailAndPassword?.maxPasswordLength}
|
|
203
|
+
name="password"
|
|
204
|
+
type="password"
|
|
205
|
+
autoComplete="current-password"
|
|
206
|
+
isDisabled={isPending}
|
|
207
|
+
value={password}
|
|
208
|
+
onChange={setPassword}
|
|
209
|
+
>
|
|
210
|
+
<Label>{localization.auth.password}</Label>
|
|
211
|
+
|
|
212
|
+
<Input
|
|
213
|
+
placeholder={localization.auth.passwordPlaceholder}
|
|
214
|
+
variant={variant === "transparent" ? "primary" : "secondary"}
|
|
215
|
+
required
|
|
216
|
+
/>
|
|
217
|
+
|
|
218
|
+
<FieldError />
|
|
219
|
+
</TextField>
|
|
220
|
+
|
|
221
|
+
{emailAndPassword?.rememberMe && (
|
|
222
|
+
<Checkbox
|
|
223
|
+
name="rememberMe"
|
|
224
|
+
isDisabled={isPending}
|
|
225
|
+
variant={variant === "transparent" ? "primary" : "secondary"}
|
|
226
|
+
>
|
|
227
|
+
<Checkbox.Control>
|
|
228
|
+
<Checkbox.Indicator />
|
|
229
|
+
</Checkbox.Control>
|
|
230
|
+
|
|
231
|
+
<Checkbox.Content>
|
|
232
|
+
<Label>{localization.auth.rememberMe}</Label>
|
|
233
|
+
</Checkbox.Content>
|
|
234
|
+
</Checkbox>
|
|
235
|
+
)}
|
|
236
|
+
|
|
237
|
+
{Captcha && <div className="flex justify-center">{Captcha}</div>}
|
|
238
|
+
|
|
239
|
+
<div className="flex flex-col gap-3">
|
|
240
|
+
<Button
|
|
241
|
+
type="submit"
|
|
242
|
+
className="w-full"
|
|
243
|
+
isPending={isSignInPending || isPending}
|
|
244
|
+
>
|
|
245
|
+
{isSignInPending && <Spinner color="current" size="sm" />}
|
|
246
|
+
|
|
247
|
+
{localization.auth.signIn}
|
|
248
|
+
</Button>
|
|
249
|
+
|
|
250
|
+
{plugins.flatMap((plugin) =>
|
|
251
|
+
plugin.authButtons?.map((AuthButton, index) => (
|
|
252
|
+
<AuthButton
|
|
253
|
+
key={`${plugin.id}-${index.toString()}`}
|
|
254
|
+
view="signIn"
|
|
255
|
+
/>
|
|
256
|
+
))
|
|
257
|
+
)}
|
|
258
|
+
</div>
|
|
259
|
+
</Form>
|
|
260
|
+
)}
|
|
261
|
+
|
|
262
|
+
{socialPosition === "bottom" && (
|
|
263
|
+
<>
|
|
264
|
+
{showSeparator && (
|
|
265
|
+
<FieldSeparator>{localization.auth.or}</FieldSeparator>
|
|
266
|
+
)}
|
|
267
|
+
|
|
268
|
+
{!!socialProviders?.length && (
|
|
269
|
+
<ProviderButtons socialLayout={socialLayout} />
|
|
270
|
+
)}
|
|
271
|
+
</>
|
|
272
|
+
)}
|
|
273
|
+
</Card.Content>
|
|
274
|
+
|
|
275
|
+
<Card.Footer className="flex-col gap-3">
|
|
276
|
+
{emailAndPassword?.forgotPassword && (
|
|
277
|
+
<Link
|
|
278
|
+
href={`${basePaths.auth}/${viewPaths.auth.forgotPassword}`}
|
|
279
|
+
className="no-underline hover:underline"
|
|
280
|
+
>
|
|
281
|
+
{localization.auth.forgotPasswordLink}
|
|
282
|
+
</Link>
|
|
283
|
+
)}
|
|
284
|
+
|
|
285
|
+
{emailAndPassword?.enabled && (
|
|
286
|
+
<Description className="text-sm">
|
|
287
|
+
{localization.auth.needToCreateAnAccount}{" "}
|
|
288
|
+
<Link
|
|
289
|
+
href={`${basePaths.auth}/${viewPaths.auth.signUp}`}
|
|
290
|
+
className="text-accent no-underline hover:underline decoration-accent-hover"
|
|
291
|
+
>
|
|
292
|
+
{localization.auth.signUp}
|
|
293
|
+
</Link>
|
|
294
|
+
</Description>
|
|
295
|
+
)}
|
|
296
|
+
</Card.Footer>
|
|
297
|
+
</Card>
|
|
298
|
+
)
|
|
299
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type UsernameAuthClient,
|
|
3
|
+
useAuth,
|
|
4
|
+
useAuthPlugin,
|
|
5
|
+
useIsUsernameAvailable
|
|
6
|
+
} from "@better-auth-ui/react"
|
|
7
|
+
import { Check, Xmark } from "@gravity-ui/icons"
|
|
8
|
+
import {
|
|
9
|
+
FieldError,
|
|
10
|
+
InputGroup,
|
|
11
|
+
Label,
|
|
12
|
+
Spinner,
|
|
13
|
+
TextField
|
|
14
|
+
} from "@heroui/react"
|
|
15
|
+
import { useDebouncer } from "@tanstack/react-pacer"
|
|
16
|
+
import { useState } from "react"
|
|
17
|
+
import { usernamePlugin } from "../../../lib/auth/username-plugin"
|
|
18
|
+
import type { AdditionalFieldProps } from "../additional-field"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Renderer for the `username` additional field. Owns availability checking,
|
|
22
|
+
* length limits, and visual indicators. `FieldError` automatically surfaces
|
|
23
|
+
* native validation messages. Availability feedback is shown via the icon and
|
|
24
|
+
* aria-label without affecting the field's invalid state.
|
|
25
|
+
*/
|
|
26
|
+
export function UsernameField({
|
|
27
|
+
name,
|
|
28
|
+
field,
|
|
29
|
+
isPending,
|
|
30
|
+
variant
|
|
31
|
+
}: AdditionalFieldProps) {
|
|
32
|
+
const { authClient } = useAuth()
|
|
33
|
+
const {
|
|
34
|
+
localization,
|
|
35
|
+
minUsernameLength,
|
|
36
|
+
maxUsernameLength,
|
|
37
|
+
isUsernameAvailable: checkAvailability
|
|
38
|
+
} = useAuthPlugin(usernamePlugin)
|
|
39
|
+
|
|
40
|
+
const currentUsername = String(field.defaultValue ?? "")
|
|
41
|
+
const [value, setValue] = useState(currentUsername)
|
|
42
|
+
|
|
43
|
+
const {
|
|
44
|
+
mutate: requestAvailability,
|
|
45
|
+
data: availability,
|
|
46
|
+
error: availabilityError,
|
|
47
|
+
reset: resetAvailability
|
|
48
|
+
} = useIsUsernameAvailable(authClient as UsernameAuthClient, {
|
|
49
|
+
// Bypass global error toast
|
|
50
|
+
onError: () => {}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const debouncer = useDebouncer(
|
|
54
|
+
(next: string) => {
|
|
55
|
+
const trimmed = next.trim()
|
|
56
|
+
// Skip blank input and the user's own current username (profile view).
|
|
57
|
+
if (!trimmed || trimmed === currentUsername) {
|
|
58
|
+
resetAvailability()
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
requestAvailability({ username: trimmed })
|
|
63
|
+
},
|
|
64
|
+
{ wait: 500 }
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
function handleChange(next: string) {
|
|
68
|
+
setValue(next)
|
|
69
|
+
resetAvailability()
|
|
70
|
+
|
|
71
|
+
if (checkAvailability) {
|
|
72
|
+
debouncer.maybeExecute(next)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const isCheckingAvailability =
|
|
77
|
+
!!checkAvailability && !!value.trim() && value.trim() !== currentUsername
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<TextField
|
|
81
|
+
name={name}
|
|
82
|
+
type="text"
|
|
83
|
+
autoComplete="username"
|
|
84
|
+
minLength={minUsernameLength}
|
|
85
|
+
maxLength={maxUsernameLength}
|
|
86
|
+
isDisabled={isPending}
|
|
87
|
+
isReadOnly={field.readOnly}
|
|
88
|
+
value={value}
|
|
89
|
+
onChange={handleChange}
|
|
90
|
+
>
|
|
91
|
+
<Label>{field.label}</Label>
|
|
92
|
+
|
|
93
|
+
<InputGroup variant={variant === "transparent" ? "primary" : "secondary"}>
|
|
94
|
+
<InputGroup.Input
|
|
95
|
+
placeholder={field.placeholder}
|
|
96
|
+
required={field.required}
|
|
97
|
+
/>
|
|
98
|
+
|
|
99
|
+
{isCheckingAvailability && (
|
|
100
|
+
<InputGroup.Suffix
|
|
101
|
+
aria-label={
|
|
102
|
+
availability?.available
|
|
103
|
+
? localization.usernameAvailable
|
|
104
|
+
: availability?.available === false
|
|
105
|
+
? localization.usernameTaken
|
|
106
|
+
: undefined
|
|
107
|
+
}
|
|
108
|
+
className="px-2"
|
|
109
|
+
>
|
|
110
|
+
{availability?.available ? (
|
|
111
|
+
<Check className="text-success" />
|
|
112
|
+
) : availabilityError || availability?.available === false ? (
|
|
113
|
+
<Xmark className="text-danger" />
|
|
114
|
+
) : (
|
|
115
|
+
<Spinner size="sm" color="current" />
|
|
116
|
+
)}
|
|
117
|
+
</InputGroup.Suffix>
|
|
118
|
+
)}
|
|
119
|
+
</InputGroup>
|
|
120
|
+
|
|
121
|
+
<FieldError />
|
|
122
|
+
</TextField>
|
|
123
|
+
)
|
|
124
|
+
}
|
package/src/email.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
export * from "./components/auth/email/email-changed-email"
|
|
4
|
+
export * from "./components/auth/email/email-verification-email"
|
|
5
|
+
export * from "./components/auth/email/magic-link-email"
|
|
6
|
+
export * from "./components/auth/email/new-device-email"
|
|
7
|
+
export * from "./components/auth/email/otp-email"
|
|
8
|
+
export * from "./components/auth/email/password-changed-email"
|
|
9
|
+
export * from "./components/auth/email/reset-password-email"
|
package/src/index.tsx
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
|
+
export * from "./components/auth/additional-field"
|
|
3
4
|
export * from "./components/auth/auth"
|
|
4
5
|
export * from "./components/auth/auth-provider"
|
|
5
6
|
export * from "./components/auth/forgot-password"
|
|
6
|
-
export * from "./components/auth/magic-link"
|
|
7
|
-
export * from "./components/auth/magic-link-button"
|
|
8
|
-
export * from "./components/auth/passkey-button"
|
|
9
7
|
export * from "./components/auth/provider-button"
|
|
10
8
|
export * from "./components/auth/provider-buttons"
|
|
11
9
|
export * from "./components/auth/reset-password"
|
|
10
|
+
export * from "./components/auth/settings/account/account-settings"
|
|
11
|
+
export * from "./components/auth/settings/account/change-email"
|
|
12
|
+
export * from "./components/auth/settings/account/user-profile"
|
|
13
|
+
export * from "./components/auth/settings/security/active-sessions"
|
|
14
|
+
export * from "./components/auth/settings/security/change-password"
|
|
15
|
+
export * from "./components/auth/settings/security/linked-accounts"
|
|
16
|
+
export * from "./components/auth/settings/security/security-settings"
|
|
17
|
+
export * from "./components/auth/settings/settings"
|
|
12
18
|
export * from "./components/auth/sign-in"
|
|
13
19
|
export * from "./components/auth/sign-out"
|
|
14
20
|
export * from "./components/auth/sign-up"
|
|
15
|
-
export * from "./components/
|
|
16
|
-
export * from "./components/
|
|
17
|
-
export * from "./components/
|
|
18
|
-
export * from "./components/email/new-device-email"
|
|
19
|
-
export * from "./components/email/otp-email"
|
|
20
|
-
export * from "./components/email/password-changed-email"
|
|
21
|
-
export * from "./components/email/reset-password-email"
|
|
22
|
-
export * from "./components/settings/account/account-settings"
|
|
23
|
-
export * from "./components/settings/account/appearance"
|
|
24
|
-
export * from "./components/settings/account/change-email"
|
|
25
|
-
export * from "./components/settings/account/manage-account"
|
|
26
|
-
export * from "./components/settings/account/manage-accounts"
|
|
27
|
-
export * from "./components/settings/account/user-profile"
|
|
28
|
-
export * from "./components/settings/security/active-sessions"
|
|
29
|
-
export * from "./components/settings/security/change-password"
|
|
30
|
-
export * from "./components/settings/security/danger-zone"
|
|
31
|
-
export * from "./components/settings/security/delete-user"
|
|
32
|
-
export * from "./components/settings/security/linked-accounts"
|
|
33
|
-
export * from "./components/settings/security/passkeys"
|
|
34
|
-
export * from "./components/settings/security/security-settings"
|
|
35
|
-
export * from "./components/settings/settings"
|
|
36
|
-
export * from "./components/user/user-avatar"
|
|
37
|
-
export * from "./components/user/user-button"
|
|
38
|
-
export * from "./components/user/user-view"
|
|
21
|
+
export * from "./components/auth/user/user-avatar"
|
|
22
|
+
export * from "./components/auth/user/user-button"
|
|
23
|
+
export * from "./components/auth/user/user-view"
|