@effectify/react-router-better-auth 0.5.11 → 1.0.0-alpha.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/dist/src/lib/auth-guard.js +67 -27
- package/dist/src/lib/handlers.d.ts +1 -1
- package/dist/src/lib/handlers.js +14 -6
- package/package.json +9 -4
|
@@ -2,38 +2,82 @@ import { AuthService } from "@effectify/node-better-auth";
|
|
|
2
2
|
import { ActionArgsContext, LoaderArgsContext } from "@effectify/react-router";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
4
|
import { redirect } from "react-router";
|
|
5
|
-
const mapHeaders = (args) =>
|
|
5
|
+
const mapHeaders = (args) => {
|
|
6
|
+
const headers = new Headers(args.request.headers);
|
|
7
|
+
if (!headers.has("origin")) {
|
|
8
|
+
headers.set("origin", "http://localhost:3000");
|
|
9
|
+
}
|
|
10
|
+
return headers;
|
|
11
|
+
};
|
|
12
|
+
// Default auth server URL - can be overridden with BETTER_AUTH_URL env var
|
|
13
|
+
const getAuthServerOrigin = () => process.env.BETTER_AUTH_URL?.replace(/\/api\/auth\/?$/, "") ||
|
|
14
|
+
"http://localhost:3001";
|
|
6
15
|
const verifySessionWithContext = (context) => Effect.gen(function* () {
|
|
7
16
|
const args = yield* context;
|
|
8
|
-
const { auth } = yield* AuthService.AuthServiceContext;
|
|
9
17
|
const forwardedHeaders = mapHeaders(args);
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
const cookieValue = forwardedHeaders.get("cookie") || "";
|
|
19
|
+
const authOrigin = getAuthServerOrigin();
|
|
20
|
+
// Make direct fetch call to auth server
|
|
21
|
+
const response = yield* Effect.tryPromise({
|
|
22
|
+
try: () => fetch(`${authOrigin}/api/auth/get-session`, {
|
|
23
|
+
headers: {
|
|
24
|
+
cookie: cookieValue,
|
|
25
|
+
"Content-Type": "application/json",
|
|
26
|
+
origin: "http://localhost:3000",
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
catch: (cause) => new AuthService.Unauthorized({
|
|
30
|
+
details: `Auth server unreachable: ${String(cause)}`,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
const fetchResult = yield* Effect.tryPromise({
|
|
34
|
+
try: () => response.json(),
|
|
35
|
+
catch: (cause) => new AuthService.Unauthorized({
|
|
36
|
+
details: `Failed to parse session: ${String(cause)}`,
|
|
37
|
+
}),
|
|
16
38
|
});
|
|
17
|
-
if (
|
|
18
|
-
return
|
|
39
|
+
if (fetchResult?.session) {
|
|
40
|
+
return {
|
|
41
|
+
user: fetchResult.user,
|
|
42
|
+
session: fetchResult.session,
|
|
43
|
+
};
|
|
19
44
|
}
|
|
20
|
-
|
|
45
|
+
throw new AuthService.Unauthorized({
|
|
46
|
+
details: "Missing or invalid authentication",
|
|
47
|
+
});
|
|
21
48
|
});
|
|
22
49
|
const verifySessionWithActionContext = (context) => Effect.gen(function* () {
|
|
23
50
|
const args = yield* context;
|
|
24
|
-
const { auth } = yield* AuthService.AuthServiceContext;
|
|
25
51
|
const forwardedHeaders = mapHeaders(args);
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
const cookieValue = forwardedHeaders.get("cookie") || "";
|
|
53
|
+
const authOrigin = getAuthServerOrigin();
|
|
54
|
+
const response = yield* Effect.tryPromise({
|
|
55
|
+
try: () => fetch(`${authOrigin}/api/auth/get-session`, {
|
|
56
|
+
headers: {
|
|
57
|
+
cookie: cookieValue,
|
|
58
|
+
"Content-Type": "application/json",
|
|
59
|
+
origin: "http://localhost:3000",
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
catch: (cause) => new AuthService.Unauthorized({
|
|
63
|
+
details: `Auth server unreachable: ${String(cause)}`,
|
|
64
|
+
}),
|
|
32
65
|
});
|
|
33
|
-
|
|
34
|
-
|
|
66
|
+
const fetchResult = yield* Effect.tryPromise({
|
|
67
|
+
try: () => response.json(),
|
|
68
|
+
catch: (cause) => new AuthService.Unauthorized({
|
|
69
|
+
details: `Failed to parse session: ${String(cause)}`,
|
|
70
|
+
}),
|
|
71
|
+
});
|
|
72
|
+
if (fetchResult?.session) {
|
|
73
|
+
return {
|
|
74
|
+
user: fetchResult.user,
|
|
75
|
+
session: fetchResult.session,
|
|
76
|
+
};
|
|
35
77
|
}
|
|
36
|
-
|
|
78
|
+
throw new AuthService.Unauthorized({
|
|
79
|
+
details: "Missing or invalid authentication",
|
|
80
|
+
});
|
|
37
81
|
});
|
|
38
82
|
const verifySession = () => verifySessionWithContext(LoaderArgsContext);
|
|
39
83
|
const verifySessionFromAction = () => verifySessionWithActionContext(ActionArgsContext);
|
|
@@ -41,15 +85,11 @@ export const withBetterAuthGuard = Object.assign((eff) => Effect.gen(function* (
|
|
|
41
85
|
const authResult = yield* verifySession();
|
|
42
86
|
return yield* Effect.provideService(eff, AuthService.AuthContext, authResult);
|
|
43
87
|
}), {
|
|
44
|
-
with: (opts) => (eff) => Effect.
|
|
45
|
-
return yield* verifySession().pipe(Effect.flatMap((authResult) => Effect.provideService(eff, AuthService.AuthContext, authResult)), Effect.catchTag("Unauthorized", () => Effect.sync(() => redirect(opts.redirectOnFail, opts.redirectInit))));
|
|
46
|
-
}),
|
|
88
|
+
with: (opts) => (eff) => verifySession().pipe(Effect.flatMap((authResult) => Effect.provideService(eff, AuthService.AuthContext, authResult)), Effect.catchTag("Unauthorized", () => Effect.sync(() => redirect(opts.redirectOnFail, opts.redirectInit)))),
|
|
47
89
|
});
|
|
48
90
|
export const withBetterAuthGuardAction = Object.assign((eff) => Effect.gen(function* () {
|
|
49
91
|
const authResult = yield* verifySessionFromAction();
|
|
50
92
|
return yield* Effect.provideService(eff, AuthService.AuthContext, authResult);
|
|
51
93
|
}), {
|
|
52
|
-
with: (opts) => (eff) => Effect.
|
|
53
|
-
return yield* verifySessionFromAction().pipe(Effect.flatMap((authResult) => Effect.provideService(eff, AuthService.AuthContext, authResult)), Effect.catchTag("Unauthorized", () => Effect.sync(() => redirect(opts.redirectOnFail, opts.redirectInit))));
|
|
54
|
-
}),
|
|
94
|
+
with: (opts) => (eff) => verifySessionFromAction().pipe(Effect.flatMap((authResult) => Effect.provideService(eff, AuthService.AuthContext, authResult)), Effect.catchTag("Unauthorized", () => Effect.sync(() => redirect(opts.redirectOnFail, opts.redirectInit)))),
|
|
55
95
|
});
|
|
@@ -2,4 +2,4 @@ import { AuthService } from "@effectify/node-better-auth";
|
|
|
2
2
|
import { ActionArgsContext, LoaderArgsContext } from "@effectify/react-router";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
4
|
export declare const betterAuthLoader: Effect.Effect<Response, never, AuthService.AuthServiceContext | LoaderArgsContext>;
|
|
5
|
-
export declare const betterAuthAction: Effect.Effect<Response, never, AuthService.AuthServiceContext
|
|
5
|
+
export declare const betterAuthAction: Effect.Effect<Response, never, ActionArgsContext | AuthService.AuthServiceContext>;
|
package/dist/src/lib/handlers.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { AuthService } from "@effectify/node-better-auth";
|
|
2
2
|
import { ActionArgsContext, LoaderArgsContext } from "@effectify/react-router";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
import { pipe } from "effect/Function";
|
|
5
|
+
const withAuthHandler = (request) => Effect.gen(function* () {
|
|
6
|
+
const auth = yield* AuthService.AuthServiceContext;
|
|
7
7
|
return yield* Effect.promise(() => auth.auth.handler(request));
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
});
|
|
9
|
+
const getLoaderRequest = Effect.gen(function* () {
|
|
10
|
+
const ctx = yield* LoaderArgsContext;
|
|
11
|
+
return ctx.request;
|
|
12
|
+
});
|
|
13
|
+
const getActionRequest = Effect.gen(function* () {
|
|
14
|
+
const ctx = yield* ActionArgsContext;
|
|
15
|
+
return ctx.request;
|
|
16
|
+
});
|
|
17
|
+
export const betterAuthLoader = pipe(getLoaderRequest, Effect.flatMap(withAuthHandler));
|
|
18
|
+
export const betterAuthAction = pipe(getActionRequest, Effect.flatMap(withAuthHandler));
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectify/react-router-better-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
4
|
"description": "Integration of React Router + better-auth with Effect for React applications",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/devx-op/effectify",
|
|
8
|
+
"directory": "packages/react/router-better-auth"
|
|
9
|
+
},
|
|
5
10
|
"type": "module",
|
|
6
11
|
"main": "./dist/src/index.js",
|
|
7
12
|
"module": "./dist/src/index.js",
|
|
@@ -22,9 +27,9 @@
|
|
|
22
27
|
"!**/*.tsbuildinfo"
|
|
23
28
|
],
|
|
24
29
|
"peerDependencies": {
|
|
25
|
-
"effect": "
|
|
26
|
-
"@effectify/react-router": "0.
|
|
27
|
-
"@effectify/node-better-auth": "0.
|
|
30
|
+
"effect": "4.0.0-beta.33",
|
|
31
|
+
"@effectify/react-router": "1.0.0-alpha.0",
|
|
32
|
+
"@effectify/node-better-auth": "1.0.0-alpha.0"
|
|
28
33
|
},
|
|
29
34
|
"devDependencies": {},
|
|
30
35
|
"dependencies": {},
|