@frontegg/react-hooks 7.0.0-alpha.0 → 7.0.0-alpha.2

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.
Files changed (137) hide show
  1. package/FronteggProvider/FronteggProvider.d.ts +12 -0
  2. package/FronteggProvider/FronteggProvider.js +138 -0
  3. package/FronteggProvider/index.d.ts +1 -12
  4. package/FronteggProvider/index.js +1 -133
  5. package/FronteggStoreContext/FronteggStoreContext.d.ts +9 -0
  6. package/FronteggStoreContext/FronteggStoreContext.js +16 -0
  7. package/FronteggStoreContext/index.d.ts +1 -13
  8. package/FronteggStoreContext/index.js +1 -14
  9. package/audits/auditLogs.d.ts +2 -4
  10. package/audits/auditLogs.js +8 -6
  11. package/audits/auditsMetadata.d.ts +2 -4
  12. package/audits/auditsMetadata.js +8 -6
  13. package/audits/hooks.d.ts +0 -8
  14. package/audits/hooks.js +6 -30
  15. package/auth/acceptInvitation.js +10 -5
  16. package/auth/activateAccount.js +10 -4
  17. package/auth/{MSP/allAccounts.d.ts → allAccounts.d.ts} +2 -4
  18. package/auth/allAccounts.js +20 -0
  19. package/auth/apiTokens.d.ts +0 -2
  20. package/auth/apiTokens.js +8 -6
  21. package/auth/customLogin.d.ts +2 -5
  22. package/auth/customLogin.js +10 -11
  23. package/auth/entitlements.d.ts +32 -13
  24. package/auth/entitlements.js +116 -21
  25. package/auth/forgotPassword.d.ts +0 -2
  26. package/auth/forgotPassword.js +8 -6
  27. package/auth/groups.d.ts +0 -4
  28. package/auth/groups.js +12 -13
  29. package/auth/hooks.d.ts +44 -12
  30. package/auth/hooks.js +73 -62
  31. package/auth/impersonate.d.ts +0 -2
  32. package/auth/impersonate.js +7 -6
  33. package/auth/index.d.ts +15 -16
  34. package/auth/index.js +16 -17
  35. package/auth/login.d.ts +1 -3
  36. package/auth/login.js +7 -6
  37. package/auth/mfa.d.ts +1 -1
  38. package/auth/mfa.js +13 -4
  39. package/auth/passkeys.d.ts +1 -4
  40. package/auth/passkeys.js +7 -6
  41. package/auth/profile.d.ts +2 -4
  42. package/auth/profile.js +9 -8
  43. package/auth/provisioning.d.ts +1 -3
  44. package/auth/provisioning.js +7 -6
  45. package/auth/resetPhoneNumber.d.ts +1 -3
  46. package/auth/resetPhoneNumber.js +7 -6
  47. package/auth/roles.d.ts +1 -3
  48. package/auth/roles.js +7 -6
  49. package/auth/security/index.d.ts +4 -0
  50. package/auth/security/index.js +4 -0
  51. package/auth/security/restrictions.d.ts +0 -2
  52. package/auth/security/restrictions.js +10 -10
  53. package/auth/security/securityCenter.d.ts +1 -4
  54. package/auth/security/securityCenter.js +8 -6
  55. package/auth/security/securityPolicy.d.ts +5 -11
  56. package/auth/security/securityPolicy.js +19 -30
  57. package/auth/security/sessionsPolicy.d.ts +1 -3
  58. package/auth/security/sessionsPolicy.js +8 -6
  59. package/auth/sessions.d.ts +1 -3
  60. package/auth/sessions.js +7 -6
  61. package/auth/signup.d.ts +1 -1
  62. package/auth/signup.js +13 -4
  63. package/auth/sms.d.ts +3 -0
  64. package/auth/sms.js +8 -0
  65. package/auth/socialLogin.d.ts +1 -1
  66. package/auth/socialLogin.js +10 -4
  67. package/auth/sso.d.ts +1 -1
  68. package/auth/sso.js +10 -4
  69. package/auth/stepUp.d.ts +16 -0
  70. package/auth/stepUp.js +51 -0
  71. package/auth/team.d.ts +9 -1
  72. package/auth/team.js +22 -4
  73. package/auth/tenants.d.ts +1 -3
  74. package/auth/tenants.js +8 -6
  75. package/common/CustomComponentHolder.js +3 -1
  76. package/common/index.js +3 -3
  77. package/connectivity/hooks.d.ts +2 -5
  78. package/connectivity/hooks.js +6 -9
  79. package/index.d.ts +2 -2
  80. package/index.js +3 -3
  81. package/node/FronteggProvider/FronteggProvider.js +146 -0
  82. package/node/FronteggProvider/index.js +9 -134
  83. package/node/FronteggStoreContext/FronteggStoreContext.js +28 -0
  84. package/node/FronteggStoreContext/index.js +11 -35
  85. package/node/audits/auditLogs.js +8 -6
  86. package/node/audits/auditsMetadata.js +8 -6
  87. package/node/audits/hooks.js +7 -34
  88. package/node/auth/acceptInvitation.js +7 -5
  89. package/node/auth/activateAccount.js +7 -4
  90. package/node/auth/allAccounts.js +27 -0
  91. package/node/auth/apiTokens.js +8 -6
  92. package/node/auth/customLogin.js +11 -12
  93. package/node/auth/entitlements.js +119 -22
  94. package/node/auth/forgotPassword.js +8 -6
  95. package/node/auth/groups.js +12 -12
  96. package/node/auth/hooks.js +77 -67
  97. package/node/auth/impersonate.js +7 -6
  98. package/node/auth/index.js +103 -101
  99. package/node/auth/login.js +7 -6
  100. package/node/auth/mfa.js +10 -4
  101. package/node/auth/passkeys.js +7 -6
  102. package/node/auth/profile.js +10 -9
  103. package/node/auth/provisioning.js +7 -6
  104. package/node/auth/resetPhoneNumber.js +7 -6
  105. package/node/auth/roles.js +7 -6
  106. package/node/auth/security/index.js +49 -0
  107. package/node/auth/security/restrictions.js +10 -10
  108. package/node/auth/security/securityCenter.js +8 -6
  109. package/node/auth/security/securityPolicy.js +16 -30
  110. package/node/auth/security/sessionsPolicy.js +8 -6
  111. package/node/auth/sessions.js +7 -6
  112. package/node/auth/signup.js +10 -4
  113. package/node/auth/sms.js +16 -0
  114. package/node/auth/socialLogin.js +7 -4
  115. package/node/auth/sso.js +7 -4
  116. package/node/auth/stepUp.js +62 -0
  117. package/node/auth/team.js +25 -4
  118. package/node/auth/tenants.js +8 -6
  119. package/node/common/CustomComponentHolder.js +3 -1
  120. package/node/common/index.js +2 -2
  121. package/node/connectivity/hooks.js +5 -8
  122. package/node/index.js +16 -16
  123. package/node/subscriptions/hooks.js +84 -51
  124. package/node/tests/hooks.tsc-test.js +339 -0
  125. package/node/useSnapshot/index.js +137 -0
  126. package/node/vendor/hooks.js +5 -6
  127. package/package.json +3 -3
  128. package/subscriptions/hooks.d.ts +32 -10
  129. package/subscriptions/hooks.js +81 -49
  130. package/tests/hooks.tsc-test.js +338 -0
  131. package/useSnapshot/index.d.ts +78 -0
  132. package/useSnapshot/index.js +128 -0
  133. package/useSnapshot/package.json +6 -0
  134. package/vendor/hooks.d.ts +0 -1
  135. package/vendor/hooks.js +9 -7
  136. package/auth/MSP/allAccounts.js +0 -15
  137. package/node/auth/MSP/allAccounts.js +0 -24
