@ackplus/nest-auth-react 1.1.31-beta.14 → 1.1.31-beta.15

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,15 @@
1
+ import { ComponentType } from 'react';
2
+ export interface WithRequirePermissionOptions {
3
+ permission: string | string[];
4
+ matchAll?: boolean;
5
+ LoadingComponent?: ComponentType;
6
+ FallbackComponent?: ComponentType;
7
+ onAccessDenied?: () => void;
8
+ }
9
+ export interface WithRequirePermissionInjectedProps {
10
+ hasPermission: boolean;
11
+ isLoading: boolean;
12
+ isAuthenticated: boolean;
13
+ }
14
+ export declare function withRequirePermission<P extends object>(WrappedComponent: ComponentType<P>, options: WithRequirePermissionOptions): ComponentType<Omit<P, keyof WithRequirePermissionInjectedProps>>;
15
+ export declare function createRequirePermissionHOC(defaultOptions: Partial<WithRequirePermissionOptions>): <P extends object>(WrappedComponent: ComponentType<P>, overrideOptions?: Partial<WithRequirePermissionOptions>) => ComponentType<Omit<P, keyof WithRequirePermissionInjectedProps>>;
@@ -0,0 +1,56 @@
1
+ import React, { useEffect } from 'react';
2
+ import { useAuthStatus } from '../hooks/use-auth-status';
3
+ import { useHasPermission } from '../hooks/use-has-role';
4
+ export function withRequirePermission(WrappedComponent, options) {
5
+ const { permission, matchAll = false, LoadingComponent, FallbackComponent, onAccessDenied, } = options;
6
+ const displayName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
7
+ function WithRequirePermissionWrapper(props) {
8
+ const { isLoading, isAuthenticated } = useAuthStatus();
9
+ const hasRequiredPermission = useHasPermission(permission, matchAll);
10
+ const accessDenied = !isAuthenticated || !hasRequiredPermission;
11
+ useEffect(() => {
12
+ if (!isLoading && accessDenied && onAccessDenied) {
13
+ onAccessDenied();
14
+ }
15
+ }, [isLoading, accessDenied]);
16
+ if (isLoading) {
17
+ if (LoadingComponent) {
18
+ return React.createElement(LoadingComponent);
19
+ }
20
+ return null;
21
+ }
22
+ if (accessDenied) {
23
+ if (onAccessDenied) {
24
+ if (LoadingComponent) {
25
+ return React.createElement(LoadingComponent);
26
+ }
27
+ return null;
28
+ }
29
+ if (FallbackComponent) {
30
+ return React.createElement(FallbackComponent);
31
+ }
32
+ return null;
33
+ }
34
+ const injectedProps = {
35
+ hasPermission: hasRequiredPermission,
36
+ isLoading,
37
+ isAuthenticated,
38
+ };
39
+ return React.createElement(WrappedComponent, { ...props, ...injectedProps });
40
+ }
41
+ WithRequirePermissionWrapper.displayName = `WithRequirePermission(${displayName})`;
42
+ return WithRequirePermissionWrapper;
43
+ }
44
+ export function createRequirePermissionHOC(defaultOptions) {
45
+ return function (WrappedComponent, overrideOptions) {
46
+ const mergedOptions = {
47
+ ...defaultOptions,
48
+ ...overrideOptions,
49
+ };
50
+ if (!mergedOptions.permission) {
51
+ throw new Error('withRequirePermission: permission option is required');
52
+ }
53
+ return withRequirePermission(WrappedComponent, mergedOptions);
54
+ };
55
+ }
56
+ //# sourceMappingURL=with-require-permission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-require-permission.js","sourceRoot":"","sources":["../../src/guards/with-require-permission.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAAiB,SAAS,EAAE,MAAM,OAAO,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAwEzD,MAAM,UAAU,qBAAqB,CACjC,gBAAkC,EAClC,OAAqC;IAErC,MAAM,EACF,UAAU,EACV,QAAQ,GAAG,KAAK,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,GACjB,GAAG,OAAO,CAAC;IAEZ,MAAM,WAAW,GAAG,gBAAgB,CAAC,WAAW,IAAI,gBAAgB,CAAC,IAAI,IAAI,WAAW,CAAC;IAEzF,SAAS,4BAA4B,CAAC,KAAwD;QAC1F,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,GAAG,aAAa,EAAE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a,GAAG;YAClB,GAAG,cAAc;YACjB,GAAG,eAAe;SACW,CAAC;QAElC,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC5E,CAAC;QAED,OAAO,qBAAqB,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAC;IAClE,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { ComponentType } from 'react';
2
+ export interface WithRequireRoleOptions {
3
+ role: string | string[];
4
+ matchAll?: boolean;
5
+ LoadingComponent?: ComponentType;
6
+ FallbackComponent?: ComponentType;
7
+ onAccessDenied?: () => void;
8
+ }
9
+ export interface WithRequireRoleInjectedProps {
10
+ hasRole: boolean;
11
+ isLoading: boolean;
12
+ isAuthenticated: boolean;
13
+ }
14
+ export declare function withRequireRole<P extends object>(WrappedComponent: ComponentType<P>, options: WithRequireRoleOptions): ComponentType<Omit<P, keyof WithRequireRoleInjectedProps>>;
15
+ export declare function createRequireRoleHOC(defaultOptions: Partial<WithRequireRoleOptions>): <P extends object>(WrappedComponent: ComponentType<P>, overrideOptions?: Partial<WithRequireRoleOptions>) => ComponentType<Omit<P, keyof WithRequireRoleInjectedProps>>;
@@ -0,0 +1,56 @@
1
+ import React, { useEffect } from 'react';
2
+ import { useAuthStatus } from '../hooks/use-auth-status';
3
+ import { useHasRole } from '../hooks/use-has-role';
4
+ export function withRequireRole(WrappedComponent, options) {
5
+ const { role, matchAll = false, LoadingComponent, FallbackComponent, onAccessDenied, } = options;
6
+ const displayName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
7
+ function WithRequireRoleWrapper(props) {
8
+ const { isLoading, isAuthenticated } = useAuthStatus();
9
+ const hasRequiredRole = useHasRole(role, matchAll);
10
+ const accessDenied = !isAuthenticated || !hasRequiredRole;
11
+ useEffect(() => {
12
+ if (!isLoading && accessDenied && onAccessDenied) {
13
+ onAccessDenied();
14
+ }
15
+ }, [isLoading, accessDenied]);
16
+ if (isLoading) {
17
+ if (LoadingComponent) {
18
+ return React.createElement(LoadingComponent);
19
+ }
20
+ return null;
21
+ }
22
+ if (accessDenied) {
23
+ if (onAccessDenied) {
24
+ if (LoadingComponent) {
25
+ return React.createElement(LoadingComponent);
26
+ }
27
+ return null;
28
+ }
29
+ if (FallbackComponent) {
30
+ return React.createElement(FallbackComponent);
31
+ }
32
+ return null;
33
+ }
34
+ const injectedProps = {
35
+ hasRole: hasRequiredRole,
36
+ isLoading,
37
+ isAuthenticated,
38
+ };
39
+ return React.createElement(WrappedComponent, { ...props, ...injectedProps });
40
+ }
41
+ WithRequireRoleWrapper.displayName = `WithRequireRole(${displayName})`;
42
+ return WithRequireRoleWrapper;
43
+ }
44
+ export function createRequireRoleHOC(defaultOptions) {
45
+ return function (WrappedComponent, overrideOptions) {
46
+ const mergedOptions = {
47
+ ...defaultOptions,
48
+ ...overrideOptions,
49
+ };
50
+ if (!mergedOptions.role) {
51
+ throw new Error('withRequireRole: role option is required');
52
+ }
53
+ return withRequireRole(WrappedComponent, mergedOptions);
54
+ };
55
+ }
56
+ //# sourceMappingURL=with-require-role.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-require-role.js","sourceRoot":"","sources":["../../src/guards/with-require-role.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAAiB,SAAS,EAAE,MAAM,OAAO,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAwEnD,MAAM,UAAU,eAAe,CAC3B,gBAAkC,EAClC,OAA+B;IAE/B,MAAM,EACF,IAAI,EACJ,QAAQ,GAAG,KAAK,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,GACjB,GAAG,OAAO,CAAC;IAEZ,MAAM,WAAW,GAAG,gBAAgB,CAAC,WAAW,IAAI,gBAAgB,CAAC,IAAI,IAAI,WAAW,CAAC;IAEzF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,oBAAoB,CAAC,cAA+C;IAChF,OAAO,UACH,gBAAkC,EAClC,eAAiD;QAEjD,MAAM,aAAa,GAAG;YAClB,GAAG,cAAc;YACjB,GAAG,eAAe;SACK,CAAC;QAE5B,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAChE,CAAC;QAED,OAAO,eAAe,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAC;IAC5D,CAAC,CAAC;AACN,CAAC"}
package/dist/index.d.ts CHANGED
@@ -10,6 +10,8 @@ export { AuthGuard, AuthGuardProps } from './guards/auth-guard';
10
10
  export { GuestGuard, GuestGuardProps } from './guards/guest-guard';
