Services

Krakstack Auth

A Better Auth foundation for TanStack Start that connects to Krakstack Auth via OAuth, with server config, React client, an /api/auth route handler, and an Effect auth service with HttpApiMiddleware.

Install
Add this registry item to your app with shadcn.
bunx --bun shadcn@latest add https://krakstack.net/r/krakstack-auth.json

Overview

auth installs the app-level Better Auth foundation used by the sign-in, sign-up, and user-button blocks. It connects to Krakstack Auth as an OAuth provider using the generic OAuth plugin with PKCE, and includes the server config, React client, the TanStack Start API route that forwards /api/auth/* requests to Better Auth, and an Effect auth service with HTTP API middleware for protecting Effect API endpoints.

Included Files

  • services/auth/config.ts - Better Auth server setup with the Krakstack Auth OAuth provider
  • services/auth/client/index.ts - Better Auth React client with generic OAuth support
  • routes/api/auth/$.tsx - TanStack Start server route for /api/auth/*
  • services/auth/index.ts - Effect Auth service with getSession and requireUser methods
  • services/auth/middleware.ts - Effect AuthMiddleware that provides CurrentUser to downstream handlers

Usage

Client-side session access:

import { authClient } from "@/services/auth/client";

const { data: session } = authClient.useSession();

Server code can import the configured auth instance:

import { auth } from "@/services/auth/config";

To initiate OAuth sign-in with Krakstack Auth:

authClient.signIn.oauth2({ providerId: "krakstack-auth", callbackURL: "/dashboard" });

Apply the auth middleware to an Effect HTTP API group:

import { AuthMiddleware } from "@/services/auth/middleware";

const MyGroup = HttpApiGroup.make("my-group")
  .middleware(AuthMiddleware)
  // ...endpoints

Access the current user in a handler:

import { CurrentUser } from "@/services/auth/middleware";

handlers.handle("myEndpoint", () =>
  Effect.gen(function* () {
    const user = yield* CurrentUser;
    // user.id, user.name, user.email
  }),
);

Environment Variables

  • BETTER_AUTH_SECRET - Secret used by Better Auth
  • BETTER_AUTH_URL - Public a... (line truncated to 2000 chars)
Dependencies
Packages and shadcn components required by this registry item.