Elysia
This guide shows how to integrate gau
with an Elysia server.
-
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”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 -
Mount the fetch handler
Section titled “Mount the fetch handler”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}`) -
Configure CORS
Section titled “Configure CORS”You are probably not using Elysia exclusively for auth. In that case, disable
gau
’s CORS handling by settingcors: false
increateAuth
. 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}`) -
Try it
Section titled “Try it”Look at the Elysia Example.
-
File Structure (example)
Section titled “File Structure (example)”Directorysrc
- auth.ts
- index.ts
- package.json