@effectify/node-better-auth 0.4.9 → 0.5.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.
- package/README.md +15 -15
- package/dist/package.json +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.js +2 -2
- package/dist/src/lib/auth-service.d.ts +4 -4
- package/dist/src/lib/auth-service.js +13 -13
- package/dist/src/lib/better-auth-error.js +2 -2
- package/dist/src/lib/handler.d.ts +8 -8
- package/dist/src/lib/handler.js +18 -18
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,33 +22,33 @@ bun add @effectify/node-better-auth
|
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
24
24
|
// Auth.js
|
|
25
|
-
import { betterAuth } from
|
|
26
|
-
import Database from
|
|
25
|
+
import { betterAuth } from "better-auth"
|
|
26
|
+
import Database from "better-sqlite3"
|
|
27
27
|
|
|
28
28
|
export const handler = betterAuth({
|
|
29
29
|
emailAndPassword: {
|
|
30
30
|
enabled: true,
|
|
31
31
|
},
|
|
32
|
-
database: new Database(
|
|
32
|
+
database: new Database("../sqlite.db") as any,
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
// Main.ts
|
|
36
|
-
import * as Auth from
|
|
37
|
-
import * as NodeHttpServer from
|
|
38
|
-
import * as HttpMiddleware from
|
|
39
|
-
import * as HttpRouter from
|
|
40
|
-
import * as HttpServer from
|
|
41
|
-
import * as Layer from
|
|
42
|
-
import * as NodeRuntime from
|
|
36
|
+
import * as Auth from "@/Auth.js"
|
|
37
|
+
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"
|
|
38
|
+
import * as HttpMiddleware from "@effect/platform/HttpMiddleware"
|
|
39
|
+
import * as HttpRouter from "@effect/platform/HttpRouter"
|
|
40
|
+
import * as HttpServer from "@effect/platform/HttpServer"
|
|
41
|
+
import * as Layer from "effect/Layer"
|
|
42
|
+
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
|
|
43
43
|
|
|
44
|
-
import { createServer } from
|
|
45
|
-
import { toEffectHandler } from
|
|
44
|
+
import { createServer } from "node:http"
|
|
45
|
+
import { toEffectHandler } from "@effectify/node-better-auth"
|
|
46
46
|
|
|
47
47
|
export const Live = HttpRouter.empty.pipe(
|
|
48
|
-
HttpRouter.all(
|
|
48
|
+
HttpRouter.all("/api/auth/*", toEffectHandler(Auth.handler)),
|
|
49
49
|
HttpServer.serve(HttpMiddleware.logger),
|
|
50
50
|
HttpServer.withLogAddress,
|
|
51
|
-
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 }))
|
|
51
|
+
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
|
|
52
52
|
)
|
|
53
53
|
|
|
54
54
|
Layer.launch(Live).pipe(NodeRuntime.runMain())
|
|
@@ -76,4 +76,4 @@ An Effect that handles HTTP requests for authentication.
|
|
|
76
76
|
|
|
77
77
|
## License
|
|
78
78
|
|
|
79
|
-
MIT
|
|
79
|
+
MIT
|
package/dist/package.json
CHANGED
package/dist/src/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./lib/auth-service.js";
|
|
2
|
+
export * from "./lib/handler.js";
|
package/dist/src/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./lib/auth-service.js";
|
|
2
|
+
export * from "./lib/handler.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type BetterAuthOptions,
|
|
2
|
-
import * as Context from
|
|
3
|
-
import * as Layer from
|
|
4
|
-
import * as Schema from
|
|
1
|
+
import { betterAuth, type BetterAuthOptions, 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
5
|
export declare namespace AuthService {
|
|
6
6
|
export type AuthInstance = ReturnType<typeof betterAuth>;
|
|
7
7
|
const AuthServiceContext_base: Context.TagClass<AuthServiceContext, "AuthServiceContext", {
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { betterAuth } from
|
|
2
|
-
import * as Context from
|
|
3
|
-
import * as Effect from
|
|
4
|
-
import * as Layer from
|
|
5
|
-
import * as Schema from
|
|
1
|
+
import { betterAuth } from "better-auth";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as Schema from "effect/Schema";
|
|
6
6
|
export var AuthService;
|
|
7
7
|
(function (AuthService) {
|
|
8
|
-
class AuthServiceContext extends Context.Tag(
|
|
8
|
+
class AuthServiceContext extends Context.Tag("AuthServiceContext")() {
|
|
9
9
|
}
|
|
10
10
|
AuthService.AuthServiceContext = AuthServiceContext;
|
|
11
11
|
AuthService.layer = (options) => Layer.effect(AuthServiceContext, Effect.gen(function* () {
|
|
12
12
|
// Try to extract database path from Database instance for logging
|
|
13
|
-
const dbPathForLog = options.database && typeof options.database ===
|
|
14
|
-
? String(options.database.name ||
|
|
15
|
-
:
|
|
13
|
+
const dbPathForLog = options.database && typeof options.database === "object" && "name" in options.database
|
|
14
|
+
? String(options.database.name || "unknown")
|
|
15
|
+
: "unknown";
|
|
16
16
|
yield* Effect.logInfo(`Creating auth instance with database path: ${dbPathForLog}`);
|
|
17
17
|
const isAdapter = options.database &&
|
|
18
|
-
typeof options.database ===
|
|
19
|
-
(
|
|
18
|
+
typeof options.database === "object" &&
|
|
19
|
+
("createSession" in options.database || "createUser" in options.database || "client" in options.database);
|
|
20
20
|
if (!isAdapter) {
|
|
21
21
|
yield* Effect.logInfo("Auto-migration is disabled in better-auth v1.4.10+. Please run 'better-auth migrate' manually.");
|
|
22
22
|
}
|
|
23
23
|
return { auth: betterAuth(options) };
|
|
24
24
|
}));
|
|
25
|
-
class AuthContext extends Context.Tag(
|
|
25
|
+
class AuthContext extends Context.Tag("AuthContext")() {
|
|
26
26
|
}
|
|
27
27
|
AuthService.AuthContext = AuthContext;
|
|
28
|
-
class Unauthorized extends Schema.TaggedError()(
|
|
28
|
+
class Unauthorized extends Schema.TaggedError()("Unauthorized", { details: Schema.String }) {
|
|
29
29
|
}
|
|
30
30
|
AuthService.Unauthorized = Unauthorized;
|
|
31
31
|
})(AuthService || (AuthService = {}));
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import * as Data from
|
|
2
|
-
export class BetterAuthApiError extends Data.TaggedError(
|
|
1
|
+
import * as Data from "effect/Data";
|
|
2
|
+
export class BetterAuthApiError extends Data.TaggedError("BetterAuthApiError") {
|
|
3
3
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import * as HttpServerRequest from
|
|
2
|
-
import * as HttpServerResponse from
|
|
3
|
-
import type { Auth } from
|
|
4
|
-
import type { ConfigError } from
|
|
5
|
-
import * as Effect from
|
|
6
|
-
import { BetterAuthApiError } from
|
|
1
|
+
import * as HttpServerRequest from "@effect/platform/HttpServerRequest";
|
|
2
|
+
import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
|
|
3
|
+
import type { Auth } from "better-auth";
|
|
4
|
+
import type { ConfigError } from "effect/ConfigError";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import { BetterAuthApiError } from "./better-auth-error.js";
|
|
7
7
|
export declare const toEffectHandler: (auth: {
|
|
8
|
-
handler: Auth[
|
|
9
|
-
} | Auth[
|
|
8
|
+
handler: Auth["handler"];
|
|
9
|
+
} | Auth["handler"]) => Effect.Effect<HttpServerResponse.HttpServerResponse, BetterAuthApiError | ConfigError, HttpServerRequest.HttpServerRequest>;
|
package/dist/src/lib/handler.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as HttpServerRequest from
|
|
2
|
-
import * as HttpServerResponse from
|
|
3
|
-
import * as NodeHttpServerRequest from
|
|
4
|
-
import { toNodeHandler } from
|
|
5
|
-
import * as Config from
|
|
6
|
-
import * as Effect from
|
|
7
|
-
import { BetterAuthApiError } from
|
|
1
|
+
import * as HttpServerRequest from "@effect/platform/HttpServerRequest";
|
|
2
|
+
import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
|
|
3
|
+
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
|
|
4
|
+
import { toNodeHandler } from "better-auth/node";
|
|
5
|
+
import * as Config from "effect/Config";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import { BetterAuthApiError } from "./better-auth-error.js";
|
|
8
8
|
const TRAILING_SLASH_REGEX = /\/+$/;
|
|
9
9
|
const PROTOCOL_REGEX = /(https?:\/\/)+/;
|
|
10
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(
|
|
14
|
+
const appUrl = yield* Config.url("BETTER_AUTH_URL");
|
|
15
15
|
// Debug: log the configured app URL so we can diagnose Config errors
|
|
16
16
|
yield* Effect.log(`toEffectHandler: BETTER_AUTH_URL=${String(appUrl)}`);
|
|
17
|
-
const normalizeUrl = (url) => url.toString().replace(TRAILING_SLASH_REGEX,
|
|
17
|
+
const normalizeUrl = (url) => url.toString().replace(TRAILING_SLASH_REGEX, "").replace(PROTOCOL_REGEX, "http://");
|
|
18
18
|
const allowedOrigins = [normalizeUrl(appUrl)];
|
|
19
|
-
const origin = nodeRequest.headers.origin ? normalizeUrl(appUrl) :
|
|
19
|
+
const origin = nodeRequest.headers.origin ? normalizeUrl(appUrl) : "";
|
|
20
20
|
if (allowedOrigins.includes(origin)) {
|
|
21
|
-
nodeResponse.setHeader(
|
|
22
|
-
nodeResponse.setHeader(
|
|
23
|
-
nodeResponse.setHeader(
|
|
24
|
-
nodeResponse.setHeader(
|
|
25
|
-
nodeResponse.setHeader(
|
|
21
|
+
nodeResponse.setHeader("Access-Control-Allow-Origin", nodeRequest.headers.origin || "");
|
|
22
|
+
nodeResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
|
23
|
+
nodeResponse.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
24
|
+
nodeResponse.setHeader("Access-Control-Max-Age", "600");
|
|
25
|
+
nodeResponse.setHeader("Access-Control-Allow-Credentials", "true");
|
|
26
26
|
}
|
|
27
27
|
// Handle preflight requests
|
|
28
|
-
if (nodeRequest.method ===
|
|
28
|
+
if (nodeRequest.method === "OPTIONS") {
|
|
29
29
|
nodeResponse.statusCode = 200;
|
|
30
30
|
nodeResponse.end();
|
|
31
31
|
return HttpServerResponse.empty({ status: 200 });
|
|
@@ -34,7 +34,7 @@ export const toEffectHandler = (auth) => Effect.gen(function* () {
|
|
|
34
34
|
yield* Effect.log(`toEffectHandler: incoming ${nodeRequest.method} ${String(nodeRequest.url)}`);
|
|
35
35
|
try {
|
|
36
36
|
yield* Effect.tryPromise({
|
|
37
|
-
try: () =>
|
|
37
|
+
try: () => "handler" in auth
|
|
38
38
|
? toNodeHandler(auth.handler)(nodeRequest, nodeResponse)
|
|
39
39
|
: toNodeHandler(auth)(nodeRequest, nodeResponse),
|
|
40
40
|
catch: (cause) => new BetterAuthApiError({ cause }),
|
|
@@ -48,7 +48,7 @@ export const toEffectHandler = (auth) => Effect.gen(function* () {
|
|
|
48
48
|
try {
|
|
49
49
|
// Try to return the error to the client as JSON to make debugging easier
|
|
50
50
|
nodeResponse.statusCode = 500;
|
|
51
|
-
nodeResponse.setHeader(
|
|
51
|
+
nodeResponse.setHeader("Content-Type", "application/json");
|
|
52
52
|
const payload = JSON.stringify({ error: String(err) });
|
|
53
53
|
nodeResponse.end(payload);
|
|
54
54
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectify/node-better-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Integration of better-auth with Effect for Node.js applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/src/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@effect/platform": "0.94.0",
|
|
26
26
|
"@effect/platform-node": "0.104.0",
|
|
27
27
|
"better-auth": "1.4.10",
|
|
28
|
-
"effect": "3.19.
|
|
28
|
+
"effect": "3.19.15"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@effect/vitest": "0.27.0",
|