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

main
ts 62 lines 2.26 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Neon from "@alchemy.run/neon";
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 * as Layer from "effect/Layer";
8 import path from "pathe";
9 import { BetterAuth } from "../../../src/index.ts";
10 import { Neon as NeonDatabase } from "../../../src/Neon.ts";
11
12 const main = path.resolve(import.meta.dirname, "auth-handler.ts");
13
14 export const AuthDb = Neon.Project("BetterAuthLambdaPg");
15
16 // connectionUri is an Output: bound into the Lambda environment at deploy,
17 // read back at runtime; the same source drives the deploy-time migration
18 // Action. The serverless driver is the optimal Lambda -> Neon pairing —
19 // WebSocket-based, no `pg` install into the artifact needed.
20 const AuthDatabase = Layer.unwrap(
21 Effect.map(AuthDb, (project) => NeonDatabase(project.connectionUri)),
22 );
23
24 export class AuthFunction extends Lambda.Function<Lambda.Function>()("BetterAuthFunction") {}
25
26 export default AuthFunction.make(
27 {
28 main,
29 url: true,
30 // password hashing (scrypt) + the auth bundle need more than the
31 // 128 MB default, and cold start + Neon round-trips more than the
32 // 3 s default timeout
33 memorySize: 512,
34 timeout: Duration.seconds(30),
35 },
36 Effect.gen(function* () {
37 const auth = yield* BetterAuth({
38 basePath: "/auth",
39 emailAndPassword: { enabled: true },
40 });
41
42 return {
43 fetch: Effect.gen(function* () {
44 const request = yield* HttpServerRequest;
45 // On Lambda the request url is absolute — route on the pathname.
46 const pathname = new URL(request.url, "http://lambda").pathname;
47 if (pathname.startsWith("/auth")) {
48 return yield* auth.fetch;
49 }
50 if (pathname.startsWith("/me")) {
51 const session = yield* auth
52 .getSession()
53 .pipe(Effect.catchTag("BetterAuthApiError", () => Effect.succeed(null)));
54 return yield* HttpServerResponse.json({
55 email: session?.user.email ?? null,
56 });
57 }
58 return HttpServerResponse.text("ok");
59 }),
60 };
61 }).pipe(Effect.provide(AuthDatabase)),
62 );