11
11
  export { RequireRole, RequireRoleProps } from './guards/require-role';
12
12
  export { RequirePermission, RequirePermissionProps } from './guards/require-permission';
13
+ export { withRequireRole, createRequireRoleHOC, WithRequireRoleOptions, WithRequireRoleInjectedProps, } from './guards/with-require-role';
14
+ export { withRequirePermission, createRequirePermissionHOC, WithRequirePermissionOptions, WithRequirePermissionInjectedProps, } from './guards/with-require-permission';
13
15
  export { createNextAuthHelpers, NextAuthHelpers, ServerAuthState } from './next/create-next-auth-helpers';
14
16
  export { NextAuthProvider, NextAuthProviderProps } from './next/next-auth-provider';
15
17
  export { CrossTabSync, createCrossTabSync, SyncEvent, SyncEventType, SyncHandler } from './sync/cross-tab-sync';
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@ export { AuthGuard } from './guards/auth-guard';
10
10
  export { GuestGuard } from './guards/guest-guard';
11
11
  export { RequireRole } from './guards/require-role';
12
12
  export { RequirePermission } from './guards/require-permission';
13
+ export { withRequireRole, createRequireRoleHOC, } from './guards/with-require-role';
14
+ export { withRequirePermission, createRequirePermissionHOC, } from './guards/with-require-permission';
13
15
  export { createNextAuthHelpers } from './next/create-next-auth-helpers';
