@effectify/node-better-auth 0.1.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,2 @@
1
+ export * from './lib/auth-service.js';
1
2
  export * from './lib/handler.js';
package/dist/src/index.js CHANGED
@@ -1 +1,2 @@
1
+ export * from './lib/auth-service.js';
1
2
  export * from './lib/handler.js';
@@ -0,0 +1,27 @@
1
+ import { type BetterAuthOptions, betterAuth, type Session, type User } from 'better-auth';
2
+ import * as Context from 'effect/Context';
3
+ import * as Layer from 'effect/Layer';
4
+ import * as Schema from 'effect/Schema';
5
+ export declare namespace AuthService {
6
+ export type AuthInstance = ReturnType<typeof betterAuth>;
7
+ const AuthServiceContext_base: Context.TagClass<AuthServiceContext, "AuthServiceContext", {
8
+ readonly auth: AuthInstance;
9
+ }>;
10
+ export class AuthServiceContext extends AuthServiceContext_base {
11
+ }
12
+ export const layer: (options: BetterAuthOptions) => Layer.Layer<AuthServiceContext, never, never>;
13
+ const AuthContext_base: Context.TagClass<AuthContext, "AuthContext", {
14
+ readonly user: User;
15
+ readonly session: Session;
16
+ }>;
17
+ export class AuthContext extends AuthContext_base {
18
+ }
19
+ const Unauthorized_base: Schema.TaggedErrorClass<Unauthorized, "Unauthorized", {
20
+ readonly _tag: Schema.tag<"Unauthorized">;
21
+ } & {
22
+ details: typeof Schema.String;
23
+ }>;
24
+ export class Unauthorized extends Unauthorized_base {
25
+ }
26
+ export {};
27
+ }
@@ -0,0 +1,28 @@
1
+ import { betterAuth } from 'better-auth';
2
+ import { getMigrations } from 'better-auth/db';
3
+ import * as Context from 'effect/Context';
4
+ import * as Effect from 'effect/Effect';
5
+ import * as Layer from 'effect/Layer';
6
+ import * as Schema from 'effect/Schema';
7
+ export var AuthService;
8
+ (function (AuthService) {
9
+ class AuthServiceContext extends Context.Tag('AuthServiceContext')() {
10
+ }
11
+ AuthService.AuthServiceContext = AuthServiceContext;
12
+ AuthService.layer = (options) => Layer.effect(AuthServiceContext, Effect.gen(function* () {
13
+ // Try to extract database path from Database instance for logging
14
+ const dbPathForLog = options.database && typeof options.database === 'object' && 'name' in options.database
15
+ ? String(options.database.name || 'unknown')
16
+ : 'unknown';
17
+ yield* Effect.logInfo(`Creating auth instance with database path: ${dbPathForLog}`);
18
+ const { runMigrations } = yield* Effect.promise(() => getMigrations(options));
19
+ yield* Effect.promise(runMigrations);
20
+ return { auth: betterAuth(options) };
21
+ }));
22
+ class AuthContext extends Context.Tag('AuthContext')() {
23
+ }
24
+ AuthService.AuthContext = AuthContext;
25
+ class Unauthorized extends Schema.TaggedError()('Unauthorized', { details: Schema.String }) {
26
+ }
27
+ AuthService.Unauthorized = Unauthorized;
28
+ })(AuthService || (AuthService = {}));
@@ -11,10 +11,10 @@ export const toEffectHandler = (auth) => Effect.gen(function* () {
11
11
  const request = yield* HttpServerRequest.HttpServerRequest;
12
12
  const nodeRequest = NodeHttpServerRequest.toIncomingMessage(request);
13
13
  const nodeResponse = NodeHttpServerRequest.toServerResponse(request);
14
- const appUrl = yield* Config.url('APP_URL');
15
- const normalizeUrl = (url) => {
16
- return url.toString().replace(TRAILING_SLASH_REGEX, '').replace(PROTOCOL_REGEX, 'http://');
17
- };
14
+ const appUrl = yield* Config.url('BETTER_AUTH_URL');
15
+ // Debug: log the configured app URL so we can diagnose Config errors
16
+ yield* Effect.log(`toEffectHandler: BETTER_AUTH_URL=${String(appUrl)}`);
17
+ const normalizeUrl = (url) => url.toString().replace(TRAILING_SLASH_REGEX, '').replace(PROTOCOL_REGEX, 'http://');
18
18
  const allowedOrigins = [normalizeUrl(appUrl)];
19
19
  const origin = nodeRequest.headers.origin ? normalizeUrl(appUrl) : '';
20
20
  if (allowedOrigins.includes(origin)) {
@@ -30,15 +30,33 @@ export const toEffectHandler = (auth) => Effect.gen(function* () {
30
30
  nodeResponse.end();
31
31
  return HttpServerResponse.empty({ status: 200 });
32
32
  }
33
- yield* Effect.tryPromise({
34
- try: () => 'handler' in auth
35
- ? toNodeHandler(auth.handler)(nodeRequest, nodeResponse)
36
- : toNodeHandler(auth)(nodeRequest, nodeResponse),
37
- catch: (cause) => {
38
- return new BetterAuthApiError({ cause });
39
- },
40
- });
41
- return HttpServerResponse.empty({
42
- status: nodeResponse.writableFinished ? nodeResponse.statusCode : 499,
43
- });
33
+ // Log incoming request for debugging
34
+ yield* Effect.log(`toEffectHandler: incoming ${nodeRequest.method} ${String(nodeRequest.url)}`);
35
+ try {
36
+ yield* Effect.tryPromise({
37
+ try: () => 'handler' in auth
38
+ ? toNodeHandler(auth.handler)(nodeRequest, nodeResponse)
39
+ : toNodeHandler(auth)(nodeRequest, nodeResponse),
40
+ catch: (cause) => new BetterAuthApiError({ cause }),
41
+ });
42
+ // Log the response status after the handler completes
43
+ yield* Effect.log(`toEffectHandler: completed ${nodeRequest.method} ${String(nodeRequest.url)} -> ${nodeResponse.statusCode}`);
44
+ }
45
+ catch (err) {
46
+ // Ensure we log errors from the underlying handler for debugging
47
+ yield* Effect.log(`toEffectHandler: error handling ${nodeRequest.method} ${String(nodeRequest.url)}: ${String(err)}`);
48
+ try {
49
+ // Try to return the error to the client as JSON to make debugging easier
50
+ nodeResponse.statusCode = 500;
51
+ nodeResponse.setHeader('Content-Type', 'application/json');
52
+ const payload = JSON.stringify({ error: String(err) });
53
+ nodeResponse.end(payload);
54
+ }
55
+ catch (writeErr) {
56
+ // If writing the error response fails, log that too
57
+ yield* Effect.log(`toEffectHandler: failed to write error response: ${String(writeErr)}`);
58
+ }
59
+ return HttpServerResponse.empty({ status: 500 });
60
+ }
61
+ return HttpServerResponse.empty({ status: nodeResponse.writableFinished ? nodeResponse.statusCode : 499 });
44
62
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectify/node-better-auth",
3
- "version": "0.1.0",
3
+ "version": "0.4.0",
4
4
  "description": "Integration of better-auth with Effect for Node.js applications",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,10 +22,10 @@
22
22
  "!**/*.tsbuildinfo"
23
23
  ],
24
24
  "dependencies": {
25
- "@effect/platform": "0.91.1",
26
- "@effect/platform-node": "0.97.1",
27
- "better-auth": "1.3.18",
28
- "effect": "3.17.14"
25
+ "@effect/platform": "0.93.3",
26
+ "@effect/platform-node": "0.101.1",
27
+ "better-auth": "1.4.1",
28
+ "effect": "3.19.6"
29
29
  },
30
30
  "devDependencies": {},
31
31
  "peerDependencies": {},