snipe-auth-rbac 0.1.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.
@@ -0,0 +1,349 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/react/index.ts
21
+ var react_exports = {};
22
+ __export(react_exports, {
23
+ AuthRbacProvider: () => AuthRbacProvider,
24
+ Can: () => Can,
25
+ RequirePermission: () => RequirePermission,
26
+ createHttpFetcher: () => createHttpFetcher,
27
+ createSupabaseFetcher: () => createSupabaseFetcher,
28
+ defineAuthRbac: () => defineAuthRbac2,
29
+ useActiveCompany: () => useActiveCompany,
30
+ useAuthRbac: () => useAuthRbac,
31
+ useCan: () => useCan,
32
+ useFrontendConfig: () => useFrontendConfig
33
+ });
34
+ module.exports = __toCommonJS(react_exports);
35
+
36
+ // src/react/AuthRbacProvider.tsx
37
+ var import_react = require("react");
38
+
39
+ // src/client.ts
40
+ function buildPermissionResolver(resources, profile, defaultCompanyId) {
41
+ const scopeByResource = new Map(
42
+ resources.map((r) => [r.resource, r.scope])
43
+ );
44
+ const can = (resource, action, options) => {
45
+ if (profile.is_super_admin) {
46
+ return true;
47
+ }
48
+ const scope = scopeByResource.get(resource);
49
+ if (!scope) {
50
+ return false;
51
+ }
52
+ if (scope === "system") {
53
+ return readGrid(profile.system_permissions, resource, action);
54
+ }
55
+ const companyId = options?.companyId ?? defaultCompanyId;
56
+ if (!companyId) {
57
+ return false;
58
+ }
59
+ const membership = profile.memberships.find(
60
+ (m) => m.company_id === companyId
61
+ );
62
+ if (!membership) {
63
+ return false;
64
+ }
65
+ return readGrid(membership.permissions, resource, action);
66
+ };
67
+ return {
68
+ can,
69
+ /** Permission map for the active (or specified) company. */
70
+ activePermissions: (companyId) => {
71
+ const id = companyId ?? defaultCompanyId;
72
+ if (!id) {
73
+ return {};
74
+ }
75
+ return profile.memberships.find((m) => m.company_id === id)?.permissions ?? {};
76
+ },
77
+ systemPermissions: () => profile.system_permissions
78
+ };
79
+ }
80
+ function readGrid(map, resource, action) {
81
+ const grid = map[resource];
82
+ if (!grid) {
83
+ return false;
84
+ }
85
+ return grid[action];
86
+ }
87
+
88
+ // src/react/AuthRbacProvider.tsx
89
+ var import_jsx_runtime = require("react/jsx-runtime");
90
+ var AuthRbacContext = (0, import_react.createContext)(null);
91
+ var STORAGE_KEY = "auth-rbac:active-company";
92
+ var defaultPersist = (id) => {
93
+ if (typeof window === "undefined") {
94
+ return;
95
+ }
96
+ try {
97
+ if (id == null) {
98
+ window.localStorage.removeItem(STORAGE_KEY);
99
+ } else {
100
+ window.localStorage.setItem(STORAGE_KEY, id);
101
+ }
102
+ } catch {
103
+ }
104
+ };
105
+ var readPersisted = () => {
106
+ if (typeof window === "undefined") {
107
+ return null;
108
+ }
109
+ try {
110
+ return window.localStorage.getItem(STORAGE_KEY);
111
+ } catch {
112
+ return null;
113
+ }
114
+ };
115
+ function AuthRbacProvider(props) {
116
+ const { fetcher, resources, initialCompanyId, persistActiveCompany } = props;
117
+ const [profile, setProfile] = (0, import_react.useState)(null);
118
+ const [loading, setLoading] = (0, import_react.useState)(true);
119
+ const [error, setError] = (0, import_react.useState)(null);
120
+ const [activeCompanyId, setActiveCompanyState] = (0, import_react.useState)(
121
+ initialCompanyId ?? readPersisted()
122
+ );
123
+ const persist = (0, import_react.useMemo)(() => {
124
+ if (persistActiveCompany === false) {
125
+ return () => {
126
+ };
127
+ }
128
+ return persistActiveCompany ?? defaultPersist;
129
+ }, [persistActiveCompany]);
130
+ const setActiveCompany = (0, import_react.useCallback)(
131
+ (id) => {
132
+ setActiveCompanyState(id);
133
+ persist(id);
134
+ },
135
+ [persist]
136
+ );
137
+ const refresh = (0, import_react.useCallback)(async () => {
138
+ setLoading(true);
139
+ setError(null);
140
+ try {
141
+ const next = await fetcher.fetchProfile();
142
+ setProfile(next);
143
+ const stillMember = activeCompanyId != null && next.memberships.some((m) => m.company_id === activeCompanyId);
144
+ if (!stillMember) {
145
+ const fallback = next.memberships[0]?.company_id ?? null;
146
+ setActiveCompanyState(fallback);
147
+ persist(fallback);
148
+ }
149
+ } catch (e) {
150
+ setError(e instanceof Error ? e : new Error(String(e)));
151
+ } finally {
152
+ setLoading(false);
153
+ }
154
+ }, [fetcher]);
155
+ (0, import_react.useEffect)(() => {
156
+ void refresh();
157
+ }, [refresh]);
158
+ const resolver = (0, import_react.useMemo)(() => {
159
+ if (profile == null) {
160
+ return {
161
+ can: () => false,
162
+ activePermissions: () => ({}),
163
+ systemPermissions: () => ({})
164
+ };
165
+ }
166
+ return buildPermissionResolver(resources, profile, activeCompanyId);
167
+ }, [profile, resources, activeCompanyId]);
168
+ const value = (0, import_react.useMemo)(
169
+ () => ({
170
+ profile,
171
+ loading,
172
+ error,
173
+ resources,
174
+ activeCompanyId,
175
+ setActiveCompany,
176
+ refresh,
177
+ resolver
178
+ }),
179
+ [
180
+ profile,
181
+ loading,
182
+ error,
183
+ resources,
184
+ activeCompanyId,
185
+ setActiveCompany,
186
+ refresh,
187
+ resolver
188
+ ]
189
+ );
190
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthRbacContext.Provider, { value, children: props.children });
191
+ }
192
+ function useAuthRbac() {
193
+ const ctx = (0, import_react.useContext)(AuthRbacContext);
194
+ if (!ctx) {
195
+ throw new Error(
196
+ "useAuthRbac must be used within an <AuthRbacProvider> \u2014 wrap your app at the root."
197
+ );
198
+ }
199
+ return ctx;
200
+ }
201
+
202
+ // src/react/useCan.ts
203
+ function useCan(resource, action, options) {
204
+ const { resolver } = useAuthRbac();
205
+ return resolver.can(resource, action, options);
206
+ }
207
+
208
+ // src/react/Can.tsx
209
+ var import_jsx_runtime2 = require("react/jsx-runtime");
210
+ function Can(props) {
211
+ const { resource, action, companyId, children, fallback = null } = props;
212
+ const allowed = useCan(resource, action, { companyId });
213
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: allowed ? children : fallback });
214
+ }
215
+
216
+ // src/react/RequirePermission.tsx
217
+ var import_jsx_runtime3 = require("react/jsx-runtime");
218
+ function RequirePermission(props) {
219
+ const {
220
+ resource,
221
+ action,
222
+ companyId,
223
+ loadingFallback = null,
224
+ deniedFallback = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { role: "alert", style: { padding: 24 }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("strong", { children: "Sie haben keinen Zugriff." }) }),
225
+ children = null
226
+ } = props;
227
+ const { profile, loading } = useAuthRbac();
228
+ const allowed = useCan(resource, action, { companyId });
229
+ if (loading || profile == null) {
230
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: loadingFallback });
231
+ }
232
+ if (!allowed) {
233
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: deniedFallback });
234
+ }
235
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
236
+ }
237
+
238
+ // src/react/useActiveCompany.ts
239
+ var import_react2 = require("react");
240
+ function useActiveCompany() {
241
+ const { profile, activeCompanyId, setActiveCompany } = useAuthRbac();
242
+ return (0, import_react2.useMemo)(() => {
243
+ const memberships = profile?.memberships ?? [];
244
+ const membership = memberships.find((m) => m.company_id === activeCompanyId) ?? null;
245
+ return {
246
+ id: activeCompanyId,
247
+ membership,
248
+ memberships,
249
+ setActive: setActiveCompany
250
+ };
251
+ }, [profile, activeCompanyId, setActiveCompany]);
252
+ }
253
+
254
+ // src/react/useFrontendConfig.ts
255
+ var import_react3 = require("react");
256
+ function useFrontendConfig() {
257
+ const { profile, activeCompanyId } = useAuthRbac();
258
+ return (0, import_react3.useMemo)(() => {
259
+ if (!profile) {
260
+ return {};
261
+ }
262
+ const membershipConfig = profile.memberships.find((m) => m.company_id === activeCompanyId)?.frontend_config ?? {};
263
+ return { ...profile.system_frontend_config, ...membershipConfig };
264
+ }, [profile, activeCompanyId]);
265
+ }
266
+
267
+ // src/fetchers.ts
268
+ function createSupabaseFetcher(opts) {
269
+ return {
270
+ async fetchProfile() {
271
+ const { data, error } = await opts.supabase.rpc(
272
+ "auth_rbac_user_profile",
273
+ { p_user_id: opts.userId }
274
+ );
275
+ if (error) {
276
+ throw new Error(
277
+ `auth-rbac: failed to load user profile via Supabase RPC: ${error.message}`
278
+ );
279
+ }
280
+ return normalizeProfile(data);
281
+ }
282
+ };
283
+ }
284
+ function createHttpFetcher(opts) {
285
+ const fetchImpl = opts.fetch ?? globalThis.fetch;
286
+ return {
287
+ async fetchProfile() {
288
+ const res = await fetchImpl(opts.url, opts.init);
289
+ if (!res.ok) {
290
+ throw new Error(
291
+ `auth-rbac: profile endpoint ${opts.url} returned ${res.status}`
292
+ );
293
+ }
294
+ const json = await res.json();
295
+ return normalizeProfile(json);
296
+ }
297
+ };
298
+ }
299
+ function normalizeProfile(raw) {
300
+ if (!raw || typeof raw !== "object") {
301
+ throw new Error("auth-rbac: profile payload is not an object");
302
+ }
303
+ const p = raw;
304
+ if (typeof p.user_id !== "string") {
305
+ throw new Error("auth-rbac: profile payload missing user_id");
306
+ }
307
+ return {
308
+ user_id: p.user_id,
309
+ is_super_admin: !!p.is_super_admin,
310
+ system_roles: Array.isArray(p.system_roles) ? p.system_roles : [],
311
+ system_permissions: p.system_permissions && typeof p.system_permissions === "object" ? p.system_permissions : {},
312
+ system_frontend_config: p.system_frontend_config && typeof p.system_frontend_config === "object" ? p.system_frontend_config : {},
313
+ memberships: Array.isArray(p.memberships) ? p.memberships : []
314
+ };
315
+ }
316
+
317
+ // src/define.ts
318
+ function defineAuthRbac(resources, runtime) {
319
+ return {
320
+ useCan: runtime.useCan,
321
+ Can: runtime.Can,
322
+ RequirePermission: runtime.RequirePermission,
323
+ resources,
324
+ resourceNames: resources.map((r) => r.resource)
325
+ };
326
+ }
327
+
328
+ // src/react/index.ts
329
+ function defineAuthRbac2(resources) {
330
+ return defineAuthRbac(resources, {
331
+ useCan,
332
+ Can,
333
+ RequirePermission
334
+ });
335
+ }
336
+ // Annotate the CommonJS export names for ESM import in node:
337
+ 0 && (module.exports = {
338
+ AuthRbacProvider,
339
+ Can,
340
+ RequirePermission,
341
+ createHttpFetcher,
342
+ createSupabaseFetcher,
343
+ defineAuthRbac,
344
+ useActiveCompany,
345
+ useAuthRbac,
346
+ useCan,
347
+ useFrontendConfig
348
+ });
349
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react/index.ts","../../src/react/AuthRbacProvider.tsx","../../src/client.ts","../../src/react/useCan.ts","../../src/react/Can.tsx","../../src/react/RequirePermission.tsx","../../src/react/useActiveCompany.ts","../../src/react/useFrontendConfig.ts","../../src/fetchers.ts","../../src/define.ts"],"sourcesContent":["/**\n * React + Next.js entry. Import this in browser code:\n *\n * import { AuthRbacProvider, useCan } from \"snipe-auth-rbac/react\";\n *\n * The non-React entry (`snipe-auth-rbac`) re-exports types and the\n * pure resolver, suitable for Node, edge workers, and tests.\n */\n\nexport {\n AuthRbacProvider,\n useAuthRbac,\n type AuthRbacProviderProps,\n} from \"./AuthRbacProvider.js\";\n\nexport { useCan } from \"./useCan.js\";\nexport { Can, type CanProps } from \"./Can.js\";\nexport {\n RequirePermission,\n type RequirePermissionProps,\n} from \"./RequirePermission.js\";\nexport { useActiveCompany, type ActiveCompany } from \"./useActiveCompany.js\";\nexport { useFrontendConfig } from \"./useFrontendConfig.js\";\n\n// Re-exports for convenience so consumers don't need two imports.\nexport type {\n Action,\n AuthRbacFetcher,\n CompanyMembership,\n FrontendConfig,\n PermissionGrid,\n PermissionMap,\n ResourceDescriptor,\n ResourceRegistry,\n ResourceScope,\n RoleSummary,\n UserProfile,\n} from \"../types.js\";\n\nexport {\n createSupabaseFetcher,\n createHttpFetcher,\n} from \"../fetchers.js\";\n\nimport { defineAuthRbac as _defineAuthRbac } from \"../define.js\";\nimport { Can } from \"./Can.js\";\nimport { RequirePermission } from \"./RequirePermission.js\";\nimport { useCan } from \"./useCan.js\";\n\nimport type { ResourceDescriptor } from \"../types.js\";\nimport type { TypedGuards } from \"../define.js\";\n\n/**\n * Typed factory — pass a const-asserted resource registry and get\n * back guards whose `resource` arg is constrained to the registered\n * names. Recommended at the top of every host project.\n *\n * See ../define.ts for the full doc + example.\n */\nexport function defineAuthRbac<\n const Reg extends ReadonlyArray<ResourceDescriptor>,\n>(resources: Reg): TypedGuards<Reg[number][\"resource\"]> {\n return _defineAuthRbac(resources, {\n useCan,\n Can,\n RequirePermission,\n });\n}\n\nexport type { TypedGuards } from \"../define.js\";\n","import {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n type ReactNode,\n} from \"react\";\n\nimport {\n buildPermissionResolver,\n type AuthRbacClient,\n} from \"../client.js\";\nimport type {\n AuthRbacFetcher,\n PermissionMap,\n ResourceRegistry,\n UserProfile,\n} from \"../types.js\";\n\ninterface AuthRbacContextValue {\n /**\n * `null` means we haven't hydrated yet. Components should treat\n * the not-loaded state as \"no permissions\" (fail-closed).\n */\n profile: UserProfile | null;\n loading: boolean;\n error: Error | null;\n resources: ResourceRegistry;\n activeCompanyId: string | null;\n setActiveCompany: (id: string | null) => void;\n refresh: () => Promise<void>;\n resolver: AuthRbacClient;\n}\n\nconst AuthRbacContext = createContext<AuthRbacContextValue | null>(null);\n\nexport interface AuthRbacProviderProps {\n fetcher: AuthRbacFetcher;\n resources: ResourceRegistry;\n /**\n * Initial active company. Common patterns:\n * - read from URL query/path\n * - read from localStorage\n * - omit and let the user pick from the switcher\n */\n initialCompanyId?: string | null;\n /**\n * Persistence hook. Called every time the active company changes.\n * Default: writes to `localStorage` under\n * `auth-rbac:active-company`. Pass `false` to disable.\n */\n persistActiveCompany?:\n | ((id: string | null) => void)\n | false;\n children: ReactNode;\n}\n\nconst STORAGE_KEY = \"auth-rbac:active-company\";\n\nconst defaultPersist = (id: string | null) => {\n if (typeof window === \"undefined\") {\n return;\n }\n try {\n if (id == null) {\n window.localStorage.removeItem(STORAGE_KEY);\n } else {\n window.localStorage.setItem(STORAGE_KEY, id);\n }\n } catch {\n // localStorage may be unavailable (private browsing, SSR) —\n // fall back to in-memory only.\n }\n};\n\nconst readPersisted = (): string | null => {\n if (typeof window === \"undefined\") {\n return null;\n }\n try {\n return window.localStorage.getItem(STORAGE_KEY);\n } catch {\n return null;\n }\n};\n\nexport function AuthRbacProvider(props: AuthRbacProviderProps) {\n const { fetcher, resources, initialCompanyId, persistActiveCompany } = props;\n\n const [profile, setProfile] = useState<UserProfile | null>(null);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n const [activeCompanyId, setActiveCompanyState] = useState<string | null>(\n initialCompanyId ?? readPersisted(),\n );\n\n const persist = useMemo(() => {\n if (persistActiveCompany === false) {\n return () => {};\n }\n return persistActiveCompany ?? defaultPersist;\n }, [persistActiveCompany]);\n\n const setActiveCompany = useCallback(\n (id: string | null) => {\n setActiveCompanyState(id);\n persist(id);\n },\n [persist],\n );\n\n const refresh = useCallback(async () => {\n setLoading(true);\n setError(null);\n try {\n const next = await fetcher.fetchProfile();\n setProfile(next);\n // If the persisted company isn't a membership, fall back to\n // the first one (or null for users with no memberships).\n const stillMember =\n activeCompanyId != null &&\n next.memberships.some((m) => m.company_id === activeCompanyId);\n if (!stillMember) {\n const fallback =\n next.memberships[0]?.company_id ?? null;\n setActiveCompanyState(fallback);\n persist(fallback);\n }\n } catch (e) {\n setError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setLoading(false);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [fetcher]);\n\n useEffect(() => {\n void refresh();\n }, [refresh]);\n\n const resolver = useMemo<AuthRbacClient>(() => {\n if (profile == null) {\n // Empty resolver until the profile lands. Always returns\n // false → guards fall through to the unauthenticated branch.\n return {\n can: () => false,\n activePermissions: () => ({}) as PermissionMap,\n systemPermissions: () => ({}) as PermissionMap,\n };\n }\n return buildPermissionResolver(resources, profile, activeCompanyId);\n }, [profile, resources, activeCompanyId]);\n\n const value = useMemo<AuthRbacContextValue>(\n () => ({\n profile,\n loading,\n error,\n resources,\n activeCompanyId,\n setActiveCompany,\n refresh,\n resolver,\n }),\n [\n profile,\n loading,\n error,\n resources,\n activeCompanyId,\n setActiveCompany,\n refresh,\n resolver,\n ],\n );\n\n return (\n <AuthRbacContext.Provider value={value}>\n {props.children}\n </AuthRbacContext.Provider>\n );\n}\n\nexport function useAuthRbac(): AuthRbacContextValue {\n const ctx = useContext(AuthRbacContext);\n if (!ctx) {\n throw new Error(\n \"useAuthRbac must be used within an <AuthRbacProvider> — wrap your app at the root.\",\n );\n }\n return ctx;\n}\n","/**\n * Transport-agnostic client: turns an adopter-supplied\n * `AuthRbacFetcher` into a permission resolver. The React provider\n * wraps this; non-React consumers (Node scripts, edge functions)\n * can use it directly.\n */\n\nimport type {\n Action,\n AuthRbacFetcher,\n PermissionMap,\n ResourceDescriptor,\n ResourceRegistry,\n ResourceScope,\n UserProfile,\n} from \"./types.js\";\n\nexport interface AuthRbacClientOptions {\n fetcher: AuthRbacFetcher;\n /**\n * The host project's full resource list. Required so the resolver\n * can look up a resource's scope without a DB round-trip per call.\n * Re-using the same array the host syncs into the\n * `auth_rbac_resources` table at boot keeps everything in lockstep.\n */\n resources: ResourceRegistry;\n}\n\nexport interface CanOptions {\n /**\n * Override the active company. Omit to use the company the\n * caller has currently activated (the React provider tracks\n * this; for direct client use you must pass it).\n */\n companyId?: string | null;\n}\n\n/**\n * Pure resolver. Given a hydrated profile it answers boolean\n * questions instantly — no I/O. The `resourceMap` is built once at\n * construction so per-call work is two map lookups.\n */\nexport function buildPermissionResolver(\n resources: ResourceRegistry,\n profile: UserProfile,\n defaultCompanyId: string | null,\n) {\n const scopeByResource = new Map<string, ResourceScope>(\n resources.map((r) => [r.resource, r.scope]),\n );\n\n const can = (\n resource: string,\n action: Action,\n options?: CanOptions,\n ): boolean => {\n if (profile.is_super_admin) {\n return true;\n }\n const scope = scopeByResource.get(resource);\n if (!scope) {\n // Unknown resource — fail closed.\n return false;\n }\n if (scope === \"system\") {\n return readGrid(profile.system_permissions, resource, action);\n }\n const companyId = options?.companyId ?? defaultCompanyId;\n if (!companyId) {\n return false;\n }\n const membership = profile.memberships.find(\n (m) => m.company_id === companyId,\n );\n if (!membership) {\n return false;\n }\n return readGrid(membership.permissions, resource, action);\n };\n\n return {\n can,\n /** Permission map for the active (or specified) company. */\n activePermissions: (companyId?: string | null): PermissionMap => {\n const id = companyId ?? defaultCompanyId;\n if (!id) {\n return {};\n }\n return (\n profile.memberships.find((m) => m.company_id === id)?.permissions ?? {}\n );\n },\n systemPermissions: (): PermissionMap => profile.system_permissions,\n };\n}\n\nfunction readGrid(\n map: PermissionMap,\n resource: string,\n action: Action,\n): boolean {\n const grid = map[resource];\n if (!grid) {\n return false;\n }\n return grid[action];\n}\n\n/**\n * Helper: groups a resource registry by `group` for the matrix UI.\n * Returns groups in insertion order with their resources.\n */\nexport function groupResources(\n registry: ResourceRegistry,\n): Array<{ group: string; resources: ResourceDescriptor[] }> {\n const order: string[] = [];\n const buckets = new Map<string, ResourceDescriptor[]>();\n for (const r of registry) {\n const key = r.group ?? \"Sonstige\";\n if (!buckets.has(key)) {\n buckets.set(key, []);\n order.push(key);\n }\n buckets.get(key)!.push(r);\n }\n return order.map((g) => ({ group: g, resources: buckets.get(g)! }));\n}\n\nexport type AuthRbacClient = ReturnType<typeof buildPermissionResolver>;\nexport type { AuthRbacClientOptions as ClientOptions };\n","import type { Action } from \"../types.js\";\nimport type { CanOptions } from \"../client.js\";\n\nimport { useAuthRbac } from \"./AuthRbacProvider.js\";\n\n/**\n * Boolean permission check.\n *\n * @example\n * const canEdit = useCan(\"properties\", \"update\");\n * <Button disabled={!canEdit}>Speichern</Button>\n *\n * @example explicitly target a non-active company\n * const canRead = useCan(\"payments\", \"read\", { companyId: targetId });\n */\nexport function useCan(\n resource: string,\n action: Action,\n options?: CanOptions,\n): boolean {\n const { resolver } = useAuthRbac();\n return resolver.can(resource, action, options);\n}\n","import type { ReactNode } from \"react\";\n\nimport type { Action } from \"../types.js\";\nimport type { CanOptions } from \"../client.js\";\n\nimport { useCan } from \"./useCan.js\";\n\nexport interface CanProps extends CanOptions {\n resource: string;\n action: Action;\n /** Rendered when the user has the permission. */\n children: ReactNode;\n /**\n * Rendered when the user does NOT have the permission. Defaults\n * to `null` (silent hide). Pass a `<NoPermissionView />` or a\n * tooltip-wrapper to surface the denial explicitly.\n */\n fallback?: ReactNode;\n}\n\n/**\n * Subtree gate. Bails before children render so any data fetching\n * inside `children` is skipped for users without permission.\n */\nexport function Can(props: CanProps) {\n const { resource, action, companyId, children, fallback = null } = props;\n const allowed = useCan(resource, action, { companyId });\n return <>{allowed ? children : fallback}</>;\n}\n","import type { ReactNode } from \"react\";\n\nimport type { Action } from \"../types.js\";\nimport type { CanOptions } from \"../client.js\";\n\nimport { useAuthRbac } from \"./AuthRbacProvider.js\";\nimport { useCan } from \"./useCan.js\";\n\nexport interface RequirePermissionProps extends CanOptions {\n resource: string;\n action: Action;\n /**\n * What to render while the profile is still loading. Defaults to\n * `null` (no flash) — pass a spinner if your routes typically\n * mount before the profile lands.\n */\n loadingFallback?: ReactNode;\n /**\n * What to render when access is denied. Defaults to a minimal\n * \"Sie haben keinen Zugriff\" message; pass your own component to\n * theme it.\n */\n deniedFallback?: ReactNode;\n /**\n * For `react-router-dom v6` route-element usage, pass an `<Outlet />`\n * here — the gate resolves to either the outlet or the denied\n * fallback. For component-tree usage, pass any children.\n */\n children?: ReactNode;\n}\n\n/**\n * Route- or component-level guard. Three render branches:\n *\n * - profile not yet loaded → `loadingFallback`\n * - permission denied → `deniedFallback`\n * - permission granted → `children`\n *\n * Drop-in replacement for the legacy `<RequireRolesRoute>` pattern.\n *\n * @example\n * // App.tsx route table\n * <Route element={\n * <RequirePermission resource=\"payments\" action=\"read\">\n * <Outlet />\n * </RequirePermission>\n * }>\n * <Route path=\"/payments\" element={<PaymentsPage />} />\n * </Route>\n */\nexport function RequirePermission(props: RequirePermissionProps) {\n const {\n resource,\n action,\n companyId,\n loadingFallback = null,\n deniedFallback = (\n <div role=\"alert\" style={{ padding: 24 }}>\n <strong>Sie haben keinen Zugriff.</strong>\n </div>\n ),\n children = null,\n } = props;\n\n const { profile, loading } = useAuthRbac();\n const allowed = useCan(resource, action, { companyId });\n\n if (loading || profile == null) {\n return <>{loadingFallback}</>;\n }\n if (!allowed) {\n return <>{deniedFallback}</>;\n }\n return <>{children}</>;\n}\n","import { useMemo } from \"react\";\n\nimport type { CompanyMembership } from \"../types.js\";\n\nimport { useAuthRbac } from \"./AuthRbacProvider.js\";\n\nexport interface ActiveCompany {\n id: string | null;\n membership: CompanyMembership | null;\n memberships: CompanyMembership[];\n setActive: (id: string | null) => void;\n}\n\n/**\n * Read + switch the active company.\n *\n * @example\n * const { id, memberships, setActive } = useActiveCompany();\n *\n * return (\n * <select value={id ?? \"\"} onChange={(e) => setActive(e.target.value || null)}>\n * {memberships.map((m) => (\n * <option key={m.company_id} value={m.company_id}>{m.company_name}</option>\n * ))}\n * </select>\n * );\n */\nexport function useActiveCompany(): ActiveCompany {\n const { profile, activeCompanyId, setActiveCompany } = useAuthRbac();\n\n return useMemo(() => {\n const memberships = profile?.memberships ?? [];\n const membership =\n memberships.find((m) => m.company_id === activeCompanyId) ?? null;\n return {\n id: activeCompanyId,\n membership,\n memberships,\n setActive: setActiveCompany,\n };\n }, [profile, activeCompanyId, setActiveCompany]);\n}\n","import { useMemo } from \"react\";\n\nimport type { FrontendConfig } from \"../types.js\";\n\nimport { useAuthRbac } from \"./AuthRbacProvider.js\";\n\n/**\n * Reads the merged `frontend_config` for the user. Sources in\n * priority order: active company's membership > system roles. Use\n * this to drive sidebar items, dashboard defaults, and any other\n * \"what should this role see\" UX without hardcoded role checks.\n *\n * The shape is intentionally `Record<string, unknown>` — your host\n * project owns the schema. Document your keys (e.g. `sidebar`,\n * `default_dashboard`) once and stick to them.\n */\nexport function useFrontendConfig(): FrontendConfig {\n const { profile, activeCompanyId } = useAuthRbac();\n return useMemo(() => {\n if (!profile) {\n return {};\n }\n const membershipConfig =\n profile.memberships.find((m) => m.company_id === activeCompanyId)\n ?.frontend_config ?? {};\n return { ...profile.system_frontend_config, ...membershipConfig };\n }, [profile, activeCompanyId]);\n}\n","/**\n * Built-in fetchers — adopters can use these or pass their own\n * implementation of `AuthRbacFetcher`.\n */\n\nimport type { AuthRbacFetcher, UserProfile } from \"./types.js\";\n\n/**\n * Calls the package's SQL function `auth_rbac_user_profile(uuid)` via\n * a Supabase JS client. Easiest path when the host project already\n * uses Supabase.\n *\n * @example\n * createSupabaseFetcher({ supabase, userId: session.user.id })\n */\nexport function createSupabaseFetcher(opts: {\n supabase: {\n rpc: (\n fn: string,\n args: Record<string, unknown>,\n ) => Promise<{ data: unknown; error: { message: string } | null }>;\n };\n userId: string;\n}): AuthRbacFetcher {\n return {\n async fetchProfile(): Promise<UserProfile> {\n const { data, error } = await opts.supabase.rpc(\n \"auth_rbac_user_profile\",\n { p_user_id: opts.userId },\n );\n if (error) {\n throw new Error(\n `auth-rbac: failed to load user profile via Supabase RPC: ${error.message}`,\n );\n }\n return normalizeProfile(data);\n },\n };\n}\n\n/**\n * Calls a regular HTTP endpoint that returns a `UserProfile` JSON\n * payload. Use this when the host project has its own backend that\n * wraps the package's Python helpers (or any equivalent).\n *\n * @example\n * createHttpFetcher({ url: \"/api/users/me/profile\" })\n */\nexport function createHttpFetcher(opts: {\n url: string;\n /** Forwarded as-is to `fetch`. Use this to attach auth headers. */\n init?: RequestInit;\n /** Override the global `fetch` if you're in a non-browser env. */\n fetch?: typeof fetch;\n}): AuthRbacFetcher {\n const fetchImpl = opts.fetch ?? globalThis.fetch;\n return {\n async fetchProfile(): Promise<UserProfile> {\n const res = await fetchImpl(opts.url, opts.init);\n if (!res.ok) {\n throw new Error(\n `auth-rbac: profile endpoint ${opts.url} returned ${res.status}`,\n );\n }\n const json = (await res.json()) as unknown;\n return normalizeProfile(json);\n },\n };\n}\n\n/**\n * Defensive normalisation: the Supabase RPC returns whatever the SQL\n * function emitted. We coerce missing fields into the empty defaults\n * so consumers can iterate without null checks. Throws if the shape\n * is unrecognisable.\n */\nfunction normalizeProfile(raw: unknown): UserProfile {\n if (!raw || typeof raw !== \"object\") {\n throw new Error(\"auth-rbac: profile payload is not an object\");\n }\n const p = raw as Partial<UserProfile> & Record<string, unknown>;\n if (typeof p.user_id !== \"string\") {\n throw new Error(\"auth-rbac: profile payload missing user_id\");\n }\n return {\n user_id: p.user_id,\n is_super_admin: !!p.is_super_admin,\n system_roles: Array.isArray(p.system_roles) ? p.system_roles : [],\n system_permissions:\n p.system_permissions && typeof p.system_permissions === \"object\"\n ? (p.system_permissions as UserProfile[\"system_permissions\"])\n : {},\n system_frontend_config:\n p.system_frontend_config && typeof p.system_frontend_config === \"object\"\n ? (p.system_frontend_config as UserProfile[\"system_frontend_config\"])\n : {},\n memberships: Array.isArray(p.memberships)\n ? (p.memberships as UserProfile[\"memberships\"])\n : [],\n };\n}\n","/**\n * Typed factory — turns a const-asserted resource registry into a\n * set of hooks/components whose `resource` arg is constrained to\n * the registered names. Typos become TypeScript errors instead of\n * silent runtime `false`.\n *\n * @example\n * // src/auth/resources.ts\n * import { defineAuthRbac } from \"snipe-auth-rbac/react\";\n *\n * export const RESOURCES = [\n * { resource: \"properties\", scope: \"company\", label: \"Liegenschaften\", group: \"Stammdaten\" },\n * { resource: \"payments\", scope: \"company\", label: \"Zahlungen\", group: \"Finanzen\" },\n * { resource: \"system_audit\", scope: \"system\", label: \"Audit-Log\", group: \"Plattform\" },\n * ] as const;\n *\n * export const { useCan, Can, RequirePermission } = defineAuthRbac(RESOURCES);\n *\n * // ----- elsewhere -----\n * useCan(\"properties\", \"update\"); // ✓\n * useCan(\"paymetns\", \"update\"); // ✗ TS error: not assignable to type \"properties\" | \"payments\" | \"system_audit\"\n */\n\nimport type { ComponentType, ReactNode } from \"react\";\n\nimport type {\n Action,\n ResourceDescriptor,\n ResourceRegistry,\n} from \"./types.js\";\n\n/**\n * Drop-in replacement signatures for the three guards, with a\n * narrowed `resource` arg.\n */\nexport interface TypedGuards<R extends string> {\n useCan: (\n resource: R,\n action: Action,\n options?: { companyId?: string | null },\n ) => boolean;\n Can: ComponentType<{\n resource: R;\n action: Action;\n companyId?: string | null;\n children: ReactNode;\n fallback?: ReactNode;\n }>;\n RequirePermission: ComponentType<{\n resource: R;\n action: Action;\n companyId?: string | null;\n loadingFallback?: ReactNode;\n deniedFallback?: ReactNode;\n children?: ReactNode;\n }>;\n /** The const-asserted registry, re-exported so call-sites can iterate. */\n resources: ResourceRegistry;\n /** All registered resource names as a union — handy for typing\n * application-side data structures. */\n resourceNames: ReadonlyArray<R>;\n}\n\n/**\n * Factory. Run once at module-init time in your host project; the\n * returned hooks/components are referentially stable.\n */\nexport function defineAuthRbac<\n const Reg extends ReadonlyArray<ResourceDescriptor>,\n>(\n resources: Reg,\n // The runtime guards live in the React entry; we accept them\n // here as plain refs so this module stays React-free at the type\n // level. The `react` entry calls this factory passing its own\n // exports so adopters never see the wiring.\n runtime: {\n useCan: TypedGuards<string>[\"useCan\"];\n Can: TypedGuards<string>[\"Can\"];\n RequirePermission: TypedGuards<string>[\"RequirePermission\"];\n },\n): TypedGuards<Reg[number][\"resource\"]> {\n type R = Reg[number][\"resource\"];\n return {\n useCan: runtime.useCan as TypedGuards<R>[\"useCan\"],\n Can: runtime.Can as TypedGuards<R>[\"Can\"],\n RequirePermission: runtime.RequirePermission as TypedGuards<R>[\"RequirePermission\"],\n resources,\n resourceNames: resources.map((r) => r.resource) as ReadonlyArray<R>,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAQO;;;ACkCA,SAAS,wBACd,WACA,SACA,kBACA;AACA,QAAM,kBAAkB,IAAI;AAAA,IAC1B,UAAU,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC;AAAA,EAC5C;AAEA,QAAM,MAAM,CACV,UACA,QACA,YACY;AACZ,QAAI,QAAQ,gBAAgB;AAC1B,aAAO;AAAA,IACT;AACA,UAAM,QAAQ,gBAAgB,IAAI,QAAQ;AAC1C,QAAI,CAAC,OAAO;AAEV,aAAO;AAAA,IACT;AACA,QAAI,UAAU,UAAU;AACtB,aAAO,SAAS,QAAQ,oBAAoB,UAAU,MAAM;AAAA,IAC9D;AACA,UAAM,YAAY,SAAS,aAAa;AACxC,QAAI,CAAC,WAAW;AACd,aAAO;AAAA,IACT;AACA,UAAM,aAAa,QAAQ,YAAY;AAAA,MACrC,CAAC,MAAM,EAAE,eAAe;AAAA,IAC1B;AACA,QAAI,CAAC,YAAY;AACf,aAAO;AAAA,IACT;AACA,WAAO,SAAS,WAAW,aAAa,UAAU,MAAM;AAAA,EAC1D;AAEA,SAAO;AAAA,IACL;AAAA;AAAA,IAEA,mBAAmB,CAAC,cAA6C;AAC/D,YAAM,KAAK,aAAa;AACxB,UAAI,CAAC,IAAI;AACP,eAAO,CAAC;AAAA,MACV;AACA,aACE,QAAQ,YAAY,KAAK,CAAC,MAAM,EAAE,eAAe,EAAE,GAAG,eAAe,CAAC;AAAA,I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I,CAAC,OAAO,OAAO,QAAQ,UAAU;AACnC,UAAM,IAAI,MAAM,6CAA6C;AAAA,EAC/D;AACA,QAAM,IAAI;AACV,MAAI,OAAO,EAAE,YAAY,UAAU;AACjC,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AAAA,IACL,SAAS,EAAE;AAAA,IACX,gBAAgB,CAAC,CAAC,EAAE;AAAA,IACpB,cAAc,MAAM,QAAQ,EAAE,YAAY,IAAI,EAAE,eAAe,CAAC;AAAA,IAChE,oBACE,EAAE,sBAAsB,OAAO,EAAE,uBAAuB,WACnD,EAAE,qBACH,CAAC;AAAA,IACP,wBACE,EAAE,0BAA0B,OAAO,EAAE,2BAA2B,WAC3D,EAAE,yBACH,CAAC;AAAA,IACP,aAAa,MAAM,QAAQ,EAAE,WAAW,IACnC,EAAE,cACH,CAAC;AAAA,EACP;AACF;;;ACjCO,SAAS,eAGd,WAKA,SAKsC;AAEtC,SAAO;AAAA,IACL,QAAQ,QAAQ;AAAA,IAChB,KAAK,QAAQ;AAAA,IACb,mBAAmB,QAAQ;AAAA,IAC3B;AAAA,IACA,eAAe,UAAU,IAAI,CAAC,MAAM,EAAE,QAAQ;AAAA,EAChD;AACF;;;AT9BO,SAASC,gBAEd,WAAsD;AACtD,SAAO,eAAgB,WAAW;AAAA,IAChC;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;","names":["defineAuthRbac","import_jsx_runtime","import_jsx_runtime","import_react","import_react","defineAuthRbac"]}
@@ -0,0 +1,221 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode, ComponentType } from 'react';
3
+ import { AuthRbacClient, CanOptions } from '../index.cjs';
4
+ export { createHttpFetcher, createSupabaseFetcher } from '../index.cjs';
5
+ import { b as AuthRbacFetcher, c as ResourceRegistry, U as UserProfile, A as Action, C as CompanyMembership, F as FrontendConfig, a as ResourceDescriptor } from '../types-BEc5SCIo.cjs';
6
+ export { P as PermissionGrid, d as PermissionMap, R as ResourceScope, e as RoleSummary } from '../types-BEc5SCIo.cjs';
7
+
8
+ interface AuthRbacContextValue {
9
+ /**
10
+ * `null` means we haven't hydrated yet. Components should treat
11
+ * the not-loaded state as "no permissions" (fail-closed).
12
+ */
13
+ profile: UserProfile | null;
14
+ loading: boolean;
15
+ error: Error | null;
16
+ resources: ResourceRegistry;
17
+ activeCompanyId: string | null;
18
+ setActiveCompany: (id: string | null) => void;
19
+ refresh: () => Promise<void>;
20
+ resolver: AuthRbacClient;
21
+ }
22
+ interface AuthRbacProviderProps {
23
+ fetcher: AuthRbacFetcher;
24
+ resources: ResourceRegistry;
25
+ /**
26
+ * Initial active company. Common patterns:
27
+ * - read from URL query/path
28
+ * - read from localStorage
29
+ * - omit and let the user pick from the switcher
30
+ */
31
+ initialCompanyId?: string | null;
32
+ /**
33
+ * Persistence hook. Called every time the active company changes.
34
+ * Default: writes to `localStorage` under
35
+ * `auth-rbac:active-company`. Pass `false` to disable.
36
+ */
37
+ persistActiveCompany?: ((id: string | null) => void) | false;
38
+ children: ReactNode;
39
+ }
40
+ declare function AuthRbacProvider(props: AuthRbacProviderProps): react_jsx_runtime.JSX.Element;
41
+ declare function useAuthRbac(): AuthRbacContextValue;
42
+
43
+ /**
44
+ * Boolean permission check.
45
+ *
46
+ * @example
47
+ * const canEdit = useCan("properties", "update");
48
+ * <Button disabled={!canEdit}>Speichern</Button>
49
+ *
50
+ * @example explicitly target a non-active company
51
+ * const canRead = useCan("payments", "read", { companyId: targetId });
52
+ */
53
+ declare function useCan(resource: string, action: Action, options?: CanOptions): boolean;
54
+
55
+ interface CanProps extends CanOptions {
56
+ resource: string;
57
+ action: Action;
58
+ /** Rendered when the user has the permission. */
59
+ children: ReactNode;
60
+ /**
61
+ * Rendered when the user does NOT have the permission. Defaults
62
+ * to `null` (silent hide). Pass a `<NoPermissionView />` or a
63
+ * tooltip-wrapper to surface the denial explicitly.
64
+ */
65
+ fallback?: ReactNode;
66
+ }
67
+ /**
68
+ * Subtree gate. Bails before children render so any data fetching
69
+ * inside `children` is skipped for users without permission.
70
+ */
71
+ declare function Can(props: CanProps): react_jsx_runtime.JSX.Element;
72
+
73
+ interface RequirePermissionProps extends CanOptions {
74
+ resource: string;
75
+ action: Action;
76
+ /**
77
+ * What to render while the profile is still loading. Defaults to
78
+ * `null` (no flash) — pass a spinner if your routes typically
79
+ * mount before the profile lands.
80
+ */
81
+ loadingFallback?: ReactNode;
82
+ /**
83
+ * What to render when access is denied. Defaults to a minimal
84
+ * "Sie haben keinen Zugriff" message; pass your own component to
85
+ * theme it.
86
+ */
87
+ deniedFallback?: ReactNode;
88
+ /**
89
+ * For `react-router-dom v6` route-element usage, pass an `<Outlet />`
90
+ * here — the gate resolves to either the outlet or the denied
91
+ * fallback. For component-tree usage, pass any children.
92
+ */
93
+ children?: ReactNode;
94
+ }
95
+ /**
96
+ * Route- or component-level guard. Three render branches:
97
+ *
98
+ * - profile not yet loaded → `loadingFallback`
99
+ * - permission denied → `deniedFallback`
100
+ * - permission granted → `children`
101
+ *
102
+ * Drop-in replacement for the legacy `<RequireRolesRoute>` pattern.
103
+ *
104
+ * @example
105
+ * // App.tsx route table
106
+ * <Route element={
107
+ * <RequirePermission resource="payments" action="read">
108
+ * <Outlet />
109
+ * </RequirePermission>
110
+ * }>
111
+ * <Route path="/payments" element={<PaymentsPage />} />
112
+ * </Route>
113
+ */
114
+ declare function RequirePermission(props: RequirePermissionProps): react_jsx_runtime.JSX.Element;
115
+
116
+ interface ActiveCompany {
117
+ id: string | null;
118
+ membership: CompanyMembership | null;
119
+ memberships: CompanyMembership[];
120
+ setActive: (id: string | null) => void;
121
+ }
122
+ /**
123
+ * Read + switch the active company.
124
+ *
125
+ * @example
126
+ * const { id, memberships, setActive } = useActiveCompany();
127
+ *
128
+ * return (
129
+ * <select value={id ?? ""} onChange={(e) => setActive(e.target.value || null)}>
130
+ * {memberships.map((m) => (
131
+ * <option key={m.company_id} value={m.company_id}>{m.company_name}</option>
132
+ * ))}
133
+ * </select>
134
+ * );
135
+ */
136
+ declare function useActiveCompany(): ActiveCompany;
137
+
138
+ /**
139
+ * Reads the merged `frontend_config` for the user. Sources in
140
+ * priority order: active company's membership > system roles. Use
141
+ * this to drive sidebar items, dashboard defaults, and any other
142
+ * "what should this role see" UX without hardcoded role checks.
143
+ *
144
+ * The shape is intentionally `Record<string, unknown>` — your host
145
+ * project owns the schema. Document your keys (e.g. `sidebar`,
146
+ * `default_dashboard`) once and stick to them.
147
+ */
148
+ declare function useFrontendConfig(): FrontendConfig;
149
+
150
+ /**
151
+ * Typed factory — turns a const-asserted resource registry into a
152
+ * set of hooks/components whose `resource` arg is constrained to
153
+ * the registered names. Typos become TypeScript errors instead of
154
+ * silent runtime `false`.
155
+ *
156
+ * @example
157
+ * // src/auth/resources.ts
158
+ * import { defineAuthRbac } from "snipe-auth-rbac/react";
159
+ *
160
+ * export const RESOURCES = [
161
+ * { resource: "properties", scope: "company", label: "Liegenschaften", group: "Stammdaten" },
162
+ * { resource: "payments", scope: "company", label: "Zahlungen", group: "Finanzen" },
163
+ * { resource: "system_audit", scope: "system", label: "Audit-Log", group: "Plattform" },
164
+ * ] as const;
165
+ *
166
+ * export const { useCan, Can, RequirePermission } = defineAuthRbac(RESOURCES);
167
+ *
168
+ * // ----- elsewhere -----
169
+ * useCan("properties", "update"); // ✓
170
+ * useCan("paymetns", "update"); // ✗ TS error: not assignable to type "properties" | "payments" | "system_audit"
171
+ */
172
+
173
+ /**
174
+ * Drop-in replacement signatures for the three guards, with a
175
+ * narrowed `resource` arg.
176
+ */
177
+ interface TypedGuards<R extends string> {
178
+ useCan: (resource: R, action: Action, options?: {
179
+ companyId?: string | null;
180
+ }) => boolean;
181
+ Can: ComponentType<{
182
+ resource: R;
183
+ action: Action;
184
+ companyId?: string | null;
185
+ children: ReactNode;
186
+ fallback?: ReactNode;
187
+ }>;
188
+ RequirePermission: ComponentType<{
189
+ resource: R;
190
+ action: Action;
191
+ companyId?: string | null;
192
+ loadingFallback?: ReactNode;
193
+ deniedFallback?: ReactNode;
194
+ children?: ReactNode;
195
+ }>;
196
+ /** The const-asserted registry, re-exported so call-sites can iterate. */
197
+ resources: ResourceRegistry;
198
+ /** All registered resource names as a union — handy for typing
199
+ * application-side data structures. */
200
+ resourceNames: ReadonlyArray<R>;
201
+ }
202
+
203
+ /**
204
+ * React + Next.js entry. Import this in browser code:
205
+ *
206
+ * import { AuthRbacProvider, useCan } from "snipe-auth-rbac/react";
207
+ *
208
+ * The non-React entry (`snipe-auth-rbac`) re-exports types and the
209
+ * pure resolver, suitable for Node, edge workers, and tests.
210
+ */
211
+
212
+ /**
213
+ * Typed factory — pass a const-asserted resource registry and get
214
+ * back guards whose `resource` arg is constrained to the registered
215
+ * names. Recommended at the top of every host project.
216
+ *
217
+ * See ../define.ts for the full doc + example.
218
+ */
219
+ declare function defineAuthRbac<const Reg extends ReadonlyArray<ResourceDescriptor>>(resources: Reg): TypedGuards<Reg[number]["resource"]>;
220
+
221
+ export { Action, type ActiveCompany, AuthRbacFetcher, AuthRbacProvider, type AuthRbacProviderProps, Can, type CanProps, CompanyMembership, FrontendConfig, RequirePermission, type RequirePermissionProps, ResourceDescriptor, ResourceRegistry, type TypedGuards, UserProfile, defineAuthRbac, useActiveCompany, useAuthRbac, useCan, useFrontendConfig };