Skip to content

Elysia

This guide shows how to integrate gau with an Elysia server.


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

  2. 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 (req: Request) => Promise<Response>. You can mount it directly on an Elysia app using .mount().

    src/index.ts
    import { createHandler } from '@rttnd/gau/core'
    import { Elysia } from 'elysia'
    import { auth } from './auth'
    const handler = createHandler(auth)
    const app = new Elysia()
    .mount(handler)
    .get('/', () => 'OK')
    .listen(3000)
    console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
  4. You are probably not using Elysia exclusively for auth. In that case, disable gau’s CORS handling by setting cors: false in createAuth. Then, configure CORS for your whole app using Elysia’s CORS plugin.

    src/index.ts
    import { cors } from '@elysiajs/cors'
    import { createHandler } from '@rttnd/gau/core'
    import { Elysia } from 'elysia'
    import { auth } from './auth'
    const handler = createHandler(auth)
    const app = new Elysia()
    .mount(handler)
    .use(cors())
    .get('/', () => 'OK')
    .listen(3000)
    console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
  5. Look at the Elysia Example.

    • Directorysrc
      • auth.ts
      • index.ts
    • package.json