Docs

Elysia Integration

This integration guide is assuming you are using Elysia with bun server.

Before you start, make sure you have a Better Auth instance configured. If you haven't done that yet, check out the installation.

Mount the handler

We need to mount the handler to elysia endpoint.

import { Elysia } from "elysia";
import { auth } from "./auth";
 
const betterAuthView = (context: Context) => {
  const BETTER_AUTH_ACCEPT_METHODS = ["POST", "GET"]
  // validate request method
  if(BETTER_AUTH_ACCEPT_METHODS.includes(context.request.method)) {
    return auth.handler(context.request);
  }
  else {
    context.error(405)
  }
}
 
const app = new Elysia().all("/api/auth/*", betterAuthView).listen(3000);
 
console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

Middleware

You can use derive to provide session and user information before pass to view.

import { Elysia } from "elysia";
import { auth } from "./auth";
 
// user middleware (compute user and session and pass to routes)
const userMiddleware = async (request: Request) => {
  const session = await auth.api.getSession({ headers: request.headers });
 
  if (!session) {
    return {
      user: null,
      session: null
    }
  }
 
  return {
    user: session.user,
    session: session.session
  }
}
 
// user info view
// type User can be export from `typeof auth.$Infer.Session.user`
// type Session can be export from `typeof auth.$Infer.Session.session`
const userInfo = (user: User | null, session: Session | null) => {
  return {
    user: user,
    session: session
  }
}
 
const app = new Elysia()
  .derive(({ request }) => userMiddleware(request))
  // about betterAuthView code, please check `Mount the handler` topic 
  .all("/api/auth/*", betterAuthView)
  // Example custom endpoint
  .get("/user", ({ user, session }) => userInfo(user, session))
  .listen(3000);
 
console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

This will allow you to access the user and session object in all of your routes.

On this page

Edit on GitHub