@better-auth-ui/core
Version:
Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.
164 lines (163 loc) • 6.21 kB
TypeScript
import { AuthClient } from '../lib/auth-client.js';
import { AuthLocale } from '../lib/auth-locale.js';
import { AuthPlugin } from '../lib/auth-plugin.js';
import { BasePaths } from '../lib/base-paths.js';
import { DeepPartial } from '../lib/deep-partial.js';
import { Localization } from '../lib/localization.js';
import { AuthView, ViewPaths } from '../lib/view-paths.js';
import { AdditionalFields } from './additional-fields-config.js';
import { AvatarConfig } from './avatar-config.js';
import { EmailAndPasswordConfig } from './email-and-password-config.js';
import { ProfileConfig } from './profile-config.js';
import { AuthSocialProvider } from './social-provider-config.js';
export type SocialSignInMode = "redirect" | "popup";
/**
* Options passed to {@link AuthConfig.navigate}.
*
* `to` is the composed path used by path-based routers (expo-router, web,
* TanStack, Next). `view` and `params` are additive, optional hints used by
* name-based routers (React Navigation) and state-only hosting (the React
* Native state adapter) that have no URL to read — path routers can ignore
* them, so this stays backward compatible with every existing web adapter.
*/
export interface NavigateOptions {
/** Composed path — used by path-based routers (expo-router, web). */
to: string;
/**
* Semantic view key — used by name-based routers and state-only hosting
* that navigate by identity rather than by URL.
* @remarks `AuthView`
*/
view?: AuthView;
/**
* Route params carried alongside the view (e.g. deep-link tokens such as a
* password-reset `token`) for routers that don't parse them from a URL.
*/
params?: Record<string, string>;
/** Replace the current history entry instead of pushing a new one. */
replace?: boolean;
}
/** Navigation callback supplied by the host router. */
export type NavigateFn = (options: NavigateOptions) => void;
/**
* Core authentication configuration interface.
*
* Defines the base structure for authentication settings including paths,
* providers, navigation functions, and feature flags.
*/
export interface AuthConfig<TAuthClient extends AuthClient = AuthClient> {
/**
* The Better Auth client instance used for authentication operations.
*/
authClient: TAuthClient;
/**
* Additional user fields rendered on sign-up and the user profile.
* @remarks `AdditionalFields`
*/
additionalFields?: AdditionalFields;
/**
* Avatar upload, optimization, and deletion configuration.
* @remarks `AvatarConfig`
* @default { enabled: true, resize: resizeAvatar, size: 256, extension: "png" }
*/
avatar: AvatarConfig;
/**
* Base paths for different application sections
* @remarks `BasePaths`
*/
basePaths: BasePaths;
/**
* Base URL for API endpoints (optional)
* @default ""
*/
baseURL: string;
/**
* Email and password authentication configuration
* @remarks `EmailAndPasswordConfig`
*/
emailAndPassword: EmailAndPasswordConfig;
/**
* Localization strings for UI components
* @remarks `Localization`
*/
localization: Localization;
/**
* Language metadata and translated messages for all auth components.
* @remarks `AuthLocale`
* @default `defaultAuthLocale`
*/
locale: AuthLocale;
/**
* Registered auth plugins. UI packages widen the element type via the
* `AuthPluginRegister` module-augmentation slot.
* @remarks `AuthPlugin[]`
* @default []
*/
plugins: AuthPlugin[];
/**
* Account profile editing configuration.
* @remarks `ProfileConfig`
* @default { name: true }
*/
profile: ProfileConfig;
/**
* Default redirect path after successful authentication
* @default "/"
*/
redirectTo: string;
/**
* Allow users to link multiple accounts from the same social provider.
* When false, providers already linked to the account are hidden from the available-to-link list.
* @default true
*/
multipleAccountsPerProvider?: boolean;
/**
* Allow the UI to unlink the final sign-in account.
*
* Enable this only when Better Auth's
* `account.accountLinking.allowUnlinkingAll` option is also enabled.
* @default false
*/
allowUnlinkingAllAccounts?: boolean;
/**
* List of enabled social authentication providers
* Built-in providers can use their string ID. Custom and Generic OAuth
* providers use `{ id, label, icon }`.
* @remarks `AuthSocialProvider[]`
*/
socialProviders?: AuthSocialProvider[];
/**
* How social sign-in opens the provider flow.
* Popup mode requires Better Auth's experimental `oauthPopupClient` action.
* @default "redirect"
*/
socialSignInMode: SocialSignInMode;
/**
* View path mappings for different authentication views
* @remarks `ViewPaths`
*/
viewPaths: ViewPaths;
/**
* Function to navigate between auth views.
*
* Receives the composed `to` path plus optional additive `view`/`params`
* hints for name-based / state-only routers (see {@link NavigateOptions}).
* Path-based routers can read `to` and ignore the rest.
* @remarks `NavigateFn`
* @default window.location.href = to (or window.location.replace if replace: true)
* @example
* // TanStack Router
* navigate={navigate}
* // Next.js
* navigate={({ to, replace }) => replace ? router.replace(to) : router.push(to)}
*/
navigate: NavigateFn;
}
export declare const defaultAuthConfig: Omit<AuthConfig, "authClient">;
export type AuthConfigOptions<TAuthClient extends AuthClient = AuthClient> = DeepPartial<Omit<AuthConfig<TAuthClient>, "authClient" | "locale">> & {
authClient: TAuthClient;
/** Imported locale bundle. Defaults to English (`en-US`). */
locale?: AuthLocale;
};
/** Resolves defaults, locale messages, plugin messages, and consumer overrides. */
export declare function resolveAuthConfig<TAuthClient extends AuthClient = AuthClient>(options: AuthConfigOptions<TAuthClient>): AuthConfig<TAuthClient>;