Skip to content

Bun.serve

This guide shows how to use gau with a backend-only Bun server using Bun.serve.

Bun’s docs: Bun.serve.


  1. Complete the Getting Started guide for installation, environment variables, and adding at least one OAuth provider.

  2. 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
  3. createHandler(auth) returns a standard Fetch handler. You can use it directly inside Bun.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}`)
  4. Look at the Bun 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' in createAuth.