Skip to content

feat: integrate better-auth for OIDC authentication on Cloudflare Workers#6

Open
m1ngsama wants to merge 1 commit into💥from
feat/better-auth-oidc
Open

feat: integrate better-auth for OIDC authentication on Cloudflare Workers#6
m1ngsama wants to merge 1 commit into💥from
feat/better-auth-oidc

Conversation

@m1ngsama
Copy link
Copy Markdown
Member

@m1ngsama m1ngsama commented Mar 27, 2026

Closes #5

Summary

  • Adds better-auth v1.5.6 to the Cloudflare Workers backend (Hono + Kysely + D1)
  • Mounts /api/auth/* for full OIDC flow with Logto as the identity provider (PKCE)
  • Guards GET /api/freshman with session/Bearer-token validation — returns 401 for unauthenticated requests
  • POST /api/freshman remains public for the open recruitment form
  • D1 migration (migrations/0001_better_auth.sql) adds user, session, account, verification tables alongside the existing freshman table
  • Enables nodejs_compat Wrangler flag required by better-auth's async-hooks dependency

Changed Files

File Change
src/auth.ts New — createAuth(env) factory using Kysely adapter + bearer + genericOAuth plugins
src/index.ts Mount auth handler; add auth middleware for GET /api/freshman
worker-configuration.d.ts Extend Env with BETTER_AUTH_SECRET, LOGTO_* vars
wrangler.jsonc Add nodejs_compat flag + LOGTO_ISSUER / LOGTO_CALLBACK_URL vars
migrations/0001_better_auth.sql D1 schema for better-auth tables
package.json Add better-auth + @better-auth/kysely-adapter

Deployment Checklist

  • Set Wrangler secrets: BETTER_AUTH_SECRET, LOGTO_CLIENT_ID, LOGTO_CLIENT_SECRET
  • Fill in LOGTO_ISSUER and LOGTO_CALLBACK_URL in wrangler.jsonc
  • Add redirect URI to Logto app: https://<worker>.dev/api/auth/callback/logto
  • Run D1 migration: wrangler d1 execute active --file=./migrations/0001_better_auth.sql

Test Plan

  • GET /api/auth/sign-in/social?provider=logto&callbackURL=/ redirects to Logto login
  • After login, session is created in D1
  • GET /api/freshman without token → 401 Unauthorized
  • GET /api/freshman with Authorization: Bearer <token> → data returned
  • POST /api/freshman without token → accepted (public endpoint)

…kers

Add better-auth (v1.5.6) to protect the admin API with session-based
authentication backed by Cloudflare D1 via the Kysely adapter.

- Mount /api/auth/* handler (sign-in, callback, sign-out, session)
- Guard GET /api/freshman with session validation (Bearer Token + Cookie)
- POST /api/freshman remains public for open recruitment form submissions
- Add genericOAuth plugin wired to Logto as the OIDC provider (PKCE)
- Add bearer plugin to support Authorization header token flow
- Enable nodejs_compat compatibility flag required by better-auth
- Add LOGTO_ISSUER / LOGTO_CALLBACK_URL env vars to wrangler.jsonc
- Extend Env interface with BETTER_AUTH_SECRET, LOGTO_* secrets
- Add D1 migration SQL for better-auth tables (user/session/account/verification)
@m1ngsama m1ngsama requested a review from LazuliKao March 27, 2026 15:30
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
active 476a068 Commit Preview URL

Branch Preview URL
Mar 27 2026, 03:30 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant