@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
ts 66 lines 2.1 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Neon from "@alchemy.run/neon";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import { CloudflareHyperdrive } from "../../../src/CloudflareHyperdrive.ts";
8 import { BetterAuth } from "../../../src/index.ts";
9
10 export const HdProject = Neon.Project("BetterAuthHdPg");
11
12 export const Hyperdrive = Effect.gen(function* () {
13 const project = yield* HdProject;
14 return yield* Cloudflare.Hyperdrive.Connection("BetterAuthHd", {
15 origin: project.origin,
16 // auth reads must never be stale
17 caching: { disabled: true },
18 });
19 });
20
21 // Hyperdrive's connection string only exists inside the Worker — the Neon
22 // origin URL drives the deploy-time migration Action instead.
23 const AuthDatabase = Layer.unwrap(
24 Effect.gen(function* () {
25 const project = yield* HdProject;
26 const connection = yield* Hyperdrive;
27 return CloudflareHyperdrive(connection, {
28 migrate: project.connectionUri,
29 });
30 }),
31 );
32
33 export default class HyperdriveAuthWorker extends Cloudflare.Worker<HyperdriveAuthWorker>()(
34 "BetterAuthHdWorker",
35 {
36 main: import.meta.url,
37 compatibility: {
38 date: "2026-03-17",
39 flags: ["nodejs_compat"],
40 },
41 },
42 Effect.gen(function* () {
43 const auth = yield* BetterAuth({
44 basePath: "/auth",
45 emailAndPassword: { enabled: true },
46 });
47
48 return {
49 fetch: Effect.gen(function* () {
50 const request = yield* HttpServerRequest;
51 if (request.url.startsWith("/auth")) {
52 return yield* auth.fetch;
53 }
54 if (request.url.startsWith("/me")) {
55 const session = yield* auth
56 .getSession()
57 .pipe(Effect.catchTag("BetterAuthApiError", () => Effect.succeed(null)));
58 return yield* HttpServerResponse.json({
59 email: session?.user.email ?? null,
60 });
61 }
62 return HttpServerResponse.text("ok");
63 }),
64 };
65 }).pipe(Effect.provide(AuthDatabase)),
66 ) {}