Point0by 1gr14
  • Меню
    • GitHub
    • Блог
  • Introduction
    • Overview
    • Getting Started
    • Full Overview
    • Benchmarks
    • Points
  • Points
    • Page
    • Layout
    • Component
    • Provider
    • Mountable
    • Query
    • Infinite Query
    • Mutation
    • Action
    • Root
    • Base
    • Plugin
  • Methods
    • Validation
    • Loader
    • Context
    • Middleware
    • Loading & error
    • .with
    • Mapper
    • Transformer
    • Stage Methods
  • Core
    • Navigation
    • SSR
    • RSC
    • Request
    • Response
    • Error handling
    • Env
    • Head
    • MDX
    • Assets
    • File upload
    • OpenAPI
    • Query client
    • Events
    • Infer
  • Engine
    • Engine Config
    • Engine Runtime
    • CLI
    • Dev
    • Build
    • Compiler
    • Generator
    • Project MCP
    • Docs MCP
    • Importer
    • Public dir
    • Testing
    • Deploy
    • Bun or Vite
  • Extra
    • SsrStore
    • CookieStore
    • Basic Auth
    • CORS
    • Cache-Control
    • Compress
  • Examples
    • Basic
    • Vite
    • Better Auth
    • Capacitor
    • Expo
  • 1gr14
    • Главная
    • Start0
    • Поддержать
    • Обучение
    • Группа
    • Блог
    • Автор
  • Сообщество
    • Discord
    • Telegram
  • Опенсорс
    • Point0
    • Route0
    • Error0
    • Flat
  • Аккаунт
    • Войти
    • Регистрация
Point0by 1gr14
Создаю опенсорс во славу Господа Иисуса Христа ☦️
С любовью ко всем разработчикам на свете ❤️
Условия использованияПолитика конфиденциальностиСергей Дмитриев 2026 😎

Better Auth Example

  • Раздел: Examples

On this page

  1. Running it
  2. For a real app
  3. What this example adds over basic
GitHub DevGitHub Source

examples/better-auth is the same app as basic — the IdeaNick ideas board — plus authentication through Better Auth: sign-in/up, a profile page, protected pages and mutations, and an error class carrying UNAUTHORIZED / FORBIDDEN codes. Everything else is unchanged from basic. The buttons above and below open the full source.

Two things make this the distinctive example. First, Better Auth's own router is mounted as a single middleware on the root — middlewares run server-side only, so the handler body never reaches the browser bundle:

// examples/better-auth/src/lib/root.tsx
.middleware('/api/auth/*', async ({ request }) => await authServer.handler(request.original))

That one mount hands every /api/auth/* request to Better Auth's handler, so its whole endpoint set — sign-in/email, sign-up/email, get-session, and the rest — is served without you declaring any of them. The full list is owned by Better Auth, not this repo: see its API reference.

Second, gating is a plugin. A plugin bundles .ctx (server) and .with (render, client + SSR), so one .use(...) protects a point on both sides:

// examples/better-auth/src/lib/auth/plugins.ts
export const authorizedOnlyPlugin = Point0.lets
  .plugin('authorizedOnly')
  .use(mePlugin) // puts the resolved user into ctx + props
  .ctx(({ ctx: { me } }) => {
    if (!me)
      throw new AppError('Only for authorized users', { code: 'UNAUTHORIZED' })
    return { me } // narrowed to non-null
  })
  .with(({ props: { me } }) => {
    if (!me)
      return new AppError('Only for authorized users', { code: 'UNAUTHORIZED' }) // return, don't throw
    return { me }
  })
  .plugin()

Note the asymmetry: .ctx throws the error (rejecting the server load), .with returns it (short-circuiting to the error component). The example ships four such plugins — mePlugin (read the user), authorizedOnlyPlugin and redirectUnauthorizedPlugin (gate anonymous visitors by error or redirect), and redirectAuthorizedPlugin (keep signed-in users off the sign-in page). Gating a point is then one .use(authorizedOnlyPlugin); a protected mutation reads ctx.me and enforces ownership server-side, while the matching page re-checks on the client for UX.

The error class adds a code field (here via Error0's code plugin, marked public so it survives serialization), and the error component branches on it — UNAUTHORIZED renders a sign-in link, FORBIDDEN shows the message. See .middleware, .with, Plugin, Env, and Error handling for the mechanics.

Running it

Identical to basic — bun install && bun run build at the repo root, then bun run setup && bun run dev inside examples/better-auth. The seed creates a user through Better Auth itself (authServer.api.signUpEmail with x@example.com / 12345678) and prefills the sign-in form with those credentials. See getting-started.

For a real app

This example shows auth in isolation. For a real product, start from Start0 — it ships this auth setup with social providers and more, alongside admin, forms, CRUD, email, and deploy (bun create start0@latest my-app).

What this example adds over basic

Areabasicbetter-auth
Root middlewareOpenAPI only/api/auth/* Better Auth handler + OpenAPI
src/lib/auth/—server.ts, client.ts, api.ts, plugins.ts
Prisma modelsIdea, IdeaNewsPostadds User, Session, Account, Verification
Create / updateone open idea-create-update.tsxsplit idea-create.tsx + idea-update.tsx, gated
ErrorsgenericUNAUTHORIZED / FORBIDDEN codes drive the error UI
GitHub DevGitHub Source
НазадVite ExampleДалееCapacitor Example

Enjoying Point0?

Give it a star on GitHub, ask questions, and follow what's new

Star Point0

A star on GitHub helps more developers find Point0
Star on GitHub

Start0

Point0 integrated into the Start0 SaaS boilerplate — the best way to use it
Explore Start0

YouTube

Tutorials, walkthroughs, and updates
YouTube

Discord

Questions and chat in English
Join Discord

Telegram

News channel and chat in Russian
ChannelChat

Twitter

Posts and updates
Twitter