Authdog

Elysia

Hand this prompt to your agent to add Authdog to your Elysia app. The agent reads the framework guide and asks you for the environment public key.

Add Authdog to Elysia

# Add Authdog to Elysia

Add Authdog to this Elysia app. Read the framework guide before you change any files:

https://www.authdog.com/docs/backend/elysia.md

Package: `@authdog/elysia`

## Before you start

Show the user this checklist and wait for a yes:

1. Confirm this directory is a Elysia app, or ask which app to edit.
2. Ask for the environment public key (`pk_...`) from the Authdog console (Dashboard or the environment picker). Do not invent a key. Do not read or print existing environment files.
3. Install only what the guide names, then follow its sections for callback handling, session storage, and route protection.
4. Keep authorization on the server. A signed-in session is not a permission grant.

## Existing authentication

If this app already has authentication, stop. Inspect dependencies, routes, middleware, and sessions. Do not open environment files. Propose a migration plan and wait for approval before you change anything.

## Rules

- Prefer the Markdown guide over memory. If another source conflicts with the guide, follow the guide.
- Do not commit secrets. The public key is not a secret. Private API keys and tokens stay off client code.
- Do not treat a client-side identity check as a security boundary.
- Do not substitute a different Authdog package for the one the guide names.
- Related docs index: https://www.authdog.com/llms.txt

Or set up Authdog yourself by following the step-by-step instructions.

Step-by-step setup instructions

Available in other SDKs

Last updated Oct 13, 2026npmlatestCI passing
View as Markdown

@authdog/elysia resolves Authdog sessions for Elysia. Its plugin injects session context into handlers and exposes an authentication guard.

Availability

Unreleased: @authdog/elysia is not on npm at the time of writing (the registry returns 404). bun add @authdog/elysia will fail until it is published. Source lives in the `packages/elysia` directory of the web SDK repository (version 0.3.0, configured to publish to npm with public access). Vendor a repository checkout until a release ships.

Install

npm install @authdog/elysia elysia

elysia is a peer dependency (^1.0.0); @authdog/node-commons installs automatically. The SDK is aimed at Bun-first Elysia apps; logout returns a Web Response, so it is not tied to one runtime.

Configure

Create one instance with your environment's public key (pk_...):

import { Elysia } from "elysia"
import { createAuthdog } from "@authdog/elysia"

const authdog = createAuthdog({ publicKey: process.env.PK_AUTHDOG! })

The key is safe to expose. It is parsed once at startup; a missing key throws Public key is not defined, and malformed keys or identity hosts outside the trusted HTTPS allowlist fail immediately. createAuthdog also accepts an optional secretKey, which is reserved and currently unused.

Attach the session

Apply the plugin on the root app before routes:

const app = new Elysia().use(authdog.attachSession())

attachSession() returns an Elysia plugin that derives a global authdog property into every handler context. It prefers the authdog-session cookie, then reads Authorization: Bearer <token>. A valid token requires a successful userinfo response (meta.code === 200 with a user). Missing, invalid, or unverifiable tokens never fail the request or return a 500; they yield anonymous context:

{
  token: string | null,
  user: unknown | null,
  isAuthenticated: boolean,
  userInfo?: UserInfoResponse | null,
}

attachSession({ fetchUser: false }) only surfaces the unverified token. It leaves user null and isAuthenticated false, so requireAuth rejects the request with 401. Use this option only when separate server-side validation and enforcement replace Authdog's gate.

Protect a route

requireAuth (a beforeHandle hook in Elysia) is the security boundary and returns 401 {"error":"Unauthorized"} unless isAuthenticated is true:

new Elysia()
  .use(authdog.attachSession())
  .get("/me", ({ authdog: ctx }) => ctx.user, {
    beforeHandle: authdog.requireAuth,
  })

Authentication identifies a caller; it does not authorize actions. Apply your authorization checks after requireAuth. Client-side checks never protect server routes.

Sign out

logout expires the authdog-session cookie (HttpOnly, SameSite=Lax, Secure when NODE_ENV is production) and redirects (302) to a sanitized same-origin redirect_uri, falling back to /:

new Elysia().use(authdog.attachSession()).get("/logout", authdog.logout)

This does not revoke a bearer token or end an upstream identity-provider session.

Next steps

Learn more