@effectify/react-router-better-auth 0.3.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.
@@ -0,0 +1,2 @@
1
+ export * from './lib/auth-guard.js';
2
+ export * from './lib/handlers.js';
@@ -0,0 +1,2 @@
1
+ export * from './lib/auth-guard.js';
2
+ export * from './lib/handlers.js';
@@ -0,0 +1,5 @@
1
+ import { AuthService } from '@effectify/node-better-auth';
2
+ import { ActionArgsContext, HttpResponseFailure, LoaderArgsContext } from '@effectify/react-router';
3
+ import * as Effect from 'effect/Effect';
4
+ export declare const withAuthGuardMiddleware: <A, E, L>(effect: Effect.Effect<A, E, LoaderArgsContext | AuthService.AuthContext | L>) => Effect.Effect<A | HttpResponseFailure<AuthService.Unauthorized>, E | AuthService.Unauthorized, LoaderArgsContext | AuthService.AuthServiceContext | L>;
5
+ export declare const withAuthGuardMiddlewareFromAction: <A, E>(effect: Effect.Effect<A, E, ActionArgsContext | AuthService.AuthContext>) => Effect.Effect<A | HttpResponseFailure<AuthService.Unauthorized>, E | AuthService.Unauthorized, ActionArgsContext | AuthService.AuthServiceContext>;
@@ -0,0 +1,52 @@
1
+ import { AuthService } from '@effectify/node-better-auth';
2
+ import { ActionArgsContext, HttpResponseFailure, httpFailure, LoaderArgsContext } from '@effectify/react-router';
3
+ import * as Effect from 'effect/Effect';
4
+ const mapHeaders = (args) => args.request.headers;
5
+ const verifySessionWithContext = (context) => Effect.gen(function* () {
6
+ const args = yield* context;
7
+ const { auth } = yield* AuthService.AuthServiceContext;
8
+ const forwardedHeaders = mapHeaders(args);
9
+ const session = yield* Effect.tryPromise({
10
+ try: () => auth.api.getSession({ headers: forwardedHeaders }),
11
+ catch: (cause) => {
12
+ const errorMessage = cause instanceof Error ? cause.message : String(cause);
13
+ return new AuthService.Unauthorized({ details: errorMessage });
14
+ },
15
+ });
16
+ if (!session) {
17
+ return yield* httpFailure(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
18
+ }
19
+ return yield* Effect.succeed({ user: session.user, session: session.session });
20
+ });
21
+ const verifySessionWithActionContext = (context) => Effect.gen(function* () {
22
+ const args = yield* context;
23
+ const { auth } = yield* AuthService.AuthServiceContext;
24
+ const forwardedHeaders = mapHeaders(args);
25
+ const session = yield* Effect.tryPromise({
26
+ try: () => auth.api.getSession({ headers: forwardedHeaders }),
27
+ catch: (cause) => {
28
+ const errorMessage = cause instanceof Error ? cause.message : String(cause);
29
+ return new AuthService.Unauthorized({ details: errorMessage });
30
+ },
31
+ });
32
+ if (!session) {
33
+ return yield* httpFailure(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
34
+ }
35
+ return yield* Effect.succeed({ user: session.user, session: session.session });
36
+ });
37
+ const verifySession = () => verifySessionWithContext(LoaderArgsContext);
38
+ const verifySessionFromAction = () => verifySessionWithActionContext(ActionArgsContext);
39
+ export const withAuthGuardMiddleware = (effect) => Effect.gen(function* () {
40
+ const authResult = yield* verifySession();
41
+ if (authResult instanceof HttpResponseFailure) {
42
+ return authResult;
43
+ }
44
+ return yield* Effect.provideService(effect, AuthService.AuthContext, authResult);
45
+ });
46
+ export const withAuthGuardMiddlewareFromAction = (effect) => Effect.gen(function* () {
47
+ const authResult = yield* verifySessionFromAction();
48
+ if (authResult instanceof HttpResponseFailure) {
49
+ return authResult;
50
+ }
51
+ return yield* Effect.provideService(effect, AuthService.AuthContext, authResult);
52
+ });
@@ -0,0 +1,5 @@
1
+ import { AuthService } from '@effectify/node-better-auth';
2
+ import { ActionArgsContext, LoaderArgsContext } from '@effectify/react-router';
3
+ import * as Effect from 'effect/Effect';
4
+ export declare const betterAuthLoader: Effect.Effect<Response, never, AuthService.AuthServiceContext | LoaderArgsContext>;
5
+ export declare const betterAuthAction: Effect.Effect<Response, never, AuthService.AuthServiceContext | ActionArgsContext>;
@@ -0,0 +1,10 @@
1
+ import { AuthService } from '@effectify/node-better-auth';
2
+ import { ActionArgsContext, LoaderArgsContext } from '@effectify/react-router';
3
+ import * as Effect from 'effect/Effect';
4
+ const getRequest = (context) => Effect.map(context, (args) => args.request);
5
+ const getRequestFromAction = (context) => Effect.map(context, (args) => args.request);
6
+ const withAuthHandler = (requestEffect) => Effect.all([requestEffect, AuthService.AuthServiceContext]).pipe(Effect.andThen(([request, auth]) => Effect.gen(function* () {
7
+ return yield* Effect.promise(() => auth.auth.handler(request));
8
+ })));
9
+ export const betterAuthLoader = LoaderArgsContext.pipe(getRequest, withAuthHandler);
10
+ export const betterAuthAction = ActionArgsContext.pipe(getRequestFromAction, withAuthHandler);
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@effectify/react-router-better-auth",
3
+ "version": "0.3.1",
4
+ "description": "Integration of React Router + better-auth with Effect for React applications",
5
+ "type": "module",
6
+ "main": "./dist/src/index.js",
7
+ "module": "./dist/src/index.js",
8
+ "types": "./dist/src/index.d.ts",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "@effectify/source": "./src/index.ts",
15
+ "types": "./dist/src/index.d.ts",
16
+ "import": "./dist/src/index.js",
17
+ "default": "./dist/src/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "!**/*.tsbuildinfo"
23
+ ],
24
+ "dependencies": {
25
+ "effect": "3.19.6",
26
+ "@effectify/react-router": "0.4.1",
27
+ "@effectify/node-better-auth": "0.4.0"
28
+ },
29
+ "devDependencies": {},
30
+ "peerDependencies": {},
31
+ "optionalDependencies": {}
32
+ }