[PR #7339] [CLOSED] feat: add Effect/Platform native integration package #24142

Closed
opened 2026-04-15 22:11:55 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7339
Author: @dallenpyrah
Created: 1/13/2026
Status: ❌ Closed

Base: canary ← Head: feat/effect-platform-integration


📝 Commits (1)

  • f735441 feat: add Effect/Platform native integration package

📊 Changes

13 files changed (+3229 additions, -0 deletions)

View changed files

➕ EFFECT_PLATFORM_INTEGRATION_PLAN.md (+913 -0)
➕ docs/content/docs/integrations/effect-platform.mdx (+505 -0)
➕ packages/effect/README.md (+375 -0)
➕ packages/effect/package.json (+74 -0)
➕ packages/effect/src/BetterAuth.ts (+388 -0)
➕ packages/effect/src/Errors.ts (+129 -0)
➕ packages/effect/src/HttpApi.ts (+375 -0)
➕ packages/effect/src/Session.ts (+180 -0)
➕ packages/effect/src/index.ts (+122 -0)
➕ packages/effect/test/BetterAuth.test.ts (+137 -0)
➕ packages/effect/tsconfig.json (+10 -0)
➕ packages/effect/tsdown.config.ts (+13 -0)
➕ packages/effect/vitest.config.ts (+8 -0)

📄 Description

Summary

This PR adds an official Effect-native integration for Better Auth with Effect/Platform's HttpApi builder.

Related Issue

Closes #7338

What's Included

New Package: @better-auth/effect

A dedicated package that provides Effect-native auth operations:

1. Effect-Wrapped Auth Service

import { BetterAuth } from "@better-auth/effect"
import { Effect } from "effect"

const program = Effect.gen(function* () {
  const auth = yield* BetterAuth
  
  // All operations return Effects with typed errors
  const { user, session } = yield* auth.signInEmail({
    email: "user@example.com",
    password: "password",
  })
})

2. HttpApi Middleware Integration

import { HttpApiGroup, HttpApiEndpoint } from "@effect/platform"
import { AuthMiddleware, CurrentUser } from "@better-auth/effect"

class UsersApi extends HttpApiGroup.make("users")
  .add(HttpApiEndpoint.get("me", "/me").addSuccess(UserProfile))
  .middleware(AuthMiddleware)  // Type-safe auth middleware
{}

// In handler - CurrentUser is automatically available
const handler = Effect.gen(function* () {
  const user = yield* CurrentUser
  return new UserProfile(user)
})

3. Typed Errors

import { Unauthorized, InvalidCredentials, RateLimited } from "@better-auth/effect"

// Errors are Effect Schema classes with proper HTTP status codes
// Handle them with Effect's error handling
program.pipe(
  Effect.catchTag("Unauthorized", (e) => ...),
  Effect.catchTag("InvalidCredentials", (e) => ...)
)

4. Session Context Tags

import { CurrentUser, CurrentSession, AuthSession, OptionalUser } from "@better-auth/effect"

// Access typed session data anywhere in your Effect chain
const user = yield* CurrentUser
const session = yield* CurrentSession
const { user, session } = yield* AuthSession
const maybeUser = yield* OptionalUser  // For optional auth routes

Package Structure

packages/effect/
├── src/
│   ├── index.ts         # Main exports
│   ├── BetterAuth.ts    # Service implementation
│   ├── Session.ts       # Session context tags
│   ├── Errors.ts        # Typed error classes
│   └── HttpApi.ts       # HttpApi middleware
├── test/
│   └── BetterAuth.test.ts
├── package.json
├── README.md
└── tsconfig.json

Documentation

Added new integration guide at docs/content/docs/integrations/effect-platform.mdx covering:

  • Quick start guide
  • Authentication middleware patterns
  • Using the BetterAuth service directly
  • Error handling
  • Full example application

Benefits

  • Type Safety: Full type inference from better-auth config to handlers
  • Error Handling: Typed errors that integrate with Effect's error channel
  • Dependency Injection: Uses Effect's Context/Layer for clean DI
  • Composability: Middleware composes naturally with HttpApi
  • Non-Breaking: New package, doesn't affect existing users

Checklist

  • New package scaffolded
  • BetterAuth service with Effect-wrapped operations
  • HttpApi middleware (AuthMiddleware, SessionMiddleware, etc.)
  • Typed error classes
  • Session context tags
  • Basic tests
  • README documentation
  • Integration guide in docs
  • Additional tests (can add more in follow-up)

Testing

The package includes basic unit tests for:

  • Error class structure
  • Context tag identifiers
  • Effect integration patterns

More comprehensive integration tests can be added in a follow-up PR once the API is finalized.


This PR addresses the growing demand for Effect integration (see #7234 and #7338) by providing a first-class Effect-native experience for Better Auth users.


Summary by cubic

Adds a native Effect/Platform integration for Better Auth via the new @better-auth/effect package. This enables type-safe auth flows with Effect, HttpApi middleware, and easy handler mounting, fulfilling the integration requested in #7338.

  • New Features
    • New package: @better-auth/effect with an Effect-wrapped BetterAuth service (all operations return Effects with typed errors).
    • HttpApi middleware: AuthMiddleware, SessionMiddleware, FullAuthMiddleware, OptionalAuthMiddleware with cookie-based security.
    • Session context tags: CurrentUser, CurrentSession, AuthSession, OptionalUser, plus makeSessionContext for precise typing.
    • Handler mounting helpers: betterAuthApp, betterAuthAppFrom, and mountBetterAuth for routing.
    • Typed error classes mapped to HTTP status codes (Unauthorized, InvalidCredentials, UserNotFound, EmailAlreadyExists, RateLimited, etc.).
    • Documentation and examples added; basic tests for errors and context tags.

Written for commit f735441ad2. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7339 **Author:** [@dallenpyrah](https://github.com/dallenpyrah) **Created:** 1/13/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/effect-platform-integration` --- ### 📝 Commits (1) - [`f735441`](https://github.com/better-auth/better-auth/commit/f735441ad2425bfd8b823aa582718ae5931e8ce2) feat: add Effect/Platform native integration package ### 📊 Changes **13 files changed** (+3229 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `EFFECT_PLATFORM_INTEGRATION_PLAN.md` (+913 -0) ➕ `docs/content/docs/integrations/effect-platform.mdx` (+505 -0) ➕ `packages/effect/README.md` (+375 -0) ➕ `packages/effect/package.json` (+74 -0) ➕ `packages/effect/src/BetterAuth.ts` (+388 -0) ➕ `packages/effect/src/Errors.ts` (+129 -0) ➕ `packages/effect/src/HttpApi.ts` (+375 -0) ➕ `packages/effect/src/Session.ts` (+180 -0) ➕ `packages/effect/src/index.ts` (+122 -0) ➕ `packages/effect/test/BetterAuth.test.ts` (+137 -0) ➕ `packages/effect/tsconfig.json` (+10 -0) ➕ `packages/effect/tsdown.config.ts` (+13 -0) ➕ `packages/effect/vitest.config.ts` (+8 -0) </details> ### 📄 Description ## Summary This PR adds an official **Effect-native** integration for Better Auth with Effect/Platform's HttpApi builder. ## Related Issue Closes #7338 ## What's Included ### New Package: `@better-auth/effect` A dedicated package that provides Effect-native auth operations: #### 1. Effect-Wrapped Auth Service ```typescript import { BetterAuth } from "@better-auth/effect" import { Effect } from "effect" const program = Effect.gen(function* () { const auth = yield* BetterAuth // All operations return Effects with typed errors const { user, session } = yield* auth.signInEmail({ email: "user@example.com", password: "password", }) }) ``` #### 2. HttpApi Middleware Integration ```typescript import { HttpApiGroup, HttpApiEndpoint } from "@effect/platform" import { AuthMiddleware, CurrentUser } from "@better-auth/effect" class UsersApi extends HttpApiGroup.make("users") .add(HttpApiEndpoint.get("me", "/me").addSuccess(UserProfile)) .middleware(AuthMiddleware) // Type-safe auth middleware {} // In handler - CurrentUser is automatically available const handler = Effect.gen(function* () { const user = yield* CurrentUser return new UserProfile(user) }) ``` #### 3. Typed Errors ```typescript import { Unauthorized, InvalidCredentials, RateLimited } from "@better-auth/effect" // Errors are Effect Schema classes with proper HTTP status codes // Handle them with Effect's error handling program.pipe( Effect.catchTag("Unauthorized", (e) => ...), Effect.catchTag("InvalidCredentials", (e) => ...) ) ``` #### 4. Session Context Tags ```typescript import { CurrentUser, CurrentSession, AuthSession, OptionalUser } from "@better-auth/effect" // Access typed session data anywhere in your Effect chain const user = yield* CurrentUser const session = yield* CurrentSession const { user, session } = yield* AuthSession const maybeUser = yield* OptionalUser // For optional auth routes ``` ## Package Structure ``` packages/effect/ ├── src/ │ ├── index.ts # Main exports │ ├── BetterAuth.ts # Service implementation │ ├── Session.ts # Session context tags │ ├── Errors.ts # Typed error classes │ └── HttpApi.ts # HttpApi middleware ├── test/ │ └── BetterAuth.test.ts ├── package.json ├── README.md └── tsconfig.json ``` ## Documentation Added new integration guide at `docs/content/docs/integrations/effect-platform.mdx` covering: - Quick start guide - Authentication middleware patterns - Using the BetterAuth service directly - Error handling - Full example application ## Benefits - **Type Safety**: Full type inference from better-auth config to handlers - **Error Handling**: Typed errors that integrate with Effect's error channel - **Dependency Injection**: Uses Effect's Context/Layer for clean DI - **Composability**: Middleware composes naturally with HttpApi - **Non-Breaking**: New package, doesn't affect existing users ## Checklist - [x] New package scaffolded - [x] BetterAuth service with Effect-wrapped operations - [x] HttpApi middleware (AuthMiddleware, SessionMiddleware, etc.) - [x] Typed error classes - [x] Session context tags - [x] Basic tests - [x] README documentation - [x] Integration guide in docs - [ ] Additional tests (can add more in follow-up) ## Testing The package includes basic unit tests for: - Error class structure - Context tag identifiers - Effect integration patterns More comprehensive integration tests can be added in a follow-up PR once the API is finalized. --- This PR addresses the growing demand for Effect integration (see #7234 and #7338) by providing a first-class Effect-native experience for Better Auth users. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a native Effect/Platform integration for Better Auth via the new @better-auth/effect package. This enables type-safe auth flows with Effect, HttpApi middleware, and easy handler mounting, fulfilling the integration requested in #7338. - **New Features** - New package: @better-auth/effect with an Effect-wrapped BetterAuth service (all operations return Effects with typed errors). - HttpApi middleware: AuthMiddleware, SessionMiddleware, FullAuthMiddleware, OptionalAuthMiddleware with cookie-based security. - Session context tags: CurrentUser, CurrentSession, AuthSession, OptionalUser, plus makeSessionContext for precise typing. - Handler mounting helpers: betterAuthApp, betterAuthAppFrom, and mountBetterAuth for routing. - Typed error classes mapped to HTTP status codes (Unauthorized, InvalidCredentials, UserNotFound, EmailAlreadyExists, RateLimited, etc.). - Documentation and examples added; basic tests for errors and context tags. <sup>Written for commit f735441ad2425bfd8b823aa582718ae5931e8ce2. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:11:55 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24142