日本語版はこちらを参照してください。
pnpm install
Before using, replace any values marked with example
with the correct ones.
app.use(async (c, next) => {
const authClient = createAuthClient({
baseURL: 'https://auth.example.com',
})
const result = await authClient.getSession({
fetchOptions: {
headers: c.req.raw.headers,
},
})
if (!result.data) {
return c.json({ error: 'Unauthorized' }, 401)
}
await next()
})
// src/lib/auth.ts
import { createAuthClient } from 'better-auth/react'
import { env } from '@/lib/env.ts'
export const authClient = createAuthClient({
baseURL: 'https://auth.example.com',
})
// src/layouts/auth-layout.tsx
import { Navigate, Outlet } from 'react-router-dom'
import { authClient } from '@/lib/auth.ts'
export const AuthLayout = () => {
const { data: session, isPending, error } = authClient.useSession()
if (isPending) {
return <div>Loading...</div>
}
if (error || !session) {
return <Navigate to="/login" replace />
}
return <Outlet />
}
Dump the DDL for better-auth.
pnpm run better-auth:generate
Display a list of migrations for the local database.
pnpm run wrangler:migrate:local:list
Apply migrations to the local database.
pnpm run wrangler:migrate:local:apply
Display a list of migrations for the remote database.
pnpm run wrangler:migrate:remote:list
Apply migrations to the remote database.
pnpm run wrangler:migrate:remote:apply