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

main
ts 73 lines 2.41 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import path from "pathe";
8 import { AuroraDataApi } from "../../../src/AuroraDataApi.ts";
9 import { BetterAuth } from "../../../src/index.ts";
10
11 const main = path.resolve(import.meta.dirname, "aurora-handler.ts");
12
13 /**
14 * Aurora Serverless v2 with the Data API (enabled by default). The cluster
15 * needs a VPC, but the Lambda does NOT join it — all access is HTTPS via
16 * the Data API bindings.
17 */
18 export const Db = Effect.gen(function* () {
19 const network = yield* AWS.EC2.Network("AuroraAuthNetwork", {
20 cidrBlock: "10.42.0.0/16",
21 availabilityZones: 2,
22 });
23 const securityGroup = yield* AWS.EC2.SecurityGroup("AuroraAuthDbSecurityGroup", {
24 vpcId: network.vpcId,
25 description: "Better Auth Aurora Data API test cluster",
26 });
27 return yield* AWS.RDS.Aurora("AuroraAuthDb", {
28 subnetIds: network.privateSubnetIds,
29 securityGroupIds: [securityGroup.groupId],
30 });
31 });
32
33 // The layer takes the Aurora composite directly — cluster + secret +
34 // writer dependency all wired from one value.
35 const AuthDatabase = AuroraDataApi(Db, { database: "postgres" });
36
37 export class AuroraAuthFunction extends Lambda.Function<Lambda.Function>()(
38 "BetterAuthAuroraFunction",
39 ) {}
40
41 export default AuroraAuthFunction.make(
42 {
43 main,
44 url: true,
45 memorySize: 512,
46 timeout: Duration.seconds(30),
47 },
48 Effect.gen(function* () {
49 const auth = yield* BetterAuth({
50 basePath: "/auth",
51 emailAndPassword: { enabled: true },
52 });
53
54 return {
55 fetch: Effect.gen(function* () {
56 const request = yield* HttpServerRequest;
57 const pathname = new URL(request.url, "http://lambda").pathname;
58 if (pathname.startsWith("/auth")) {
59 return yield* auth.fetch;
60 }
61 if (pathname.startsWith("/me")) {
62 const session = yield* auth
63 .getSession()
64 .pipe(Effect.catchTag("BetterAuthApiError", () => Effect.succeed(null)));
65 return yield* HttpServerResponse.json({
66 email: session?.user.email ?? null,
67 });
68 }
69 return HttpServerResponse.text("ok");
70 }),
71 };
72 }).pipe(Effect.provide(AuthDatabase)),
73 );