5cbd74c5 days ago37 lines · 1.2 KB| 1 | import { createAuthPlugin |
| 2 | import { |
| 3 | usernamePlugin as coreUsernamePlugin, |
| 4 | type UsernamePluginOptions, |
| 5 | } from "@better-auth-ui/core/plugins/username"; |
| 6 |
| 7 | import { SignInUsername } from "@/components/auth/username/sign-in-username"; |
| 8 | import { UsernameField } from "@/components/auth/username/username-field"; |
| 9 |
| 10 | export const usernamePlugin = createAuthPlugin( |
| 11 | coreUsernamePlugin.id, |
| 12 | (options: UsernamePluginOptions = {}) => { |
| 13 | const core = coreUsernamePlugin(options); |
| 14 | const withRenderer = (fields: typeof core.additionalFields) => |
| 15 | fields?.map((field) => |
| 16 | field.name === "username" ? { ...field, render: UsernameField } : field, |
| 17 | ); |
| 18 |
| 19 | return { |
| 20 | ...core, |
| 21 | additionalFields: withRenderer(core.additionalFields), |
| 22 | // The core resolver rebuilds the fields from the localization once the config resolves, dropping `render`. |
| 23 | _localizationResolver: ( |
| 24 | ...args: Parameters<typeof core._localizationResolver> |
| 25 | ) => { |
| 26 | const resolved = core._localizationResolver(...args); |
| 27 | return { |
| 28 | ...resolved, |
| 29 | additionalFields: withRenderer(resolved.additionalFields), |
| 30 | }; |
| 31 | }, |
| 32 | views: { |
| 33 | auth: { signIn: SignInUsername }, |
| 34 | }, |
| 35 | }; |
| 36 | }, |
| 37 | ); |