payload-auth
Reference

Exports

Every entry point the payload-auth package exposes, and what lives behind it.

Entry points

Import pathContents
payload-authEverything below re-exported
payload-auth/better-authPlugin, adapter and types — the usual import
payload-auth/better-auth/pluginPlugin only
payload-auth/better-auth/adapterAdapter only
payload-auth/better-auth/plugin/clientClient components ('use client')
payload-auth/better-auth/plugin/rscReact Server Components and views
payload-auth/shared/payload/fieldsReusable Payload field components

payload-auth/better-auth

betterAuthPlugin(options)

function betterAuthPlugin(pluginOptions: PayloadAuthOptions): (config: Config) => Config

The Payload plugin. See Plugin options.

getPayloadAuth(config)

function getPayloadAuth<O extends PayloadAuthOptions>(
  config: Promise<SanitizedConfig> | SanitizedConfig,
): Promise<BasePayload & { betterAuth: BetterAuthReturn<O> }>

Payload's getPayload with the Better Auth instance attached and typed. Throws if the plugin is not installed.

import configPromise from '@payload-config'
import { getPayloadAuth } from 'payload-auth/better-auth'
import type { PayloadAuthConfig } from './auth/options'

const payload = await getPayloadAuth<PayloadAuthConfig>(configPromise)

generateVerifyEmailUrl(options)

function generateVerifyEmailUrl(options: {
  userEmail: string
  secret: string
  verifyRouteUrl: string
  callbackURL?: string   // default '/'
  expiresIn?: number     // seconds, default 3600
}): Promise<string>

Signs a JWT containing the email address and builds a verification URL. Useful for seeding or for custom verification flows. The callbackURL is passed through a safe-redirect check before being appended.

sanitizeBetterAuthOptions(...)

The internal function that rewrites PayloadAuthOptions into resolved BetterAuthOptions. Exported for inspection and testing; you rarely call it directly.

Types

import type {
  BetterAuthOptions,
  BetterAuthReturn,
  PayloadAuthOptions,
  LoginMethod,
  SocialProvider,
  SendAdminInviteEmailFn,
  GenerateAdminInviteUrlFn,
  PayloadRequestWithBetterAuth,
  CollectionHookWithBetterAuth,
  EndpointWithBetterAuth,
} from 'payload-auth/better-auth'
TypePurpose
PayloadAuthOptionsThe plugin's options object
BetterAuthOptionsBetter Auth's options with database removed
BetterAuthReturn<O>The instance on payload.betterAuth: handler, api, options, $Infer, $ERROR_CODES, $context
LoginMethodUnion of valid admin.loginMethods values
SocialProviderUnion of providers with a built-in admin button
PayloadRequestWithBetterAuth<O>PayloadRequest whose payload carries betterAuth
CollectionHookWithBetterAuth<O, T>Type a Payload hook so req.payload.betterAuth is available
EndpointWithBetterAuth<O>Type a Payload endpoint the same way

Typing a custom hook:

import type {
  CollectionHookWithBetterAuth,
  PayloadAuthOptions,
} from 'payload-auth/better-auth'
import type { CollectionAfterChangeHook } from 'payload'
import type { PayloadAuthConfig } from '@/lib/auth/options'

type AfterChange = CollectionHookWithBetterAuth<
  PayloadAuthConfig,
  CollectionAfterChangeHook
>

export const onUserChange: AfterChange = async ({ doc, req }) => {
  const session = await req.payload.betterAuth.api.getSession({ headers: req.headers })
  // …
  return doc
}

payload-auth/better-auth/plugin/client

Client components, all marked 'use client'.

ExportPurpose
AdminButtonsImpersonate / revoke sessions / ban / unban, for the user edit view
AdminInviteButtonGenerates an admin invitation link
LogoutButtonEnds the Better Auth session
CredentialsFormEmail (or username) and password form
AlternativeMethodsSocial and passwordless buttons
LoginFormProviderContext provider for the login form pieces
useLoginFormHook exposing the login form state
TwoFactorAuth2FA enrolment: QR code, verification, backup codes

payload-auth/better-auth/plugin/rsc

Server components and admin views.

ExportPurpose
AdminLoginThe /admin/login view
AdminSignupThe /admin/signup view
ForgotPasswordThe /admin/forgot-password view
ResetPasswordThe /admin/reset-password view
TwoFactorVerifyThe /admin/two-factor-verify view
PasskeysPasskey management list
RSCRedirectRedirect helper used by the login flow

payload-auth/shared/payload/fields

Field components you can attach to your own collections:

ExportPurpose
FieldCopyButtonCopy-to-clipboard button beside a field
GenerateUuidButtonFill a text field with a fresh UUID
{
  name: 'token',
  type: 'text',
  admin: {
    components: {
      afterInput: [{ path: 'payload-auth/shared/payload/fields#GenerateUuidButton' }],
    },
  },
}

payload-auth/better-auth/adapter

ExportPurpose
payloadAdapterThe Better Auth DBAdapter implementation
generateSchemaWrite Payload collection configs from Better Auth options

See Adapter.

On this page