@shivaedev/react 0.31.1 → 0.32.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/CHANGELOG.md +15 -0
- package/README.md +41 -15
- package/dist/client/Forbidden.d.ts +10 -0
- package/dist/client/Forbidden.d.ts.map +1 -0
- package/dist/client/Forbidden.js +6 -0
- package/dist/client/Forbidden.js.map +1 -0
- package/dist/client/authSession.d.ts +7 -3
- package/dist/client/authSession.d.ts.map +1 -1
- package/dist/client/authSession.js +20 -3
- package/dist/client/authSession.js.map +1 -1
- package/dist/client/permissionBoundary.d.ts +3 -0
- package/dist/client/permissionBoundary.d.ts.map +1 -1
- package/dist/client/permissionBoundary.js +2 -2
- package/dist/client/permissionBoundary.js.map +1 -1
- package/dist/permissionGuard.d.ts +2 -1
- package/dist/permissionGuard.d.ts.map +1 -1
- package/dist/permissionGuard.js +3 -3
- package/dist/permissionGuard.js.map +1 -1
- package/dist/permissions.d.ts +11 -6
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +5 -1
- package/dist/permissions.js.map +1 -1
- package/package.json +8 -8
- package/src/client/Forbidden.ts +26 -0
- package/src/client/authSession.ts +32 -6
- package/src/client/permissionBoundary.ts +2 -2
- package/src/permissionGuard.ts +4 -3
- package/src/permissions.ts +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.32.0 - 2026-11-01
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- React exposes an Effect `PermissionSession` schema alongside its inferred session type, using access's shared permission codec.
|
|
8
|
+
- React session reads expose typed permission grants while preserving provider fields.
|
|
9
|
+
- React supplies `Forbidden` with a router link back to an allowed route, and `PermissionBoundaryError` for route failures inside the router.
|
|
10
|
+
- Identity's `betterAuthMount` accepts `onPasswordReset`, an Effect callback that runs with the host's context and composes after Better Auth's native reset callback.
|
|
11
|
+
- React's `permissionGuard` accepts `hidden: true` to render TanStack Router's not-found page when a signed-in viewer lacks a required permission.
|
|
12
|
+
|
|
13
|
+
### Breaking
|
|
14
|
+
|
|
15
|
+
- React's `authSession` now requires a nullable session with a `user` object and validates its permission grants. Use `authQuery` for reads of other payload shapes; remove application session permission decoders when using `authSession`.
|
|
16
|
+
- `PermissionSession` is now a schema-derived type. Extend it with an intersection or an application interface rather than module augmentation.
|
|
17
|
+
|
|
3
18
|
## 0.31.1 - 2026-11-01
|
|
4
19
|
|
|
5
20
|
### Changed
|
package/README.md
CHANGED
|
@@ -668,12 +668,16 @@ Permissions come from the session payload's `user.permissions`, populated by `@s
|
|
|
668
668
|
|
|
669
669
|
Declare a route's requirement in TanStack's native `staticData`: `signedIn: true` admits any signed-in viewer, and `permission` requires a specific grant. Routes that declare neither stay public. Install `permissionGuard` once as the root route's `beforeLoad`; it checks all matched routes before their loaders run, including requirements on parents. Signed-out visitors go to the app's `signIn` path with the full requested location in the `returnTo` search field. A signed-in session without a required grant throws the shared `Forbidden` failure. A signed-in-only parent does not bypass a child's permission requirement.
|
|
670
670
|
|
|
671
|
+
Set `permissionGuard({ session, signIn, hidden: true })` to hide refused routes from signed-in members. The guard throws TanStack's native not-found result, renders the app's `notFoundComponent` at the requested URL and prevents the route's loader from running. Signed-out visitors still go to sign-in with their return location; sessions with the required grant still enter the route.
|
|
672
|
+
|
|
671
673
|
```tsx
|
|
672
674
|
import { RegistryContext } from "@effect/atom-react";
|
|
673
|
-
import { createRootRouteWithContext, createRoute, createRouter, RouterProvider } from "@tanstack/react-router";
|
|
675
|
+
import { createRootRouteWithContext, createRoute, createRouter, Link, RouterProvider, useRouter } from "@tanstack/react-router";
|
|
676
|
+
import type { ReactNode } from "react";
|
|
674
677
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
675
678
|
import { permission } from "@shivaedev/access/permission.ts";
|
|
676
|
-
import {
|
|
679
|
+
import { Forbidden } from "@shivaedev/react/client/Forbidden.ts";
|
|
680
|
+
import { PermissionBoundary, PermissionBoundaryError, useCan } from "@shivaedev/react/client/permissionBoundary.ts";
|
|
677
681
|
import { useLoaderQuery } from "@shivaedev/react/client/useLoaderQuery.ts";
|
|
678
682
|
import { permissionGuard } from "@shivaedev/react/permissionGuard.ts";
|
|
679
683
|
import { can, type PermissionSession } from "@shivaedev/react/permissions.ts";
|
|
@@ -682,8 +686,8 @@ import { api, registry } from "./notes-client.ts";
|
|
|
682
686
|
import { auth, SignIn } from "./auth.tsx";
|
|
683
687
|
|
|
684
688
|
const ReadNotes = permission("notes.read");
|
|
685
|
-
const root = createRootRouteWithContext<{ registry: AtomRegistry.AtomRegistry; api: typeof api }>()({
|
|
686
|
-
beforeLoad: permissionGuard({ session: () =>
|
|
689
|
+
const root = createRootRouteWithContext<{ registry: AtomRegistry.AtomRegistry; api: typeof api; session: PermissionSession | undefined }>()({
|
|
690
|
+
beforeLoad: ({ context, ...route }) => permissionGuard({ session: () => context.session, signIn: "/login" })(route),
|
|
687
691
|
});
|
|
688
692
|
const profile = createRoute({
|
|
689
693
|
getParentRoute: () => root,
|
|
@@ -715,7 +719,9 @@ const login = createRoute({
|
|
|
715
719
|
},
|
|
716
720
|
});
|
|
717
721
|
const router = createRouter({
|
|
718
|
-
context: { registry, api },
|
|
722
|
+
context: { registry, api, session: auth.session },
|
|
723
|
+
InnerWrap: PermissionShell,
|
|
724
|
+
defaultErrorComponent: PermissionBoundaryError,
|
|
719
725
|
routeTree: root.addChildren([profile, note, login]),
|
|
720
726
|
disableGlobalCatchBoundary: true,
|
|
721
727
|
});
|
|
@@ -725,23 +731,43 @@ function Controls() {
|
|
|
725
731
|
return can(ReadNotes) ? <button type="button">Read notes</button> : null;
|
|
726
732
|
}
|
|
727
733
|
|
|
728
|
-
|
|
734
|
+
function PermissionShell({ children }: { readonly children: ReactNode }): ReactNode {
|
|
735
|
+
const session = useRouter<typeof router>().options.context?.session;
|
|
729
736
|
return (
|
|
730
|
-
<PermissionBoundary session={session} fallback={<
|
|
731
|
-
<
|
|
732
|
-
|
|
733
|
-
<RouterProvider router={router} />
|
|
734
|
-
</RegistryContext.Provider>
|
|
737
|
+
<PermissionBoundary session={session} fallback={<NoAccess />}>
|
|
738
|
+
<Controls />
|
|
739
|
+
{children}
|
|
735
740
|
</PermissionBoundary>
|
|
736
741
|
);
|
|
737
742
|
}
|
|
738
743
|
|
|
744
|
+
function NoAccess(): ReactNode {
|
|
745
|
+
return (
|
|
746
|
+
<>
|
|
747
|
+
<Forbidden to="/profile" backLabel="Back to profile" />
|
|
748
|
+
<Link to="/login">Use another account</Link>
|
|
749
|
+
</>
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
export function App({ session }: { readonly session: PermissionSession | undefined }) {
|
|
754
|
+
return (
|
|
755
|
+
<RegistryContext.Provider value={registry}>
|
|
756
|
+
<RouterProvider router={router} context={{ session }} />
|
|
757
|
+
</RegistryContext.Provider>
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
|
|
739
761
|
const showNotes = can(auth.session, ReadNotes);
|
|
740
762
|
```
|
|
741
763
|
|
|
742
|
-
`auth` owns the current session and passes it to `App`; `SignIn` updates that session before invoking `onSignedIn`. Use the session's registry for router context and React, as with other query routes. Importing `permissionGuard` augments TanStack's `StaticDataRouteOption`; route options and their inference remain native.
|
|
764
|
+
`auth` owns the current session and passes it to `App`; `SignIn` updates that session before invoking `onSignedIn`. Use the session's registry for router context and React, as with other query routes. Importing `permissionGuard` augments TanStack's `StaticDataRouteOption`; route options and their inference remain native. The guard returns `void` explicitly, so `beforeLoad` needs no return annotation, including when it reads the session from route context.
|
|
765
|
+
|
|
766
|
+
`InnerWrap` puts `PermissionBoundary` inside the router. Its fallback can render `Link` and read route context. Set `defaultErrorComponent: PermissionBoundaryError` so route and loader failures reach the boundary's fallback while TanStack keeps its navigation controller mounted. This lets a refused viewer follow the fallback link without reloading the page. Keep `disableGlobalCatchBoundary: true`; other errors propagate to an enclosing application boundary. A route-specific `errorComponent` owns any failures it consumes.
|
|
767
|
+
|
|
768
|
+
`Forbidden` from `@shivaedev/react/client/Forbidden.ts` supplies the no-access heading, explanation and a router link. Set `to` to a route the viewer may open. Optional `title`, `children`, `backLabel` and `className` let the application choose wording and layout. The default heading is “Permission denied” and the link says “Go back”.
|
|
743
769
|
|
|
744
|
-
`PermissionBoundary`
|
|
770
|
+
`PermissionBoundary` also catches component query failures containing the shared `Forbidden` class. Without a boundary, query failures keep their normal typed state. TanStack resets route failures when the viewer navigates away; remount a boundary to clear a failure it catches outside the route error path.
|
|
745
771
|
|
|
746
772
|
#### Better Auth actions
|
|
747
773
|
|
|
@@ -775,7 +801,7 @@ const onSubmit = (credentials: { email: string; password: string }) => signIn(cr
|
|
|
775
801
|
|
|
776
802
|
`authCall` returns a function from input to `Effect<A, AuthFailure>`. It uses the same failure codes and successful data as `authAction`.
|
|
777
803
|
|
|
778
|
-
Use `authSession` from `@shivaedev/react/client/authSession.ts` for the application's Better Auth session. It returns a native `atom` for `useQuery` and a `refresh` Effect that refreshes that same atom and waits through its loading state. Supply Better Auth's public `getSession` call with `disableCookieCache: true` so a mutation reads current user data:
|
|
804
|
+
Use `authSession` from `@shivaedev/react/client/authSession.ts` for the application's Better Auth session. It returns a native `atom` for `useQuery` and a `refresh` Effect that refreshes that same atom and waits through its loading state. Both expose typed `user.permissions` alongside the provider's existing session and user fields, using the `PermissionSession` schema from `@shivaedev/react/permissions.ts`. Applications do not decode `get-session` themselves. Missing grants stay optional; malformed grants fail with `AuthUnavailable`. The reader must return a nullable session with a `user` object; use `authQuery` for other payload shapes. Supply Better Auth's public `getSession` call with `disableCookieCache: true` so a mutation reads current user data:
|
|
779
805
|
|
|
780
806
|
```tsx
|
|
781
807
|
import { Effect } from "effect";
|
|
@@ -1003,7 +1029,7 @@ pnpm add @shivaedev/react @effect/atom-react effect react
|
|
|
1003
1029
|
pnpm add @shivaedev/form
|
|
1004
1030
|
```
|
|
1005
1031
|
|
|
1006
|
-
Add the optional router peer when using `queryRoute`, `loadQuery`, `permissionGuard`, `PermissionBoundary` or `useSignOut`:
|
|
1032
|
+
Add the optional router peer when using `queryRoute`, `loadQuery`, `permissionGuard`, `PermissionBoundary`, `PermissionBoundaryError`, `Forbidden` or `useSignOut`:
|
|
1007
1033
|
|
|
1008
1034
|
```sh
|
|
1009
1035
|
pnpm add @tanstack/react-router
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface ForbiddenProps {
|
|
3
|
+
readonly backLabel?: string;
|
|
4
|
+
readonly children?: ReactNode;
|
|
5
|
+
readonly className?: string;
|
|
6
|
+
readonly title?: string;
|
|
7
|
+
readonly to: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function Forbidden({ to, title, children, backLabel, className, }: ForbiddenProps): ReactNode;
|
|
10
|
+
//# sourceMappingURL=Forbidden.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Forbidden.d.ts","sourceRoot":"","sources":["../../src/client/Forbidden.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEtD,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;IAC9B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,SAAS,CAAC,EACzB,EAAE,EACF,KAA2B,EAC3B,QAAiD,EACjD,SAAqB,EACrB,SAAS,GACT,EAAE,cAAc,GAAG,SAAS,CAQ5B"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Link } from "@tanstack/react-router";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
export function Forbidden({ to, title = "Permission denied", children = "You do not have access to this page.", backLabel = "Go back", className, }) {
|
|
4
|
+
return createElement("main", { className }, createElement("h1", null, title), createElement("p", null, children), createElement(Link, { to }, backLabel));
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=Forbidden.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Forbidden.js","sourceRoot":"","sources":["../../src/client/Forbidden.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAkB,MAAM,OAAO,CAAC;AAUtD,MAAM,UAAU,SAAS,CAAC,EACzB,EAAE,EACF,KAAK,GAAG,mBAAmB,EAC3B,QAAQ,GAAG,sCAAsC,EACjD,SAAS,GAAG,SAAS,EACrB,SAAS,GACO;IAChB,OAAO,aAAa,CACnB,MAAM,EACN,EAAE,SAAS,EAAE,EACb,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,EAChC,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,EAClC,aAAa,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,SAAS,CAAC,CACtC,CAAC;AACH,CAAC"}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
-
import
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
-
import
|
|
5
|
+
import { AuthUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
6
|
+
import { PermissionSession } from "#permissions.ts";
|
|
6
7
|
import type { AuthActionResult } from "./authAction.ts";
|
|
7
8
|
export interface AuthSession<A, E> {
|
|
8
9
|
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E | AuthUnavailable>>;
|
|
9
10
|
readonly refresh: Effect.Effect<A, E | AuthUnavailable, AtomRegistry.AtomRegistry>;
|
|
10
11
|
}
|
|
11
|
-
export
|
|
12
|
+
export type SessionWithPermissions<A> = A extends null ? null : A & PermissionSession;
|
|
13
|
+
export declare function authSession<A extends {
|
|
14
|
+
readonly user: object;
|
|
15
|
+
} | null, E extends object = never>(read: (signal: AbortSignal) => Promise<AuthActionResult<A, E>>): AuthSession<SessionWithPermissions<A>, E>;
|
|
12
16
|
//# sourceMappingURL=authSession.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authSession.d.ts","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"authSession.d.ts","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,MAAM,WAAW,WAAW,CAAC,CAAC,EAAE,CAAC;IAChC,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC;IAC1E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,eAAe,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;CACnF;AAED,MAAM,MAAM,sBAAsB,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,iBAAiB,CAAC;AAatF,wBAAgB,WAAW,CAAC,CAAC,SAAS;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAAE,CAAC,SAAS,MAAM,GAAG,KAAK,EAC/F,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAC5D,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAmB3C"}
|
|
@@ -1,8 +1,25 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
2
3
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
3
|
-
import {
|
|
4
|
+
import { AuthUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
5
|
+
import { PermissionSession } from "#permissions.ts";
|
|
6
|
+
function decodeSession(data) {
|
|
7
|
+
if (data === null) {
|
|
8
|
+
return Effect.succeed(null);
|
|
9
|
+
}
|
|
10
|
+
return Schema.decodeUnknownEffect(PermissionSession)(data).pipe(Effect.map((access) => ({ ...data, user: { ...data.user, ...access.user } })), Effect.mapError(() => new AuthUnavailable({ message: "Could not read session permissions" })));
|
|
11
|
+
}
|
|
4
12
|
export function authSession(read) {
|
|
5
|
-
const atom =
|
|
13
|
+
const atom = Atom.make(Effect.gen(function* () {
|
|
14
|
+
const result = yield* Effect.tryPromise({
|
|
15
|
+
catch: (cause) => new AuthUnavailable({ message: String(cause) }),
|
|
16
|
+
try: read,
|
|
17
|
+
});
|
|
18
|
+
if (result.error !== null) {
|
|
19
|
+
return yield* Effect.fail(result.error);
|
|
20
|
+
}
|
|
21
|
+
return yield* decodeSession(result.data);
|
|
22
|
+
}));
|
|
6
23
|
const refresh = Effect.gen(function* () {
|
|
7
24
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
8
25
|
registry.refresh(atom);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authSession.js","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"authSession.js","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAWpD,SAAS,aAAa,CAAC,IAAsC;IAC5D,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,MAAM,CAAC,mBAAmB,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAC9D,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAC7E,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,eAAe,CAAC,EAAE,OAAO,EAAE,oCAAoC,EAAE,CAAC,CAAC,CAC7F,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAC1B,IAA8D;IAE9D,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CACrB,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnB,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;YACvC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,eAAe,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACjE,GAAG,EAAE,IAAI;SACT,CAAC,CAAC;QACH,IAAI,MAAM,CAAC,KAAK,KAAK,IAAI,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,KAAK,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC,CAAC,CACF,CAAC;IACF,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnC,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC;QAClD,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,OAAO,KAAK,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;IAClF,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC1B,CAAC"}
|
|
@@ -7,5 +7,8 @@ export interface PermissionBoundaryProps {
|
|
|
7
7
|
readonly session: PermissionSession | null | undefined;
|
|
8
8
|
}
|
|
9
9
|
export declare function useCan(): (permission: Permission) => boolean;
|
|
10
|
+
export declare function PermissionBoundaryError({ error }: {
|
|
11
|
+
readonly error: unknown;
|
|
12
|
+
}): ReactNode;
|
|
10
13
|
export declare function PermissionBoundary({ children, fallback, session }: PermissionBoundaryProps): ReactNode;
|
|
11
14
|
//# sourceMappingURL=permissionBoundary.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionBoundary.d.ts","sourceRoot":"","sources":["../../src/client/permissionBoundary.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,SAAS,EAAc,MAAM,OAAO,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAGlE,OAAO,EAAO,KAAK,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE9D,MAAM,WAAW,uBAAuB;IACvC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;CACvD;AAED,wBAAgB,MAAM,IAAI,CAAC,UAAU,EAAE,UAAU,KAAK,OAAO,CAG5D;
|
|
1
|
+
{"version":3,"file":"permissionBoundary.d.ts","sourceRoot":"","sources":["../../src/client/permissionBoundary.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,SAAS,EAAc,MAAM,OAAO,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAGlE,OAAO,EAAO,KAAK,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE9D,MAAM,WAAW,uBAAuB;IACvC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;CACvD;AAED,wBAAgB,MAAM,IAAI,CAAC,UAAU,EAAE,UAAU,KAAK,OAAO,CAG5D;AAED,wBAAgB,uBAAuB,CAAC,EAAE,KAAK,EAAE,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAMzF;AAED,wBAAgB,kBAAkB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,uBAAuB,GAAG,SAAS,CAMtG"}
|
|
@@ -7,7 +7,7 @@ export function useCan() {
|
|
|
7
7
|
const context = useContext(PermissionContext);
|
|
8
8
|
return (permission) => can(context?.session, permission);
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
export function PermissionBoundaryError({ error }) {
|
|
11
11
|
const context = useContext(PermissionContext);
|
|
12
12
|
if (!isForbidden(error)) {
|
|
13
13
|
throw error;
|
|
@@ -15,6 +15,6 @@ function PermissionFailure({ error }) {
|
|
|
15
15
|
return context?.fallback;
|
|
16
16
|
}
|
|
17
17
|
export function PermissionBoundary({ children, fallback, session }) {
|
|
18
|
-
return createElement(PermissionContext.Provider, { value: { fallback, session } }, createElement(CatchBoundary, { children, errorComponent:
|
|
18
|
+
return createElement(PermissionContext.Provider, { value: { fallback, session } }, createElement(CatchBoundary, { children, errorComponent: PermissionBoundaryError, getResetKey: () => "permission" }));
|
|
19
19
|
}
|
|
20
20
|
//# sourceMappingURL=permissionBoundary.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionBoundary.js","sourceRoot":"","sources":["../../src/client/permissionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAkB,UAAU,EAAE,MAAM,OAAO,CAAC;AAElE,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,GAAG,EAA0B,MAAM,iBAAiB,CAAC;AAQ9D,MAAM,UAAU,MAAM;IACrB,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC9C,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;AAC1D,CAAC;AAED,
|
|
1
|
+
{"version":3,"file":"permissionBoundary.js","sourceRoot":"","sources":["../../src/client/permissionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAkB,UAAU,EAAE,MAAM,OAAO,CAAC;AAElE,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,GAAG,EAA0B,MAAM,iBAAiB,CAAC;AAQ9D,MAAM,UAAU,MAAM;IACrB,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC9C,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,EAAE,KAAK,EAA+B;IAC7E,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,KAAK,CAAC;IACb,CAAC;IACD,OAAO,OAAO,EAAE,QAAQ,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAA2B;IAC1F,OAAO,aAAa,CACnB,iBAAiB,CAAC,QAAQ,EAC1B,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAChC,aAAa,CAAC,aAAa,EAAE,EAAE,QAAQ,EAAE,cAAc,EAAE,uBAAuB,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,YAAY,EAAE,CAAC,CACpH,CAAC;AACH,CAAC"}
|
|
@@ -8,6 +8,7 @@ declare module "@tanstack/react-router" {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
export interface PermissionGuardOptions {
|
|
11
|
+
readonly hidden?: boolean;
|
|
11
12
|
readonly session: () => PermissionSession | null | undefined;
|
|
12
13
|
readonly signIn: string;
|
|
13
14
|
}
|
|
@@ -20,5 +21,5 @@ export interface PermissionGuardContext {
|
|
|
20
21
|
};
|
|
21
22
|
}[];
|
|
22
23
|
}
|
|
23
|
-
export declare function permissionGuard({ session, signIn }: PermissionGuardOptions): (context: PermissionGuardContext) => void;
|
|
24
|
+
export declare function permissionGuard({ session, signIn, hidden }: PermissionGuardOptions): (context: PermissionGuardContext) => void;
|
|
24
25
|
//# sourceMappingURL=permissionGuard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionGuard.d.ts","sourceRoot":"","sources":["../src/permissionGuard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,EAAO,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,KAAK,cAAc,EAAgB,MAAM,mBAAmB,CAAC;AAEtE,OAAO,QAAQ,wBAAwB,CAAC;IACvC,UAAU,qBAAqB;QAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;QACjC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAC5B;CACD;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,OAAO,EAAE,MAAM,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE;YAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;YAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;SAAE,CAAA;KAAE,EAAE,CAAC;CACxH;AAED,wBAAgB,eAAe,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,sBAAsB,GAAG,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,
|
|
1
|
+
{"version":3,"file":"permissionGuard.d.ts","sourceRoot":"","sources":["../src/permissionGuard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,EAAO,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,KAAK,cAAc,EAAgB,MAAM,mBAAmB,CAAC;AAEtE,OAAO,QAAQ,wBAAwB,CAAC;IACvC,UAAU,qBAAqB;QAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;QACjC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAC5B;CACD;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE;YAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;YAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;SAAE,CAAA;KAAE,EAAE,CAAC;CACxH;AAED,wBAAgB,eAAe,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAc,EAAE,EAAE,sBAAsB,GAAG,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,CAgBtI"}
|
package/dist/permissionGuard.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { redirect } from "@tanstack/react-router";
|
|
1
|
+
import { notFound, redirect } from "@tanstack/react-router";
|
|
2
2
|
import { forbidden } from "@shivaedev/access/refusal.ts";
|
|
3
3
|
import { can } from "./permissions.js";
|
|
4
4
|
import { signInSearch } from "./signInSearch.js";
|
|
5
|
-
export function permissionGuard({ session, signIn }) {
|
|
5
|
+
export function permissionGuard({ session, signIn, hidden = false }) {
|
|
6
6
|
return ({ matches, location }) => {
|
|
7
7
|
const required = matches.flatMap((match) => (match.staticData.permission === undefined ? [] : [match.staticData.permission]));
|
|
8
8
|
if (required.length === 0 && !matches.some((match) => match.staticData.signedIn === true)) {
|
|
@@ -14,7 +14,7 @@ export function permissionGuard({ session, signIn }) {
|
|
|
14
14
|
}
|
|
15
15
|
for (const permission of required) {
|
|
16
16
|
if (!can(current, permission)) {
|
|
17
|
-
throw forbidden(permission);
|
|
17
|
+
throw hidden ? notFound() : forbidden(permission);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionGuard.js","sourceRoot":"","sources":["../src/permissionGuard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"permissionGuard.js","sourceRoot":"","sources":["../src/permissionGuard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAE5D,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AACzD,OAAO,EAAE,GAAG,EAA0B,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAuB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAoBtE,MAAM,UAAU,eAAe,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,KAAK,EAA0B;IAC1F,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;QAChC,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC9H,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,QAAQ,KAAK,IAAI,CAAC,EAAE,CAAC;YAC3F,OAAO;QACR,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,EAAE,CAAC;QAC1B,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC/C,MAAM,QAAQ,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAChE,CAAC;QACD,KAAK,MAAM,UAAU,IAAI,QAAQ,EAAE,CAAC;YACnC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC,EAAE,CAAC;gBAC/B,MAAM,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;YACnD,CAAC;QACF,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
|
package/dist/permissions.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Permission } from "@shivaedev/access/permission.ts";
|
|
3
|
+
export declare const PermissionSession: Schema.Struct<{
|
|
4
|
+
readonly user: Schema.Struct<{
|
|
5
|
+
readonly permissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
6
|
+
readonly name: Schema.String;
|
|
7
|
+
readonly strength: Schema.Literals<readonly ["own", "any"]>;
|
|
8
|
+
}>>>;
|
|
9
|
+
}>;
|
|
10
|
+
}>;
|
|
11
|
+
export type PermissionSession = typeof PermissionSession.Type;
|
|
7
12
|
export declare function can(session: PermissionSession | null | undefined, permission: Permission): boolean;
|
|
8
13
|
//# sourceMappingURL=permissions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissions.d.ts","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"permissions.d.ts","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAiB,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE5E,eAAO,MAAM,iBAAiB;;;;;;;EAE5B,CAAC;AACH,MAAM,MAAM,iBAAiB,GAAG,OAAO,iBAAiB,CAAC,IAAI,CAAC;AAE9D,wBAAgB,GAAG,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAElG"}
|
package/dist/permissions.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { hasPermission, Permission } from "@shivaedev/access/permission.ts";
|
|
3
|
+
export const PermissionSession = Schema.Struct({
|
|
4
|
+
user: Schema.Struct({ permissions: Schema.optionalKey(Schema.Array(Permission)) }),
|
|
5
|
+
});
|
|
2
6
|
export function can(session, permission) {
|
|
3
7
|
return hasPermission(session?.user.permissions, permission);
|
|
4
8
|
}
|
package/dist/permissions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE5E,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC9C,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;CAClF,CAAC,CAAC;AAGH,MAAM,UAAU,GAAG,CAAC,OAA6C,EAAE,UAAsB;IACxF,OAAO,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;AAC7D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0",
|
|
4
4
|
"description": "React query, action, editor and session hooks for native Effect atoms",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -56,14 +56,14 @@
|
|
|
56
56
|
"@effect/sql-pg": "4.0.6",
|
|
57
57
|
"@effect/sql-sqlite-node": "4.0.6",
|
|
58
58
|
"@shivaedev/changes": "0.6.0",
|
|
59
|
-
"@shivaedev/identity": "0.
|
|
60
|
-
"@shivaedev/jobs": "0.29.
|
|
61
|
-
"@shivaedev/quality": "0.
|
|
62
|
-
"@shivaedev/server": "0.
|
|
59
|
+
"@shivaedev/identity": "0.17.0",
|
|
60
|
+
"@shivaedev/jobs": "0.29.1",
|
|
61
|
+
"@shivaedev/quality": "0.54.0",
|
|
62
|
+
"@shivaedev/server": "0.17.0",
|
|
63
63
|
"@shivaedev/service": "0.6.0",
|
|
64
|
-
"@shivaedev/sql": "0.
|
|
65
|
-
"@shivaedev/test-sql": "0.
|
|
66
|
-
"@shivaedev/test-story": "0.
|
|
64
|
+
"@shivaedev/sql": "0.31.0",
|
|
65
|
+
"@shivaedev/test-sql": "0.16.0",
|
|
66
|
+
"@shivaedev/test-story": "0.32.0",
|
|
67
67
|
"@tanstack/react-router": "1.170.46",
|
|
68
68
|
"@types/node": "24.19.2",
|
|
69
69
|
"@types/react": "19.3.0",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Link } from "@tanstack/react-router";
|
|
2
|
+
import { createElement, type ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
export interface ForbiddenProps {
|
|
5
|
+
readonly backLabel?: string;
|
|
6
|
+
readonly children?: ReactNode;
|
|
7
|
+
readonly className?: string;
|
|
8
|
+
readonly title?: string;
|
|
9
|
+
readonly to: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function Forbidden({
|
|
13
|
+
to,
|
|
14
|
+
title = "Permission denied",
|
|
15
|
+
children = "You do not have access to this page.",
|
|
16
|
+
backLabel = "Go back",
|
|
17
|
+
className,
|
|
18
|
+
}: ForbiddenProps): ReactNode {
|
|
19
|
+
return createElement(
|
|
20
|
+
"main",
|
|
21
|
+
{ className },
|
|
22
|
+
createElement("h1", null, title),
|
|
23
|
+
createElement("p", null, children),
|
|
24
|
+
createElement(Link, { to }, backLabel),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -1,18 +1,44 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
-
import
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
-
import
|
|
5
|
+
import { AuthUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
6
|
+
import { PermissionSession } from "#permissions.ts";
|
|
6
7
|
import type { AuthActionResult } from "./authAction.ts";
|
|
7
|
-
import { authQuery } from "./authQuery.ts";
|
|
8
8
|
|
|
9
9
|
export interface AuthSession<A, E> {
|
|
10
10
|
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E | AuthUnavailable>>;
|
|
11
11
|
readonly refresh: Effect.Effect<A, E | AuthUnavailable, AtomRegistry.AtomRegistry>;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export
|
|
15
|
-
|
|
14
|
+
export type SessionWithPermissions<A> = A extends null ? null : A & PermissionSession;
|
|
15
|
+
|
|
16
|
+
function decodeSession<A extends { readonly user: object } | null>(data: A): Effect.Effect<SessionWithPermissions<A>, AuthUnavailable>;
|
|
17
|
+
function decodeSession(data: { readonly user: object } | null) {
|
|
18
|
+
if (data === null) {
|
|
19
|
+
return Effect.succeed(null);
|
|
20
|
+
}
|
|
21
|
+
return Schema.decodeUnknownEffect(PermissionSession)(data).pipe(
|
|
22
|
+
Effect.map((access) => ({ ...data, user: { ...data.user, ...access.user } })),
|
|
23
|
+
Effect.mapError(() => new AuthUnavailable({ message: "Could not read session permissions" })),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function authSession<A extends { readonly user: object } | null, E extends object = never>(
|
|
28
|
+
read: (signal: AbortSignal) => Promise<AuthActionResult<A, E>>,
|
|
29
|
+
): AuthSession<SessionWithPermissions<A>, E> {
|
|
30
|
+
const atom = Atom.make(
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const result = yield* Effect.tryPromise({
|
|
33
|
+
catch: (cause) => new AuthUnavailable({ message: String(cause) }),
|
|
34
|
+
try: read,
|
|
35
|
+
});
|
|
36
|
+
if (result.error !== null) {
|
|
37
|
+
return yield* Effect.fail(result.error);
|
|
38
|
+
}
|
|
39
|
+
return yield* decodeSession(result.data);
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
16
42
|
const refresh = Effect.gen(function* () {
|
|
17
43
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
18
44
|
registry.refresh(atom);
|
|
@@ -16,7 +16,7 @@ export function useCan(): (permission: Permission) => boolean {
|
|
|
16
16
|
return (permission) => can(context?.session, permission);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
function
|
|
19
|
+
export function PermissionBoundaryError({ error }: { readonly error: unknown }): ReactNode {
|
|
20
20
|
const context = useContext(PermissionContext);
|
|
21
21
|
if (!isForbidden(error)) {
|
|
22
22
|
throw error;
|
|
@@ -28,6 +28,6 @@ export function PermissionBoundary({ children, fallback, session }: PermissionBo
|
|
|
28
28
|
return createElement(
|
|
29
29
|
PermissionContext.Provider,
|
|
30
30
|
{ value: { fallback, session } },
|
|
31
|
-
createElement(CatchBoundary, { children, errorComponent:
|
|
31
|
+
createElement(CatchBoundary, { children, errorComponent: PermissionBoundaryError, getResetKey: () => "permission" }),
|
|
32
32
|
);
|
|
33
33
|
}
|
package/src/permissionGuard.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { redirect } from "@tanstack/react-router";
|
|
1
|
+
import { notFound, redirect } from "@tanstack/react-router";
|
|
2
2
|
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
3
3
|
import { forbidden } from "@shivaedev/access/refusal.ts";
|
|
4
4
|
import { can, type PermissionSession } from "./permissions.ts";
|
|
@@ -12,6 +12,7 @@ declare module "@tanstack/react-router" {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface PermissionGuardOptions {
|
|
15
|
+
readonly hidden?: boolean;
|
|
15
16
|
readonly session: () => PermissionSession | null | undefined;
|
|
16
17
|
readonly signIn: string;
|
|
17
18
|
}
|
|
@@ -21,7 +22,7 @@ export interface PermissionGuardContext {
|
|
|
21
22
|
readonly matches: readonly { readonly staticData: { readonly permission?: Permission; readonly signedIn?: boolean } }[];
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export function permissionGuard({ session, signIn }: PermissionGuardOptions): (context: PermissionGuardContext) => void {
|
|
25
|
+
export function permissionGuard({ session, signIn, hidden = false }: PermissionGuardOptions): (context: PermissionGuardContext) => void {
|
|
25
26
|
return ({ matches, location }) => {
|
|
26
27
|
const required = matches.flatMap((match) => (match.staticData.permission === undefined ? [] : [match.staticData.permission]));
|
|
27
28
|
if (required.length === 0 && !matches.some((match) => match.staticData.signedIn === true)) {
|
|
@@ -33,7 +34,7 @@ export function permissionGuard({ session, signIn }: PermissionGuardOptions): (c
|
|
|
33
34
|
}
|
|
34
35
|
for (const permission of required) {
|
|
35
36
|
if (!can(current, permission)) {
|
|
36
|
-
throw forbidden(permission);
|
|
37
|
+
throw hidden ? notFound() : forbidden(permission);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
};
|
package/src/permissions.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { hasPermission, Permission } from "@shivaedev/access/permission.ts";
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
4
|
+
export const PermissionSession = Schema.Struct({
|
|
5
|
+
user: Schema.Struct({ permissions: Schema.optionalKey(Schema.Array(Permission)) }),
|
|
6
|
+
});
|
|
7
|
+
export type PermissionSession = typeof PermissionSession.Type;
|
|
8
8
|
|
|
9
9
|
export function can(session: PermissionSession | null | undefined, permission: Permission): boolean {
|
|
10
10
|
return hasPermission(session?.user.permissions, permission);
|