| 1 | import { BetterAuth } from "@alchemy.run/better-auth"; |
| 2 | import * as Config from "effect/Config"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | |
| 9 | export const makeAuth = Effect.gen(function* () { |
| 10 | const baseURL = yield* Config.String("AUTH_BASE_URL").pipe(Config.option); |
| 11 | const githubEnabled = yield* Config.Boolean("GITHUB_ENABLED").pipe( |
| 12 | Config.withDefault(false), |
| 13 | ); |
| 14 | const github = githubEnabled |
| 15 | ? { |
| 16 | clientId: yield* Config.String("GITHUB_CLIENT_ID"), |
| 17 | clientSecret: Redacted.value( |
| 18 | yield* Config.Redacted("GITHUB_CLIENT_SECRET"), |
| 19 | ), |
| 20 | } |
| 21 | : undefined; |
| 22 | |
| 23 | return yield* BetterAuth({ |
| 24 | basePath: "/api/auth", |
| 25 | emailAndPassword: { enabled: true }, |
| 26 | baseURL: Option.getOrUndefined(baseURL), |
| 27 | socialProviders: github ? { github } : {}, |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | export class Auth extends Context.Service< |
| 32 | Auth, |
| 33 | Effect.Success<typeof makeAuth> |
| 34 | >()("app/Auth") { |
| 35 | static readonly layer = Layer.effect(Auth, makeAuth); |
| 36 | } |