Bun.serve
This guide shows how to use gau
with a backend-only Bun server using Bun.serve
.
Bun’s docs: Bun.serve.
-
Follow the basic setup
Section titled “Follow the basic setup”Complete the Getting Started guide for installation, environment variables, and adding at least one OAuth provider.
-
Create your auth configuration
Section titled “Create your auth configuration”Define your
createAuth
configuration.src/auth.ts import process from 'node:process'import { MemoryAdapter } from '@rttnd/gau/adapters/memory'import { createAuth } from '@rttnd/gau/core'import { GitHub } from '@rttnd/gau/oauth'export const auth = createAuth({adapter: MemoryAdapter(),providers: [GitHub({clientId: process.env.AUTH_GITHUB_ID!,clientSecret: process.env.AUTH_GITHUB_SECRET!,}),],jwt: {secret: process.env.AUTH_SECRET!,},// When frontend runs on a different origin, set the trusted hosts, example:// trustHosts: ['http://localhost:5173'],})export type Auth = typeof auth -
Start a Bun.serve HTTP server
Section titled “Start a Bun.serve HTTP server”createHandler(auth)
returns a standard Fetch handler. You can use it directly insideBun.serve
.server.ts import { auth } from './src/auth'import { createHandler } from '@rttnd/gau/core'const handler = createHandler(auth)const server = Bun.serve({routes: {'/api/auth/*': req => handler(req),'/': new Response('OK'),},})console.log(`Server listening on ${server.url.hostname}:${server.url.port}`) -
Try it
Section titled “Try it”Look at the Bun Example.
-
File Structure (example)
Section titled “File Structure (example)”Directorysrc
- auth.ts
- index.ts
- package.json
- By default, sessions use cookies when possible. If
redirectTo
points to a different origin (or a custom scheme for native apps),gau
automatically returns the session token in the URL hash. - You can force strategies via
session.strategy: 'cookie' | 'token'
increateAuth
.