1import { BetterAuth } from "@alchemy.run/better-auth";
2import * as Config from "effect/Config";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Redacted from "effect/Redacted";
8
9export 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
31export class Auth extends Context.Service<
32 Auth,
33 Effect.Success<typeof makeAuth>
34>()("app/Auth") {
35 static readonly layer = Layer.effect(Auth, makeAuth);
36}