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.
bunx --bun shadcn@latest add https://krakstack.net/r/krakstack-auth.jsonOverview
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 providerservices/auth/client/index.ts- Better Auth React client with generic OAuth supportroutes/api/auth/$.tsx- TanStack Start server route for/api/auth/*services/auth/index.ts- EffectAuthservice withgetSessionandrequireUsermethodsservices/auth/middleware.ts- EffectAuthMiddlewarethat providesCurrentUserto 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)
// ...endpointsAccess 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 AuthBETTER_AUTH_URL- Public a... (line truncated to 2000 chars)