14
16
  export { NextAuthProvider } from './next/next-auth-provider';
15
17
  export { CrossTabSync, createCrossTabSync } from './sync/cross-tab-sync';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAoB,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,YAAY,EAAuC,MAAM,yBAAyB,CAAC;AAI5F,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAoB,MAAM,yBAAyB,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAIpE,OAAO,EAAE,SAAS,EAAkB,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAmB,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,WAAW,EAAoB,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAA0B,MAAM,6BAA6B,CAAC;AAIxF,OAAO,EAAE,qBAAqB,EAAoC,MAAM,iCAAiC,CAAC;AAC1G,OAAO,EAAE,gBAAgB,EAAyB,MAAM,2BAA2B,CAAC;AAIpF,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAyC,MAAM,uBAAuB,CAAC;AA8BhH,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAGvD,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAoB,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,YAAY,EAAuC,MAAM,yBAAyB,CAAC;AAI5F,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAoB,MAAM,yBAAyB,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAIpE,OAAO,EAAE,SAAS,EAAkB,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAmB,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,WAAW,EAAoB,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAA0B,MAAM,6BAA6B,CAAC;AAGxF,OAAO,EACH,eAAe,EACf,oBAAoB,GAGvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACH,qBAAqB,EACrB,0BAA0B,GAG7B,MAAM,kCAAkC,CAAC;AAI1C,OAAO,EAAE,qBAAqB,EAAoC,MAAM,iCAAiC,CAAC;AAC1G,OAAO,EAAE,gBAAgB,EAAyB,MAAM,2BAA2B,CAAC;AAIpF,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAyC,MAAM,uBAAuB,CAAC;AA8BhH,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAGvD,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ackplus/nest-auth-react",
3
- "version": "1.1.31-beta.14",
3
+ "version": "1.1.31-beta.15",
4
4
  "description": "React SDK for NestJS authentication - hooks, guards, and Next.js integration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -38,7 +38,7 @@
38
38
  "react": "^18.0.0 || ^19.0.0"
39
39
  },
40
40
  "dependencies": {
41
- "@ackplus/nest-auth-client": "1.1.31-beta.14"
41
+ "@ackplus/nest-auth-client": "1.1.31-beta.15"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^5.7.3",