The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { BetterAuth, type BetterAuthApiError } from "@alchemy.run/better-auth"; |
| 2 | import type { RuntimeContext } from "alchemy"; |
| 3 | import type { HttpEffect } from "alchemy/Http"; |
| 4 | import * as Config from "effect/Config"; |
| 5 | import * as Context from "effect/Context"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | |
| 11 | /** What the Worker needs from better-auth: its routes, and the session behind a request. */ |
| 12 | export class Auth extends Context.Service< |
| 13 | Auth, |
| 14 | { |
| 15 | readonly fetch: HttpEffect<RuntimeContext>; |
| 16 | readonly getSession: ( |
| 17 | headers: Headers, |
| 18 | ) => Effect.Effect< |
| 19 | { readonly user: { readonly id: string; readonly name: string } } | null, |
| 20 | BetterAuthApiError, |
| 21 | RuntimeContext |
| 22 | >; |
| 23 | } |
| 24 | >()("Notes/Auth") {} |
| 25 | |
| 26 | /** better-auth with GitHub and Google sign-in, its tables in the Worker's database. */ |
| 27 | export const AuthLive = Layer.effect( |
| 28 | Auth, |
| 29 | Effect.gen(function* () { |
| 30 | const baseURL = yield* Config.String("AUTH_BASE_URL").pipe(Config.option); |
| 31 | return yield* BetterAuth({ |
| 32 | basePath: "/api/auth", |
| 33 | baseURL: Option.getOrUndefined(baseURL), |
| 34 | socialProviders: { |
| 35 | github: { |
| 36 | clientId: yield* Config.String("GITHUB_CLIENT_ID"), |
| 37 | clientSecret: Redacted.value(yield* Config.Redacted("GITHUB_CLIENT_SECRET")), |
| 38 | }, |
| 39 | google: { |
| 40 | clientId: yield* Config.String("GOOGLE_CLIENT_ID"), |
| 41 | clientSecret: Redacted.value(yield* Config.Redacted("GOOGLE_CLIENT_SECRET")), |
| 42 | }, |
| 43 | }, |
| 44 | }); |
| 45 | }), |
| 46 | ); |