feat: integrate better-auth for OIDC authentication on Cloudflare Workers#6
Open
feat: integrate better-auth for OIDC authentication on Cloudflare Workers#6
Conversation
…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)
Deploying with
|
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5
Summary
/api/auth/*for full OIDC flow with Logto as the identity provider (PKCE)GET /api/freshmanwith session/Bearer-token validation — returns401for unauthenticated requestsPOST /api/freshmanremains public for the open recruitment formmigrations/0001_better_auth.sql) addsuser,session,account,verificationtables alongside the existingfreshmantablenodejs_compatWrangler flag required by better-auth's async-hooks dependencyChanged Files
src/auth.tscreateAuth(env)factory using Kysely adapter +bearer+genericOAuthpluginssrc/index.tsGET /api/freshmanworker-configuration.d.tsEnvwithBETTER_AUTH_SECRET,LOGTO_*varswrangler.jsoncnodejs_compatflag +LOGTO_ISSUER/LOGTO_CALLBACK_URLvarsmigrations/0001_better_auth.sqlpackage.jsonbetter-auth+@better-auth/kysely-adapterDeployment Checklist
BETTER_AUTH_SECRET,LOGTO_CLIENT_ID,LOGTO_CLIENT_SECRETLOGTO_ISSUERandLOGTO_CALLBACK_URLinwrangler.jsonchttps://<worker>.dev/api/auth/callback/logtowrangler d1 execute active --file=./migrations/0001_better_auth.sqlTest Plan
GET /api/auth/sign-in/social?provider=logto&callbackURL=/redirects to Logto loginGET /api/freshmanwithout token →401 UnauthorizedGET /api/freshmanwithAuthorization: Bearer <token>→ data returnedPOST /api/freshmanwithout token → accepted (public endpoint)