Feature Request: Effect/Platform Native Integration #2701

Closed
opened 2026-03-13 10:14:04 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @dallenpyrah on GitHub (Jan 13, 2026).

Summary

Add official Effect-native integration for using Better-Auth with Effect/Platform's HttpApi builder.

Motivation

Effect/Platform is gaining significant popularity for building type-safe APIs in TypeScript. Users want to use Better-Auth in an Effect-native way where:

  • All auth operations return Effects with typed errors
  • Integration with Effect's dependency injection (Context/Layer)
  • Seamless integration with HttpApi middleware system
  • Type-safe session access in handlers
  • #7234 (Drizzle Effect-based execution model)

Proposed Solution

Create a new @better-auth/effect package that provides:

1. Effect-Wrapped Auth Operations

import { BetterAuth } from "@better-auth/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 ProtectedApi extends HttpApiGroup.make("api")
  .add(HttpApiEndpoint.get("me", "/me").addSuccess(UserProfile))
  .middleware(AuthMiddleware)  // Type-safe auth middleware
{}

// In handler - CurrentUser is automatically available
const handler = HttpApiBuilder.group(MyApi, "api", (h) =>
  h.handle("me", () =>
    Effect.gen(function* () {
      const user = yield* CurrentUser  // Typed user from session
      return new UserProfile(user)
    })
  )
)

3. Typed Error Handling

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

// Errors are Effect Schema classes with proper HTTP status codes
// Unauthorized -> 401
// InvalidCredentials -> 401  
// Forbidden -> 403
// etc.

4. Session Context Tags

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

// Access typed session data anywhere in your Effect chain
const user = yield* CurrentUser
const session = yield* CurrentSession

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

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

Additional Context

  • Builds on existing better-auth architecture
  • Aligns with Effect ecosystem patterns
  • Enables future @effect/sql-drizzle integration for full Effect-native DB operations

Implementation Plan

  1. Create @better-auth/effect package
  2. Implement core service and error types
  3. Implement HttpApi middleware
  4. Write documentation and examples
  5. Add tests

I'm happy to contribute a PR for this feature.

Originally created by @dallenpyrah on GitHub (Jan 13, 2026). ## Summary Add official Effect-native integration for using Better-Auth with Effect/Platform's HttpApi builder. ## Motivation Effect/Platform is gaining significant popularity for building type-safe APIs in TypeScript. Users want to use Better-Auth in an Effect-native way where: - All auth operations return `Effect`s with typed errors - Integration with Effect's dependency injection (Context/Layer) - Seamless integration with HttpApi middleware system - Type-safe session access in handlers ## Related Issues - #7234 (Drizzle Effect-based execution model) ## Proposed Solution Create a new `@better-auth/effect` package that provides: ### 1. Effect-Wrapped Auth Operations ```typescript import { BetterAuth } from "@better-auth/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 ProtectedApi extends HttpApiGroup.make("api") .add(HttpApiEndpoint.get("me", "/me").addSuccess(UserProfile)) .middleware(AuthMiddleware) // Type-safe auth middleware {} // In handler - CurrentUser is automatically available const handler = HttpApiBuilder.group(MyApi, "api", (h) => h.handle("me", () => Effect.gen(function* () { const user = yield* CurrentUser // Typed user from session return new UserProfile(user) }) ) ) ``` ### 3. Typed Error Handling ```typescript import { Unauthorized, InvalidCredentials } from "@better-auth/effect" // Errors are Effect Schema classes with proper HTTP status codes // Unauthorized -> 401 // InvalidCredentials -> 401 // Forbidden -> 403 // etc. ``` ### 4. Session Context Tags ```typescript import { CurrentUser, CurrentSession } from "@better-auth/effect" // Access typed session data anywhere in your Effect chain const user = yield* CurrentUser const session = yield* CurrentSession ``` ## 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 ``` ## 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 ## Additional Context - Builds on existing better-auth architecture - Aligns with Effect ecosystem patterns - Enables future @effect/sql-drizzle integration for full Effect-native DB operations ## Implementation Plan 1. Create `@better-auth/effect` package 2. Implement core service and error types 3. Implement HttpApi middleware 4. Write documentation and examples 5. Add tests I'm happy to contribute a PR for this feature.
GiteaMirror added the enhancement label 2026-03-13 10:14:04 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 13, 2026):

Thanks for your feature request regarding the integration of Better Auth with Effect/Platform! This is a great idea, as many users are looking for enhanced type safety and seamless integration in their TypeScript applications.

Currently, there isn't official support for integrating Better Auth with the Effect platform, but your proposal is well articulated and would provide significant benefits to users working within that ecosystem. As you mentioned, the creation of an @better-auth/effect package could bring type-safe auth operations and error handling, among other features, which aligns well with the goals of Better Auth for maintainability and robustness [1].

Your offering to contribute a PR for this feature is highly appreciated, and it will be great to see how it develops! If you have any further questions or need assistance during the implementation, please feel free to engage with the Better Auth team by tagging @bekacru or @ping__.

What specific features or enhancements are you most excited to implement in this integration?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 13, 2026): Thanks for your feature request regarding the integration of Better Auth with Effect/Platform! This is a great idea, as many users are looking for enhanced type safety and seamless integration in their TypeScript applications. Currently, there isn't official support for integrating Better Auth with the Effect platform, but your proposal is well articulated and would provide significant benefits to users working within that ecosystem. As you mentioned, the creation of an `@better-auth/effect` package could bring type-safe auth operations and error handling, among other features, which aligns well with the goals of Better Auth for maintainability and robustness [[1]](https://www.better-auth.com/docs). Your offering to contribute a PR for this feature is highly appreciated, and it will be great to see how it develops! If you have any further questions or need assistance during the implementation, please feel free to engage with the Better Auth team by tagging @bekacru or @ping__. What specific features or enhancements are you most excited to implement in this integration? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@himself65 commented on GitHub (Jan 13, 2026):

You are welcome to create a library for that. But our team don't have a plan right now

@himself65 commented on GitHub (Jan 13, 2026): You are welcome to create a library for that. But our team don't have a plan right now
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2701