@effectify/node-better-auth 0.1.0 → 0.4.1
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.
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/lib/auth-service.d.ts +27 -0
- package/dist/src/lib/auth-service.js +28 -0
- package/dist/src/lib/handler.js +33 -15
- package/package.json +10 -10
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -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 = {}));
|
package/dist/src/lib/handler.js
CHANGED
|
@@ -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('
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
status
|
|
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,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectify/node-better-auth",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Integration of better-auth with Effect for Node.js applications",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/src/index.js",
|
|
7
|
+
"module": "./dist/src/index.js",
|
|
8
|
+
"types": "./dist/src/index.d.ts",
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"dist",
|
|
22
22
|
"!**/*.tsbuildinfo"
|
|
23
23
|
],
|
|
24
|
-
"
|
|
25
|
-
"@effect/platform": "0.
|
|
26
|
-
"@effect/platform-node": "0.
|
|
27
|
-
"better-auth": "1.
|
|
28
|
-
"effect": "3.
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@effect/platform": "0.94.0",
|
|
26
|
+
"@effect/platform-node": "0.104.0",
|
|
27
|
+
"better-auth": "1.4.1",
|
|
28
|
+
"effect": "3.19.13"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {},
|
|
31
|
-
"
|
|
31
|
+
"dependencies": {},
|
|
32
32
|
"optionalDependencies": {}
|
|
33
33
|
}
|