@@ -1,16 +1,35 @@
1
- import { Entitlement, EntitledToOptions } from '@frontegg/redux-store';
1
+ import { CustomAttributes } from '@frontegg/entitlements-javascript-commons';
2
+ import { EntitledToOptions, Entitlement } from '@frontegg/redux-store';
2
3
  /**
3
- @param key
4
- @returns if the user is entitled to the given feature. Attaching the justification if not
5
- */
6
- export declare const useFeatureEntitlements: (key: string) => Entitlement;
4
+ @param key feature key
5
+ @param customAttributes user attributes
6
+ @returns if the user is entitled to the given feature and attributes. Attaching the justification if not
7
+ */
8
+ export declare const useFeatureEntitlements: (key: string, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
7
9
  /**
8
- @param key
9
- @returns if the user is entitled to the given permission. Attaching the justification if not
10
- */
11
- export declare const usePermissionEntitlements: (key: string) => Entitlement;
10
+ @param key permission key
11
+ @param customAttributes user attributes
12
+ @returns if the user is entitled to the given permission and attributes. Attaching the justification if not
13
+ */
14
+ export declare const usePermissionEntitlements: (key: string, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
12
15
  /**
13
- @param entitledToOptions including permission or feature key
14
- @returns if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
15
- */
16
- export declare const useEntitlements: (options: EntitledToOptions) => Entitlement;
16
+ @param entitledToOptions including permission or feature key
17
+ @param customAttributes user attributes
18
+ @returns if the user is entitled to the given feature or permission and attributes (check only one). Attaching the justification if not
19
+ */
20
+ export declare const useEntitlements: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
21
+ /**
22
+ @param entitledToOptions including permission or feature key
23
+ @returns an action your can use to detect if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
24
+ */
25
+ export declare const useEntitlementsActions: () => {
26
+ isEntitledTo: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
27
+ };
28
+ /**
29
+ @returns if the option to use entitlements is enabled
30
+ */
31
+ export declare const useEntitlementsOptions: () => {
32
+ isEntitlementsEnabled: boolean;
33
+ isEntitledTo: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
34
+ verifyIsEntitledFF: boolean;
35
+ };
@@ -1,36 +1,131 @@
1
- import { getPermissionEntitlements, getFeatureEntitlements, getEntitlements } from '@frontegg/redux-store';
2
- import { useAuth } from './hooks';
1
+ import { FeatureFlags, USE_ENTITLEMENTS_V2_ENDPOINT_FF } from '@frontegg/rest-api';
2
+ import { useAuthUserOrNull } from './hooks';
3
+ import { useMemo } from 'react';
4
+ import { useRootState, useShadowDom } from '../common';
5
+ import { useFeatureFlags } from '../flags';
6
+ import { useStore } from '../FronteggStoreContext';
7
+ import { useSnapshot } from '../useSnapshot';
8
+ import { getEntitlements, getFeatureEntitlements, getPermissionEntitlements } from '@frontegg/redux-store';
3
9
 
4
10
  /**
5
11
  * @returns entitlemenets state as stored in the user
6
12
  */
7
- const useEntitlementsState = () => useAuth(({
8
- user
9
- }) => user == null ? void 0 : user.entitlements);
13
+ const useEntitlementsState = () => {
14
+ var _useSnapshot$user$ent, _useSnapshot$user;
15
+ const state = useStore().store.auth;
16
+ return (_useSnapshot$user$ent = (_useSnapshot$user = useSnapshot(state).user) == null ? void 0 : _useSnapshot$user.entitlements) != null ? _useSnapshot$user$ent : {};
17
+ };
10
18
 
11
19
  /**
12
- @param key
13
- @returns if the user is entitled to the given feature. Attaching the justification if not
14
- */
15
- export const useFeatureEntitlements = key => {
16
- const entitlements = useEntitlementsState();
17
- return getFeatureEntitlements(entitlements, key);
20
+ * @returns user state
21
+ */
22
+ const useUserState = () => {
23
+ return useAuthUserOrNull() || undefined;
18
24
  };
19
25
 
20
26
  /**
21
- @param key
22
- @returns if the user is entitled to the given permission. Attaching the justification if not
23
- */
24
- export const usePermissionEntitlements = key => {
27
+ * @param customAttributes user attributes
28
+ * @returns is entitled query data including: entitltments state, final attributes (consumer and frontegg) and API version to use
29
+ */
30
+ const useEntitlementsQueryData = customAttributes => {
31
+ const user = useUserState();
25
32
  const entitlements = useEntitlementsState();
26
- return getPermissionEntitlements(entitlements, key);
33
+ const {
34
+ appName
35
+ } = useRootState();
36
+ const [useEntitlementsV2] = FeatureFlags.getFeatureFlags([USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
37
+ const attributes = {
38
+ custom: customAttributes,
39
+ jwt: user
40
+ };
41
+ return {
42
+ entitlements,
43
+ attributes,
44
+ isV2: useEntitlementsV2
45
+ };
27
46
  };
28
47
 
29
48
  /**
30
- @param entitledToOptions including permission or feature key
31
- @returns if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
32
- */
33
- export const useEntitlements = options => {
49
+ @param key feature key
50
+ @param customAttributes user attributes
51
+ @returns if the user is entitled to the given feature and attributes. Attaching the justification if not
52
+ */
53
+ export const useFeatureEntitlements = (key, customAttributes) => {
54
+ const {
55
+ entitlements,
56
+ attributes,
57
+ isV2
58
+ } = useEntitlementsQueryData(customAttributes);
59
+ return getFeatureEntitlements(entitlements, key, attributes, isV2);
60
+ };
61
+
62
+ /**
63
+ @param key permission key
64
+ @param customAttributes user attributes
65
+ @returns if the user is entitled to the given permission and attributes. Attaching the justification if not
66
+ */
67
+ export const usePermissionEntitlements = (key, customAttributes) => {
68
+ const {
69
+ entitlements,
70
+ attributes,
71
+ isV2
72
+ } = useEntitlementsQueryData(customAttributes);
73
+ return getPermissionEntitlements(entitlements, key, attributes, isV2);
74
+ };
75
+
76
+ /**
77
+ @param entitledToOptions including permission or feature key
78
+ @param customAttributes user attributes
79
+ @returns if the user is entitled to the given feature or permission and attributes (check only one). Attaching the justification if not
80
+ */
81
+ export const useEntitlements = (options, customAttributes) => {
82
+ const {
83
+ entitlements,
84
+ attributes,
85
+ isV2
86
+ } = useEntitlementsQueryData(customAttributes);
87
+ return getEntitlements(entitlements, options, attributes, isV2);
88
+ };
89
+
90
+ /**
91
+ @param entitledToOptions including permission or feature key
92
+ @returns an action your can use to detect if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
93
+ */
94
+ export const useEntitlementsActions = () => {
95
+ // this code is duplicated because React is yelling when using useEntitlementsQueryData inside the isEntitledTo function because it's not a hook
96
+ const user = useUserState();
34
97
  const entitlements = useEntitlementsState();
35
- return getEntitlements(entitlements, options);
98
+ const {
99
+ appName
100
+ } = useRootState();
101
+ const [useEntitlementsV2] = FeatureFlags.getFeatureFlags([USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
102
+ return useMemo(() => ({
103
+ isEntitledTo: (options, customAttributes) => {
104
+ const attributes = {
105
+ custom: customAttributes,
106
+ jwt: user
107
+ };
108
+ return getEntitlements(entitlements, options, attributes, useEntitlementsV2);
109
+ }
110
+ }), [user, entitlements, useEntitlementsV2]);
111
+ };
112
+
113
+ /**
114
+ @returns if the option to use entitlements is enabled
115
+ */
116
+ export const useEntitlementsOptions = () => {
117
+ var _entitlementsOptions$;
118
+ const {
119
+ entitlementsOptions
120
+ } = useShadowDom();
121
+ const isEntitlementsEnabled = (_entitlementsOptions$ = entitlementsOptions == null ? void 0 : entitlementsOptions.enabled) != null ? _entitlementsOptions$ : false;
122
+ const {
123
+ isEntitledTo
124
+ } = useEntitlementsActions();
125
+ const [verifyIsEntitledFF] = useFeatureFlags(['admin-portal-use-is-entitled']);
126
+ return {
127
+ isEntitlementsEnabled,
128
+ isEntitledTo,
129
+ verifyIsEntitledFF
130
+ };
36
131
  };
@@ -1,5 +1,3 @@
1
1
  import { ForgotPasswordState, ForgotPasswordActions } from '@frontegg/redux-store';
2
- export declare type ForgotPasswordStateMapper<S> = (state: ForgotPasswordState) => S;
3
2
  export declare function useForgotPasswordState(): ForgotPasswordState;
4
- export declare function useForgotPasswordState<S>(stateMapper: ForgotPasswordStateMapper<S>): S;
5
3
  export declare const useForgotPasswordActions: () => ForgotPasswordActions;
@@ -1,7 +1,9 @@
1
- import { forgotPasswordReducers, forgotPasswordActions } from '@frontegg/redux-store';
2
- import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
- const defaultMapper = state => state;
4
- export function useForgotPasswordState(stateMapper = defaultMapper) {
5
- return stateHookGenerator(stateMapper, 'forgotPasswordState');
1
+ import { useStore } from '../FronteggStoreContext';
2
+ import { useSnapshot } from '../useSnapshot';
3
+ export function useForgotPasswordState() {
4
+ const state = useStore().store.auth.forgotPasswordState;
5
+ return useSnapshot(state);
6
6
  }
7
- export const useForgotPasswordActions = () => reducerActionsGenerator(forgotPasswordActions, forgotPasswordReducers);
7
+ export const useForgotPasswordActions = () => {
8
+ return useStore().stateActions.auth.forgotPasswordActions;
9
+ };
package/auth/groups.d.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  import { GroupsState, GroupsActions, GroupsDialogsState, GroupsDialogsActions } from '@frontegg/redux-store';
2
- export declare type GroupsStateMapper<S> = (state: GroupsState) => S;
3
2
  export declare function useGroupsState(): GroupsState;
4
- export declare function useGroupsState<S>(stateMapper: GroupsStateMapper<S>): S;
5
3
  export declare const useGroupsActions: () => GroupsActions;
6
- export declare type GroupsDialogsStateMapper<S> = (state: GroupsDialogsState) => S;
7
4
  export declare function useGroupsDialogsState(): GroupsDialogsState;
8
- export declare function useGroupsDialogsState<S>(stateMapper: GroupsDialogsStateMapper<S>): S;
9
5
  export declare const useGroupsDialogsActions: () => GroupsDialogsActions;
package/auth/groups.js CHANGED
@@ -1,15 +1,14 @@
1
- import { groupsActions, groupsReducers, groupsDialogsActions, groupsDialogsReducers } from '@frontegg/redux-store';
2
- import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
- const defaultMapper = state => state;
4
- export function useGroupsState(stateMapper = defaultMapper) {
5
- return stateHookGenerator(stateMapper, 'groupsState');
1
+ import { useStore } from '../FronteggStoreContext';
2
+ import { useSnapshot } from '../useSnapshot';
3
+ export function useGroupsState() {
4
+ return useSnapshot(useStore().store.auth.groupsState);
6
5
  }
7
- export const useGroupsActions = () => reducerActionsGenerator(groupsActions, groupsReducers);
8
-
9
- //dialogs
10
-
11
- const defaultDialogsMapper = state => state;
12
- export function useGroupsDialogsState(stateMapper = defaultDialogsMapper) {
13
- return stateHookGenerator(stateMapper, 'groupsDialogsState');
6
+ export const useGroupsActions = () => {
7
+ return useStore().stateActions.auth.groupsActions;
8
+ };
9
+ export function useGroupsDialogsState() {
10
+ return useSnapshot(useStore().store.auth.groupsDialogsState);
14
11
  }
15
- export const useGroupsDialogsActions = () => reducerActionsGenerator(groupsDialogsActions, groupsDialogsReducers);
12
+ export const useGroupsDialogsActions = () => {
13
+ return useStore().stateActions.auth.groupsDialogsActions;
14
+ };
package/auth/hooks.d.ts CHANGED
@@ -1,14 +1,27 @@
1
- import { CaseReducerActions, SliceCaseReducers } from '@frontegg/redux-store';
2
- import { AuthActions, AuthPageRoutes, AuthState, User } from '@frontegg/redux-store';
1
+ import { AuthActions, AuthState } from '@frontegg/redux-store';
3
2
  import { RedirectOptions } from '@frontegg/rest-api';
3
+ import { AuthPageRoutes, User } from '@frontegg/redux-store';
4
4
  export declare type AuthMapper = {
5
5
  state: (state: AuthState) => any;
6
6
  actions: (actions: AuthActions) => any;
7
7
  };
8
- export declare type StateHookFunction<T> = (() => T) & (<S extends {}>(mapper: (state: T) => S) => S);
9
8
  export declare type AuthStateMapper<S> = (state: AuthState) => S;
9
+ /**
10
+ * Use this `frontegg` hook function to obtain the complete authentication state, if it exists.
11
+ *
12
+ * ```jsx
13
+ * export const MyFunctionComponent = () => {
14
+ * const { isAuthenticated, user } = useAuth();
15
+ *
16
+ * return isAuthenticated ? <div>Hello, User {user.name}</div> : null;
17
+ * }
18
+ * ```
19
+ *
20
+ * You can also utilize other `frontegg` hooks like `useAuthUser` to specifically retrieve the user and redirect to the login page if necessary, `useAuthUserOrNull` to get the user if available, and `useIsAuthenticated` for checking authentication status.
21
+ */
10
22
  export declare function useAuth(): AuthState;
11
23
  export declare function useAuth<S>(stateMapper: AuthStateMapper<S>): S;
24
+ export declare function useAuthState(): AuthState;
12
25
  /**
13
26
  * ```jsx
14
27
  * export const MyFunctionComponent = () => {
@@ -24,9 +37,24 @@ export declare function useAuth<S>(stateMapper: AuthStateMapper<S>): S;
24
37
  * }
25
38
  * ```
26
39
  *
27
- * use this frontegg hook function to get if user is "Authenticated"
40
+ /**
41
+ * Use this frontegg hook function to redirect the user to the login page when in hosted login mode.
42
+ * If the user is already authenticated, this method will direct the user to the store, and you can retrieve user information using the `useAuthUserOrNull` method.
43
+ *
44
+ * To ensure the user is available on the first page load when authenticated, configure this option in your `FronteggProvider`:
45
+ * `authOptions`:
46
+ * `hostedLoginOptions`:
47
+ * `loadUserOnFirstLoad: true`
48
+ *
49
+ * When using this option, you can have the user on the first load, and you can control when the user is redirected to the login page by using `loginWithRedirect`.
28
50
  */
29
- export declare const useLoginWithRedirect: () => AuthActions['requestHostedLoginAuthorize'];
51
+ export declare const useLoginWithRedirect: () => (additionalParams?: Record<string, string> | undefined) => Promise<void>;
52
+ export declare const useLoginWithRedirectV2: () => (payload?: {
53
+ additionalParams?: Record<string, string> | undefined;
54
+ shouldRedirectToLogin?: boolean | undefined;
55
+ firstTime?: boolean | undefined;
56
+ loginDirectAction?: import("@frontegg/redux-store").LoginDirectAction | undefined;
57
+ } | undefined) => Promise<void>;
30
58
  export declare const useAuthActions: () => AuthActions;
31
59
  export declare const useOnRedirectTo: () => (path: string, opts?: RedirectOptions | undefined) => void;
32
60
  export declare const useAuthRoutes: () => AuthPageRoutes;
@@ -49,14 +77,18 @@ export declare const useIsAuthenticated: () => boolean;
49
77
  * }
50
78
  * ```
51
79
  *
52
- * use this frontegg hook function to get the authenticated user
53
- * the return user is null if not authenticated
80
+ * Use this `frontegg` hook function to obtain the authenticated user. If the user is not authenticated, this method will immediately redirect the user to the login page. (Primarily intended for embedded mode usage)
81
+ * To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
54
82
  */
55
83
  export declare const useAuthUser: () => User;
56
- export declare const useAuthUserOrNull: () => User | null;
57
84
  /**
58
- * hooks helpers
85
+ * ```jsx
86
+ * export const MyFunctionComponent = () => {
87
+ * const user = useAuthUserOrNull();
88
+ * return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
89
+ * }
90
+ * ```
91
+ *
92
+ * Use this `frontegg` hook function to retrieve the authenticated user. If the user is not authenticated, this hook will return null. To redirect the user to the login page in case they are not authenticated, use the `useAuthUser` method.
59
93
  */
60
- export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
61
- export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuthState, enableMapperFalsyReturnValue?: boolean) => any;
62
- export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
94
+ export declare const useAuthUserOrNull: () => User | null;
package/auth/hooks.js CHANGED
@@ -1,17 +1,34 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import { useContext, useMemo } from 'react';
3
- import { bindActionCreators } from '@frontegg/redux-store';
4
- import { authActions, authStoreName } from '@frontegg/redux-store';
5
- import { shallowEqual } from 'react-redux';
6
- import { useSelector, useDispatch } from '../FronteggStoreContext';
1
+ import { useContext } from 'react';
7
2
  import { ContextHolder } from '@frontegg/rest-api';
8
- import { ShadowDomContext } from '../common';
3
+ import { FronteggStoreContext, useStore } from '../FronteggStoreContext';
4
+ import { useSnapshot } from '../useSnapshot';
9
5
  const defaultMapper = {
10
6
  state: state => state,
11
7
  actions: actions => actions
12
8
  };
9
+
10
+ /**
11
+ * Use this `frontegg` hook function to obtain the complete authentication state, if it exists.
12
+ *
13
+ * ```jsx
14
+ * export const MyFunctionComponent = () => {
15
+ * const { isAuthenticated, user } = useAuth();
16
+ *
17
+ * return isAuthenticated ? <div>Hello, User {user.name}</div> : null;
18
+ * }
19
+ * ```
20
+ *
21
+ * You can also utilize other `frontegg` hooks like `useAuthUser` to specifically retrieve the user and redirect to the login page if necessary, `useAuthUserOrNull` to get the user if available, and `useIsAuthenticated` for checking authentication status.
22
+ */
23
+
13
24
  export function useAuth(stateMapper = defaultMapper.state) {
14
- return useSelector(state => stateMapper(state[authStoreName]), shallowEqual);
25
+ const state = useStore().store.auth;
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ return stateMapper(useSnapshot(state));
28
+ }
29
+ export function useAuthState() {
30
+ const state = useStore().store.auth;
31
+ return useSnapshot(state);
15
32
  }
16
33
 
17
34
  /**
@@ -29,22 +46,39 @@ export function useAuth(stateMapper = defaultMapper.state) {
29
46
  * }
30
47
  * ```
31
48
  *
32
- * use this frontegg hook function to get if user is "Authenticated"
49
+ /**
50
+ * Use this frontegg hook function to redirect the user to the login page when in hosted login mode.
51
+ * If the user is already authenticated, this method will direct the user to the store, and you can retrieve user information using the `useAuthUserOrNull` method.
52
+ *
53
+ * To ensure the user is available on the first page load when authenticated, configure this option in your `FronteggProvider`:
54
+ * `authOptions`:
55
+ * `hostedLoginOptions`:
56
+ * `loadUserOnFirstLoad: true`
57
+ *
58
+ * When using this option, you can have the user on the first load, and you can control when the user is redirected to the login page by using `loginWithRedirect`.
33
59
  */
34
60
  export const useLoginWithRedirect = () => {
35
- const dispatch = useDispatch();
36
- return useMemo(() => bindActionCreators(authActions.requestHostedLoginAuthorize, dispatch), [authActions.requestHostedLoginAuthorize]);
61
+ const {
62
+ actions
63
+ } = useContext(FronteggStoreContext);
64
+ return actions.requestHostedLoginAuthorize;
65
+ };
66
+ export const useLoginWithRedirectV2 = () => {
67
+ const {
68
+ actions
69
+ } = useContext(FronteggStoreContext);
70
+ return actions.requestHostedLoginAuthorizeV2;
37
71
  };
38
72
  export const useAuthActions = () => {
39
- const dispatch = useDispatch();
40
- return useMemo(() => bindActionCreators(authActions, dispatch), [authActions]);
73
+ return useStore().actions;
41
74
  };
42
75
  export const useOnRedirectTo = () => {
43
- var _context$onRedirectTo;
44
- const context = useContext(ShadowDomContext);
45
- return (_context$onRedirectTo = context == null ? void 0 : context.onRedirectTo) != null ? _context$onRedirectTo : ContextHolder.onRedirectTo;
76
+ const {
77
+ onRedirectTo
78
+ } = useAuth();
79
+ return onRedirectTo || ContextHolder.onRedirectTo;
46
80
  };
47
- export const useAuthRoutes = () => useAuth(state => _extends({}, state.routes));
81
+ export const useAuthRoutes = () => useAuthState().routes;
48
82
 
49
83
  /**
50
84
  * ```jsx
@@ -56,11 +90,12 @@ export const useAuthRoutes = () => useAuth(state => _extends({}, state.routes));
56
90
  *
57
91
  * use this frontegg hook function to get if user is "Authenticated"
58
92
  */
59
- export const useIsAuthenticated = () => useSelector(({
60
- [authStoreName]: {
93
+ export const useIsAuthenticated = () => {
94
+ const {
61
95
  isAuthenticated
62
- }
63
- }) => isAuthenticated, shallowEqual);
96
+ } = useSnapshot(useStore().store.auth);
97
+ return isAuthenticated;
98
+ };
64
99
 
65
100
  /**
66
101
  * ```jsx
@@ -70,8 +105,8 @@ export const useIsAuthenticated = () => useSelector(({
70
105
  * }
71
106
  * ```
72
107
  *
73
- * use this frontegg hook function to get the authenticated user
74
- * the return user is null if not authenticated
108
+ * Use this `frontegg` hook function to obtain the authenticated user. If the user is not authenticated, this method will immediately redirect the user to the login page. (Primarily intended for embedded mode usage)
109
+ * To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
75
110
  */
76
111
  export const useAuthUser = () => {
77
112
  const {
@@ -79,7 +114,9 @@ export const useAuthUser = () => {
79
114
  customLoginUrl
80
115
  } = useAuthRoutes();
81
116
  const onRedirectTo = useOnRedirectTo();
82
- const user = useAuthUserOrNull();
117
+ const {
118
+ user
119
+ } = useAuth();
83
120
  const isSSR = typeof window === 'undefined';
84
121
  const noUser = {};
85
122
  if (user == null && !isSSR) {
@@ -90,46 +127,20 @@ export const useAuthUser = () => {
90
127
  }
91
128
  return user || noUser;
92
129
  };
130
+
131
+ /**
132
+ * ```jsx
133
+ * export const MyFunctionComponent = () => {
134
+ * const user = useAuthUserOrNull();
135
+ * return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
136
+ * }
137
+ * ```
138
+ *
139
+ * Use this `frontegg` hook function to retrieve the authenticated user. If the user is not authenticated, this hook will return null. To redirect the user to the login page in case they are not authenticated, use the `useAuthUser` method.
140
+ */
93
141
  export const useAuthUserOrNull = () => {
94
142
  const {
95
143
  user
96
- } = useSelector(({
97
- [authStoreName]: {
98
- user
99
- }
100
- }) => ({
101
- user
102
- }), shallowEqual);
144
+ } = useAuth();
103
145
  return user || null;
104
- };
105
-
106
- /**
107
- * hooks helpers
108
- */
109
- export const sliceReducerActionsBy = reducer => {
110
- const reducerKeys = Object.keys(reducer);
111
- const reducerActions = reducerKeys.map(key => ({
112
- [key]: authActions[key]
113
- }));
114
- return reducerActions.reduce((p, n) => _extends({}, p, n), {});
115
- };
116
-
117
- /*
118
- * enableMapperFalsyReturnValue was added to be backward compatible with all usages
119
- * the scenario that require enableMapperFalsyReturnValue of true is when the
120
- * mapper function returns a falsy value then we want to return it as is - it's a valid case like in entitlements (when it's undefined)
121
- * IMO all usages should be like that
122
- */
123
- export const stateHookGenerator = (stateMapper, subState, enableMapperFalsyReturnValue = false) => {
124
- return useSelector(state => {
125
- const mapperValue = stateMapper == null ? void 0 : stateMapper(state[authStoreName][subState]);
126
- if (enableMapperFalsyReturnValue && stateMapper) {
127
- return mapperValue;
128
- }
129
- return mapperValue != null ? mapperValue : state[authStoreName][subState];
130
- }, shallowEqual);
131
- };
132
- export const reducerActionsGenerator = (actions, reducers) => {
133
- const dispatch = useDispatch();
134
- return useMemo(() => bindActionCreators(_extends({}, actions, sliceReducerActionsBy(reducers)), dispatch), [dispatch]);
135
146
  };
@@ -1,5 +1,3 @@
1
1
  import { ImpersonateState, ImpersonateActions } from '@frontegg/redux-store';
2
- export declare type ImpersonateStateMapper<S> = (state: ImpersonateState) => S;
3
2
  export declare function useImpersonateState(): ImpersonateState;
4
- export declare function useImpersonateState<S>(stateMapper: ImpersonateStateMapper<S>): S;
5
3
  export declare const useImpersonateActions: () => ImpersonateActions;
@@ -1,7 +1,8 @@
1
- import { impersonateActions, impersonateReducers } from '@frontegg/redux-store';
2
- import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
- const defaultMapper = state => state;
4
- export function useImpersonateState(stateMapper = defaultMapper) {
5
- return stateHookGenerator(stateMapper, 'impersonateState');
1
+ import { useSnapshot } from '../useSnapshot';
2
+ import { useStore } from '../FronteggStoreContext';
3
+ export function useImpersonateState() {
4
+ return useSnapshot(useStore().store.auth.impersonateState);
6
5
  }
7
- export const useImpersonateActions = () => reducerActionsGenerator(impersonateActions, impersonateReducers);
6
+ export const useImpersonateActions = () => {
7
+ return useStore().stateActions.auth.impersonateActions;
8
+ };
package/auth/index.d.ts CHANGED
@@ -1,27 +1,26 @@
1
- export { useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect, } from './hooks';
1
+ export { useAuthState, useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect, useLoginWithRedirectV2, } from './hooks';
2
2
  export * from './acceptInvitation';
3
3
  export * from './activateAccount';
4
+ export * from './allAccounts';
4
5
  export * from './apiTokens';
6
+ export * from './customLogin';
7
+ export * from './entitlements';
5
8
  export * from './forgotPassword';
6
- export * from './resetPhoneNumber';
9
+ export * from './groups';
10
+ export * from './impersonate';
7
11
  export * from './login';
8
12
  export * from './mfa';
13
+ export * from './passkeys';
9
14
  export * from './profile';
15
+ export * from './provisioning';
16
+ export * from './resetPhoneNumber';
17
+ export * from './roles';
18
+ export * from './sessions';
10
19
  export * from './signup';
20
+ export * from './sms';
11
21
  export * from './socialLogin';
12
- export * from './team';
13
22
  export * from './sso';
14
- export * from './security/securityPolicy';
23
+ export * from './stepUp';
24
+ export * from './team';
25
+ export * from './security';
15
26
  export * from './tenants';
16
- export * from './roles';
17
- export * from './sessions';
18
- export * from './security/sessionsPolicy';
19
- export * from './security/restrictions';
20
- export * from './provisioning';
21
- export * from './impersonate';
22
- export * from './passkeys';
23
- export * from './groups';
24
- export * from './customLogin';
25
- export * from './MSP/allAccounts';
26
- export * from './entitlements';
27
- export * from './security/securityCenter';
package/auth/index.js CHANGED
@@ -1,27 +1,26 @@
1
- export { useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect } from './hooks';
1
+ export { useAuthState, useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect, useLoginWithRedirectV2 } from './hooks';
2
2
  export * from './acceptInvitation';
3
3
  export * from './activateAccount';
4
+ export * from './allAccounts';
4
5
  export * from './apiTokens';
6
+ export * from './customLogin';
7
+ export * from './entitlements';
5
8
  export * from './forgotPassword';
6
- export * from './resetPhoneNumber';
9
+ export * from './groups';
10
+ export * from './impersonate';
7
11
  export * from './login';
8
12
  export * from './mfa';
13
+ export * from './passkeys';
9
14
  export * from './profile';
15
+ export * from './provisioning';
16
+ export * from './resetPhoneNumber';
17
+ export * from './roles';
18
+ export * from './sessions';
10
19
  export * from './signup';
20
+ export * from './sms';
11
21
  export * from './socialLogin';
12
- export * from './team';
13
22
  export * from './sso';
14
- export * from './security/securityPolicy';
15
- export * from './tenants';
16
- export * from './roles';
17
- export * from './sessions';
18
- export * from './security/sessionsPolicy';
19
- export * from './security/restrictions';
20
- export * from './provisioning';
21
- export * from './impersonate';
22
- export * from './passkeys';
23
- export * from './groups';
24
- export * from './customLogin';
25
- export * from './MSP/allAccounts';
26
- export * from './entitlements';
27
- export * from './security/securityCenter';
23
+ export * from './stepUp';
24
+ export * from './team';
25
+ export * from './security';
26
+ export * from './tenants';
package/auth/login.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { LoginState, LoginActions } from '@frontegg/redux-store';
2
- export declare type LoginStateMapper<S> = (state: LoginState) => S;
1
+ import type { LoginState, LoginActions } from '@frontegg/redux-store';
3
2
  export declare function useLoginState(): LoginState;
4
- export declare function useLoginState<S>(stateMapper: LoginStateMapper<S>): S;
5
3
  export declare const useLoginActions: () => LoginActions;