@effectify/react-router-better-auth 0.3.2 → 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.
@@ -1,5 +1,18 @@
1
1
  import { AuthService } from '@effectify/node-better-auth';
2
- import { ActionArgsContext, HttpResponseFailure, LoaderArgsContext } from '@effectify/react-router';
2
+ import { ActionArgsContext, HttpResponse, LoaderArgsContext } from '@effectify/react-router';
3
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>;
4
+ export interface AuthGuardOptions {
5
+ redirectOnFail?: string;
6
+ redirectInit?: ResponseInit;
7
+ }
8
+ type WithBetterAuthGuard = {
9
+ <A, E, L>(eff: Effect.Effect<A, E, LoaderArgsContext | AuthService.AuthContext | L>): Effect.Effect<A, E | AuthService.Unauthorized, LoaderArgsContext | AuthService.AuthServiceContext | L>;
10
+ with: (options: AuthGuardOptions) => <A, E, L>(eff: Effect.Effect<HttpResponse<A> | Response, E, LoaderArgsContext | AuthService.AuthContext | L>) => Effect.Effect<HttpResponse<A> | Response, E, LoaderArgsContext | AuthService.AuthServiceContext | L>;
11
+ };
12
+ export declare const withBetterAuthGuard: WithBetterAuthGuard;
13
+ type WithBetterAuthGuardAction = {
14
+ <A, E>(eff: Effect.Effect<A, E, ActionArgsContext | AuthService.AuthContext>): Effect.Effect<A, E | AuthService.Unauthorized, ActionArgsContext | AuthService.AuthServiceContext>;
15
+ with: (options: AuthGuardOptions) => <A, E>(eff: Effect.Effect<HttpResponse<A> | Response, E, ActionArgsContext | AuthService.AuthContext>) => Effect.Effect<HttpResponse<A> | Response, E, ActionArgsContext | AuthService.AuthServiceContext>;
16
+ };
17
+ export declare const withBetterAuthGuardAction: WithBetterAuthGuardAction;
18
+ export {};
@@ -1,6 +1,7 @@
1
1
  import { AuthService } from '@effectify/node-better-auth';
2
- import { ActionArgsContext, HttpResponseFailure, httpFailure, LoaderArgsContext } from '@effectify/react-router';
2
+ import { ActionArgsContext, LoaderArgsContext } from '@effectify/react-router';
3
3
  import * as Effect from 'effect/Effect';
4
+ import { redirect } from 'react-router';
4
5
  const mapHeaders = (args) => args.request.headers;
5
6
  const verifySessionWithContext = (context) => Effect.gen(function* () {
6
7
  const args = yield* context;
@@ -14,7 +15,7 @@ const verifySessionWithContext = (context) => Effect.gen(function* () {
14
15
  },
15
16
  });
16
17
  if (!session) {
17
- return yield* httpFailure(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
18
+ return yield* Effect.fail(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
18
19
  }
19
20
  return yield* Effect.succeed({ user: session.user, session: session.session });
20
21
  });
@@ -30,23 +31,25 @@ const verifySessionWithActionContext = (context) => Effect.gen(function* () {
30
31
  },
31
32
  });
32
33
  if (!session) {
33
- return yield* httpFailure(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
34
+ return yield* Effect.fail(new AuthService.Unauthorized({ details: 'Missing or invalid authentication' }));
34
35
  }
35
36
  return yield* Effect.succeed({ user: session.user, session: session.session });
36
37
  });
37
38
  const verifySession = () => verifySessionWithContext(LoaderArgsContext);
38
39
  const verifySessionFromAction = () => verifySessionWithActionContext(ActionArgsContext);
39
- export const withAuthGuardMiddleware = (effect) => Effect.gen(function* () {
40
+ export const withBetterAuthGuard = Object.assign(((eff) => Effect.gen(function* () {
40
41
  const authResult = yield* verifySession();
41
- if (authResult instanceof HttpResponseFailure) {
42
- return authResult;
43
- }
44
- return yield* Effect.provideService(effect, AuthService.AuthContext, authResult);
42
+ return yield* Effect.provideService(eff, AuthService.AuthContext, authResult);
43
+ })), {
44
+ with: (opts) => (eff) => Effect.gen(function* () {
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
+ }),
45
47
  });
46
- export const withAuthGuardMiddlewareFromAction = (effect) => Effect.gen(function* () {
48
+ export const withBetterAuthGuardAction = Object.assign(((eff) => Effect.gen(function* () {
47
49
  const authResult = yield* verifySessionFromAction();
48
- if (authResult instanceof HttpResponseFailure) {
49
- return authResult;
50
- }
51
- return yield* Effect.provideService(effect, AuthService.AuthContext, authResult);
50
+ return yield* Effect.provideService(eff, AuthService.AuthContext, authResult);
51
+ })), {
52
+ with: (opts) => (eff) => Effect.gen(function* () {
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
+ }),
52
55
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectify/react-router-better-auth",
3
- "version": "0.3.2",
3
+ "version": "0.4.1",
4
4
  "description": "Integration of React Router + better-auth with Effect for React applications",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -23,8 +23,8 @@
23
23
  ],
24
24
  "peerDependencies": {
25
25
  "effect": "3.19.13",
26
- "@effectify/react-router": "0.4.2",
27
- "@effectify/node-better-auth": "0.4.1"
26
+ "@effectify/node-better-auth": "0.4.9",
27
+ "@effectify/react-router": "0.4.9"
28
28
  },
29
29
  "devDependencies": {},
30
30
  "dependencies": {},