@frontegg/react-hooks 5.59.1 → 6.0.1-alpha.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.
Files changed (107) hide show
  1. package/FronteggProvider.d.ts +11 -10
  2. package/FronteggProvider.js +137 -0
  3. package/FronteggStoreContext.d.ts +8 -8
  4. package/FronteggStoreContext.js +13 -0
  5. package/audits/auditLogs.d.ts +5 -5
  6. package/audits/auditLogs.js +4 -0
  7. package/audits/auditsMetadata.d.ts +5 -5
  8. package/audits/auditsMetadata.js +4 -0
  9. package/audits/hooks.d.ts +12 -12
  10. package/audits/hooks.js +35 -0
  11. package/audits/index.d.ts +3 -3
  12. package/audits/index.js +3 -5
  13. package/auth/acceptInvitation.d.ts +5 -5
  14. package/auth/acceptInvitation.js +4 -0
  15. package/auth/activateAccount.d.ts +5 -5
  16. package/auth/activateAccount.js +4 -0
  17. package/auth/apiTokens.d.ts +5 -5
  18. package/auth/apiTokens.js +4 -0
  19. package/auth/forgotPassword.d.ts +5 -5
  20. package/auth/forgotPassword.js +4 -0
  21. package/auth/hooks.d.ts +62 -62
  22. package/auth/hooks.js +79 -0
  23. package/auth/index.d.ts +16 -16
  24. package/auth/index.js +16 -7
  25. package/auth/login.d.ts +5 -5
  26. package/auth/login.js +4 -0
  27. package/auth/mfa.d.ts +4 -4
  28. package/auth/mfa.js +4 -0
  29. package/auth/profile.d.ts +6 -6
  30. package/auth/profile.js +16 -0
  31. package/auth/resetPhoneNumber.d.ts +5 -5
  32. package/auth/resetPhoneNumber.js +4 -0
  33. package/auth/roles.d.ts +5 -5
  34. package/auth/roles.js +4 -0
  35. package/auth/securityPolicy.d.ts +11 -11
  36. package/auth/securityPolicy.js +25 -0
  37. package/auth/signup.d.ts +5 -5
  38. package/auth/signup.js +4 -0
  39. package/auth/socialLogin.d.ts +5 -5
  40. package/auth/socialLogin.js +4 -0
  41. package/auth/sso.d.ts +4 -4
  42. package/auth/sso.js +4 -0
  43. package/auth/team.d.ts +5 -5
  44. package/auth/team.js +4 -0
  45. package/auth/tenants.d.ts +5 -5
  46. package/auth/tenants.js +4 -0
  47. package/common/index.d.ts +17 -17
  48. package/common/index.js +30 -0
  49. package/common/package.json +6 -0
  50. package/connectivity/hooks.d.ts +5 -5
  51. package/connectivity/hooks.js +15 -0
  52. package/connectivity/index.d.ts +1 -1
  53. package/connectivity/index.js +1 -5
  54. package/index.d.ts +8 -8
  55. package/index.js +13 -93
  56. package/node/FronteggProvider.js +160 -0
  57. package/node/FronteggStoreContext.js +49 -0
  58. package/node/audits/auditLogs.js +18 -0
  59. package/node/audits/auditsMetadata.js +18 -0
  60. package/node/audits/hooks.js +62 -0
  61. package/node/audits/index.js +46 -13
  62. package/node/auth/acceptInvitation.js +18 -0
  63. package/node/auth/activateAccount.js +18 -0
  64. package/node/auth/apiTokens.js +18 -0
  65. package/node/auth/forgotPassword.js +18 -0
  66. package/node/auth/hooks.js +127 -0
  67. package/node/auth/index.js +275 -54
  68. package/node/auth/login.js +18 -0
  69. package/node/auth/mfa.js +18 -0
  70. package/node/auth/profile.js +34 -0
  71. package/node/auth/resetPhoneNumber.js +18 -0
  72. package/node/auth/roles.js +18 -0
  73. package/node/auth/securityPolicy.js +43 -0
  74. package/node/auth/signup.js +18 -0
  75. package/node/auth/socialLogin.js +18 -0
  76. package/node/auth/sso.js +18 -0
  77. package/node/auth/team.js +18 -0
  78. package/node/auth/tenants.js +18 -0
  79. package/node/common/index.js +44 -0
  80. package/node/connectivity/hooks.js +31 -0
  81. package/node/connectivity/index.js +17 -12
  82. package/node/index.js +111 -187
  83. package/node/subscriptions/hooks.js +131 -0
  84. package/node/subscriptions/index.js +15 -27
  85. package/node/vendor/hooks.js +31 -0
  86. package/node/vendor/index.js +17 -12
  87. package/package.json +12 -7
  88. package/subscriptions/hooks.d.ts +19 -19
  89. package/subscriptions/hooks.js +69 -0
  90. package/subscriptions/index.d.ts +1 -1
  91. package/subscriptions/index.js +1 -5
  92. package/vendor/hooks.d.ts +5 -5
  93. package/vendor/hooks.js +15 -0
  94. package/vendor/index.d.ts +1 -1
  95. package/vendor/index.js +1 -5
  96. package/FronteggStoreContext-d6c3b684.js +0 -9
  97. package/auditsMetadata-9ab88593.js +0 -35
  98. package/hooks-6400e233.js +0 -14
  99. package/hooks-7fc5d634.js +0 -64
  100. package/hooks-eaf879c9.js +0 -14
  101. package/node/FronteggStoreContext-6cfac56c.js +0 -18
  102. package/node/auditsMetadata-d15799a1.js +0 -42
  103. package/node/hooks-429ffd3a.js +0 -83
  104. package/node/hooks-5d5a445e.js +0 -17
  105. package/node/hooks-cf739b27.js +0 -17
  106. package/node/roles-f45dbc50.js +0 -215
  107. package/roles-f87bc6a0.js +0 -172
@@ -1,10 +1,11 @@
1
- import { FC } from 'react';
2
- import { ContextOptions } from '@frontegg/rest-api';
3
- import { FronteggAppInstance } from '@frontegg/types';
4
- declare type FronteggProviderProps = {
5
- app?: FronteggAppInstance;
6
- contextOptions?: ContextOptions;
7
- setLoading?: (loading: boolean) => void;
8
- };
9
- export declare const FronteggStoreProvider: FC<FronteggProviderProps>;
10
- export {};
1
+ import { FC, ReactNode } from 'react';
2
+ import { ContextOptions } from '@frontegg/rest-api';
3
+ import { FronteggAppInstance } from '@frontegg/types';
4
+ declare type FronteggProviderProps = {
5
+ app?: FronteggAppInstance;
6
+ contextOptions?: ContextOptions;
7
+ setLoading?: (loading: boolean) => void;
8
+ children?: ReactNode | undefined;
9
+ };
10
+ export declare const FronteggStoreProvider: FC<FronteggProviderProps>;
11
+ export {};
@@ -0,0 +1,137 @@
1
+ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2
+ const _excluded = ["authenticatedUrl", "signUpSuccessUrl"];
3
+ import React, { useEffect, useMemo, useRef, useState } from 'react';
4
+ import { Provider } from 'react-redux';
5
+ import { createFronteggStore } from '@frontegg/redux-store';
6
+ import { ContextHolder } from '@frontegg/rest-api';
7
+ import { useAuth, useAuthRoutes } from './auth';
8
+ import { FronteggStoreContext } from './FronteggStoreContext';
9
+ import { Fragment as _Fragment } from "react/jsx-runtime";
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+
12
+ const setLoading = loading => {
13
+ const isSSR = typeof document === 'undefined' || typeof window === 'undefined';
14
+
15
+ if (!isSSR) {
16
+ if (loading) {
17
+ document.body.classList.add('frontegg-loading');
18
+ } else {
19
+ document.body.classList.remove('frontegg-loading');
20
+ }
21
+ }
22
+ };
23
+
24
+ const HideChildrenIfFronteggRoutes = ({
25
+ children,
26
+ basename
27
+ }) => {
28
+ const routes = useAuthRoutes();
29
+ const uriRef = useRef(window.location.pathname);
30
+ const animationFrameRef = useRef(0);
31
+ const [uri, setUri] = useState(window.location.pathname);
32
+
33
+ const checker = () => {
34
+ if (uriRef.current !== window.location.pathname) {
35
+ uriRef.current = window.location.pathname;
36
+ setUri(document.location.pathname);
37
+ }
38
+
39
+ animationFrameRef.current = window.requestAnimationFrame(checker);
40
+ };
41
+
42
+ useEffect(() => {
43
+ document.addEventListener('frontegg_onRedirectTo_fired', () => {
44
+ if (uriRef.current !== window.location.pathname) {
45
+ uriRef.current = window.location.pathname;
46
+ setUri(document.location.pathname);
47
+ }
48
+ });
49
+ window.addEventListener('popstate', () => {
50
+ if (uriRef.current !== window.location.pathname) {
51
+ uriRef.current = window.location.pathname;
52
+ setUri(document.location.pathname);
53
+ }
54
+ });
55
+ animationFrameRef.current = window.requestAnimationFrame(checker);
56
+ return () => {
57
+ window.cancelAnimationFrame(animationFrameRef.current);
58
+ };
59
+ }, [setUri]);
60
+ const calculatedBasename = basename ? basename.endsWith('/') ? basename.substring(0, basename.length - 1) : '' : '';
61
+
62
+ const checkRoutes = _objectWithoutPropertiesLoose(routes, _excluded);
63
+
64
+ if (Object.values(checkRoutes).map(url => `${calculatedBasename}${url}`).indexOf(uri) !== -1) {
65
+ return null;
66
+ }
67
+
68
+ return _jsx(_Fragment, {
69
+ children: children
70
+ });
71
+ };
72
+
73
+ const FronteggContent = ({
74
+ children,
75
+ app,
76
+ setLoading
77
+ }) => {
78
+ var _app$options, _app$options2, _app$options3;
79
+
80
+ const {
81
+ isLoading
82
+ } = useAuth(({
83
+ isLoading
84
+ }) => ({
85
+ isLoading
86
+ }));
87
+ const customLoginBox = !app || (app == null ? void 0 : (_app$options = app.options) == null ? void 0 : _app$options.customLoginBox) === true;
88
+ const alwaysShowChildren = customLoginBox || (app == null ? void 0 : (_app$options2 = app.options) == null ? void 0 : _app$options2.hostedLoginBox) === true && (app == null ? void 0 : (_app$options3 = app.options) == null ? void 0 : _app$options3.framework) === 'nextjs';
89
+ const loadingRef = useRef(undefined);
90
+
91
+ if (isLoading !== loadingRef.current) {
92
+ loadingRef.current = isLoading;
93
+ setLoading(isLoading);
94
+ }
95
+
96
+ if (isLoading) {
97
+ return null;
98
+ }
99
+
100
+ if (alwaysShowChildren) {
101
+ return _jsx(_Fragment, {
102
+ children: children
103
+ });
104
+ }
105
+
106
+ return _jsx(HideChildrenIfFronteggRoutes, {
107
+ basename: app == null ? void 0 : app.options.basename,
108
+ children: children
109
+ });
110
+ };
111
+
112
+ export const FronteggStoreProvider = props => {
113
+ var _app$options$contextO, _app$options4, _app$options$previewM, _app$options5, _app$options$authOpti, _app$options6;
114
+
115
+ const {
116
+ children,
117
+ app,
118
+ contextOptions,
119
+ setLoading: setLoadingProps
120
+ } = props;
121
+ const context = (_app$options$contextO = app == null ? void 0 : (_app$options4 = app.options) == null ? void 0 : _app$options4.contextOptions) != null ? _app$options$contextO : contextOptions;
122
+ const previewMode = (_app$options$previewM = app == null ? void 0 : (_app$options5 = app.options) == null ? void 0 : _app$options5.previewMode) != null ? _app$options$previewM : false;
123
+ const authOptions = (_app$options$authOpti = app == null ? void 0 : (_app$options6 = app.options) == null ? void 0 : _app$options6.authOptions) != null ? _app$options$authOpti : {};
124
+ ContextHolder.setContext(context);
125
+ const store = useMemo(() => createFronteggStore({
126
+ context: context
127
+ }, app, previewMode, authOptions), [app, previewMode]);
128
+ return _jsx(Provider, {
129
+ context: FronteggStoreContext,
130
+ store: store,
131
+ children: _jsx(FronteggContent, {
132
+ setLoading: setLoadingProps != null ? setLoadingProps : setLoading,
133
+ app: app,
134
+ children: children
135
+ })
136
+ });
137
+ };
@@ -1,8 +1,8 @@
1
- import React from 'react';
2
- import { ReactReduxContextValue } from 'react-redux';
3
- export declare const FronteggStoreContext: React.Context<ReactReduxContextValue<any, import("redux").AnyAction>>;
4
- export declare const useSelector: <Selected extends unknown>(selector: (state: any) => Selected, equalityFn?: ((previous: Selected, next: Selected) => boolean) | undefined) => Selected;
5
- export declare const useDispatch: () => import("redux").Dispatch<import("redux").AnyAction>;
6
- export declare const useStore: () => import("redux").Store<any, import("redux").AnyAction>;
7
- export { shallowEqual, connectAdvanced, Provider } from 'react-redux';
8
- export default FronteggStoreContext;
1
+ import React from 'react';
2
+ import { ReactReduxContextValue } from 'react-redux';
3
+ export declare const FronteggStoreContext: React.Context<ReactReduxContextValue<any, import("redux").AnyAction>>;
4
+ export declare const useSelector: <Selected extends unknown>(selector: (state: any) => Selected, equalityFn?: ((previous: Selected, next: Selected) => boolean) | undefined) => Selected;
5
+ export declare const useDispatch: () => import("redux").Dispatch<import("redux").AnyAction>;
6
+ export declare const useStore: () => import("redux").Store<any, import("redux").AnyAction>;
7
+ export { shallowEqual, connectAdvanced, Provider } from 'react-redux';
8
+ export default FronteggStoreContext;
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { createSelectorHook, createDispatchHook, createStoreHook } from 'react-redux';
3
+ export const FronteggStoreContext = React.createContext(null);
4
+
5
+ if (process.env.NODE_ENV !== 'production') {
6
+ FronteggStoreContext.displayName = 'FronteggStoreContext';
7
+ }
8
+
9
+ export const useSelector = createSelectorHook(FronteggStoreContext);
10
+ export const useDispatch = createDispatchHook(FronteggStoreContext);
11
+ export const useStore = createStoreHook(FronteggStoreContext);
12
+ export { shallowEqual, connectAdvanced, Provider } from 'react-redux';
13
+ export default FronteggStoreContext;
@@ -1,5 +1,5 @@
1
- import { StateHookFunction } from './hooks';
2
- import { AuditLogsActions, AuditLogsState } from '@frontegg/redux-store';
3
- export declare type AuditLogsStateMapper<S extends object> = (state: AuditLogsState) => S;
4
- export declare const useAuditLogsState: StateHookFunction<AuditLogsState>;
5
- export declare const useAuditLogsActions: () => AuditLogsActions;
1
+ import { StateHookFunction } from './hooks';
2
+ import { AuditLogsActions, AuditLogsState } from '@frontegg/redux-store';
3
+ export declare type AuditLogsStateMapper<S extends object> = (state: AuditLogsState) => S;
4
+ export declare const useAuditLogsState: StateHookFunction<AuditLogsState>;
5
+ export declare const useAuditLogsActions: () => AuditLogsActions;
@@ -0,0 +1,4 @@
1
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
2
+ import { auditLogsActions, auditLogsReducers } from '@frontegg/redux-store';
3
+ export const useAuditLogsState = stateMapper => stateHookGenerator(stateMapper, 'auditLogsState');
4
+ export const useAuditLogsActions = () => reducerActionsGenerator(auditLogsActions, auditLogsReducers);
@@ -1,5 +1,5 @@
1
- import { StateHookFunction } from './hooks';
2
- import { AuditsMetadataActions, AuditsMetadataState } from '@frontegg/redux-store';
3
- export declare type AuditsMetadataStateMapper<S extends object> = (state: AuditsMetadataState) => S;
4
- export declare const useAuditsMetadataState: StateHookFunction<AuditsMetadataState>;
5
- export declare const useAuditsMetadataActions: () => AuditsMetadataActions;
1
+ import { StateHookFunction } from './hooks';
2
+ import { AuditsMetadataActions, AuditsMetadataState } from '@frontegg/redux-store';
3
+ export declare type AuditsMetadataStateMapper<S extends object> = (state: AuditsMetadataState) => S;
4
+ export declare const useAuditsMetadataState: StateHookFunction<AuditsMetadataState>;
5
+ export declare const useAuditsMetadataActions: () => AuditsMetadataActions;
@@ -0,0 +1,4 @@
1
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
2
+ import { auditsMetadataActions, auditsMetadataReducers } from '@frontegg/redux-store';
3
+ export const useAuditsMetadataState = stateMapper => stateHookGenerator(stateMapper, 'auditsMetadataState');
4
+ export const useAuditsMetadataActions = () => reducerActionsGenerator(auditsMetadataActions, auditsMetadataReducers);
package/audits/hooks.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { CaseReducerActions, SliceCaseReducers } from '@frontegg/redux-store';
2
- import { AuditsActions, AuditsState } from '@frontegg/redux-store';
3
- export declare type AuthStateMapper<S extends object> = (state: AuditsState) => S;
4
- export declare type StateHookFunction<T> = (() => T) & (<S extends object>(mapper: (state: T) => S) => S);
5
- export declare const useAuditsState: <S extends object>(stateMapper?: AuthStateMapper<S> | undefined) => S;
6
- export declare const useAuditsActions: () => AuditsActions;
7
- /**
8
- * hooks helpers
9
- */
10
- export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
11
- export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuditsState) => any;
12
- export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
1
+ import { CaseReducerActions, SliceCaseReducers } from '@frontegg/redux-store';
2
+ import { AuditsActions, AuditsState } from '@frontegg/redux-store';
3
+ export declare type AuthStateMapper<S extends object> = (state: AuditsState) => S;
4
+ export declare type StateHookFunction<T> = (() => T) & (<S extends object>(mapper: (state: T) => S) => S);
5
+ export declare const useAuditsState: <S extends object>(stateMapper?: AuthStateMapper<S> | undefined) => S;
6
+ export declare const useAuditsActions: () => AuditsActions;
7
+ /**
8
+ * hooks helpers
9
+ */
10
+ export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
11
+ export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuditsState) => any;
12
+ export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
@@ -0,0 +1,35 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import { useMemo } from 'react';
3
+ import { bindActionCreators } from '@frontegg/redux-store';
4
+ import { shallowEqual } from 'react-redux';
5
+ import { useSelector, useDispatch } from '../FronteggStoreContext';
6
+ import { auditsActions, auditsStoreName } from '@frontegg/redux-store';
7
+ export const useAuditsState = stateMapper => {
8
+ return useSelector(state => {
9
+ var _stateMapper;
10
+
11
+ return (_stateMapper = stateMapper == null ? void 0 : stateMapper(state[auditsStoreName])) != null ? _stateMapper : state[auditsStoreName];
12
+ }, shallowEqual);
13
+ };
14
+ export const useAuditsActions = () => {
15
+ const dispatch = useDispatch();
16
+ return useMemo(() => bindActionCreators(auditsActions, dispatch), [auditsActions]);
17
+ };
18
+ export const sliceReducerActionsBy = reducer => {
19
+ const reducerKeys = Object.keys(reducer);
20
+ const reducerActions = reducerKeys.map(key => ({
21
+ [key]: auditsActions[key]
22
+ }));
23
+ return reducerActions.reduce((p, n) => _extends({}, p, n), {});
24
+ };
25
+ export const stateHookGenerator = (stateMapper, subState) => {
26
+ return useSelector(state => {
27
+ var _stateMapper2;
28
+
29
+ return (_stateMapper2 = stateMapper == null ? void 0 : stateMapper(state[auditsStoreName][subState])) != null ? _stateMapper2 : state[auditsStoreName][subState];
30
+ }, shallowEqual);
31
+ };
32
+ export const reducerActionsGenerator = (actions, reducers) => {
33
+ const dispatch = useDispatch();
34
+ return useMemo(() => bindActionCreators(_extends({}, actions, sliceReducerActionsBy(reducers)), dispatch), [dispatch]);
35
+ };
package/audits/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { useAuditsState, useAuditsActions } from './hooks';
2
- export * from './auditLogs';
3
- export * from './auditsMetadata';
1
+ export { useAuditsState, useAuditsActions } from './hooks';
2
+ export * from './auditLogs';
3
+ export * from './auditsMetadata';
package/audits/index.js CHANGED
@@ -1,5 +1,3 @@
1
- export { c as useAuditLogsActions, b as useAuditLogsState, a as useAuditsActions, e as useAuditsMetadataActions, d as useAuditsMetadataState, u as useAuditsState } from '../auditsMetadata-9ab88593.js';
2
- import '@frontegg/redux-store';
3
- import 'react';
4
- import 'react-redux';
5
- import '../FronteggStoreContext-d6c3b684.js';
1
+ export { useAuditsState, useAuditsActions } from './hooks';
2
+ export * from './auditLogs';
3
+ export * from './auditsMetadata';
@@ -1,5 +1,5 @@
1
- import { AcceptInvitationState, AcceptInvitationActions } from '@frontegg/redux-store';
2
- import { StateHookFunction } from './hooks';
3
- export declare type AcceptInvitationStateMapper<S extends object> = (state: AcceptInvitationState) => S;
4
- export declare const useAcceptInvitationState: StateHookFunction<AcceptInvitationState>;
5
- export declare const useAcceptInvitationActions: () => AcceptInvitationActions;
1
+ import { AcceptInvitationState, AcceptInvitationActions } from '@frontegg/redux-store';
2
+ import { StateHookFunction } from './hooks';
3
+ export declare type AcceptInvitationStateMapper<S extends object> = (state: AcceptInvitationState) => S;
4
+ export declare const useAcceptInvitationState: StateHookFunction<AcceptInvitationState>;
5
+ export declare const useAcceptInvitationActions: () => AcceptInvitationActions;
@@ -0,0 +1,4 @@
1
+ import { acceptInvitationReducers, acceptInvitationActions } from '@frontegg/redux-store';
2
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
+ export const useAcceptInvitationState = stateMapper => stateHookGenerator(stateMapper, 'acceptInvitationState');
4
+ export const useAcceptInvitationActions = () => reducerActionsGenerator(acceptInvitationActions, acceptInvitationReducers);
@@ -1,5 +1,5 @@
1
- import { ActivateAccountState, ActivateAccountActions } from '@frontegg/redux-store';
2
- import { StateHookFunction } from './hooks';
3
- export declare type ActivateAccountStateMapper<S extends object> = (state: ActivateAccountState) => S;
4
- export declare const useActivateAccountState: StateHookFunction<ActivateAccountState>;
5
- export declare const useActivateAccountActions: () => ActivateAccountActions;
1
+ import { ActivateAccountState, ActivateAccountActions } from '@frontegg/redux-store';
2
+ import { StateHookFunction } from './hooks';
3
+ export declare type ActivateAccountStateMapper<S extends object> = (state: ActivateAccountState) => S;
4
+ export declare const useActivateAccountState: StateHookFunction<ActivateAccountState>;
5
+ export declare const useActivateAccountActions: () => ActivateAccountActions;
@@ -0,0 +1,4 @@
1
+ import { activateAccountReducers, activateAccountActions } from '@frontegg/redux-store';
2
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
+ export const useActivateAccountState = stateMapper => stateHookGenerator(stateMapper, 'activateState');
4
+ export const useActivateAccountActions = () => reducerActionsGenerator(activateAccountActions, activateAccountReducers);
@@ -1,5 +1,5 @@
1
- import { ApiTokensActions, ApiTokensState } from '@frontegg/redux-store';
2
- import { StateHookFunction } from './hooks';
3
- export declare type ApiTokensStateMapper<S extends object> = (state: ApiTokensState) => S;
4
- export declare const useApiTokensState: StateHookFunction<ApiTokensState>;
5
- export declare const useApiTokensActions: () => ApiTokensActions;
1
+ import { ApiTokensActions, ApiTokensState } from '@frontegg/redux-store';
2
+ import { StateHookFunction } from './hooks';
3
+ export declare type ApiTokensStateMapper<S extends object> = (state: ApiTokensState) => S;
4
+ export declare const useApiTokensState: StateHookFunction<ApiTokensState>;
5
+ export declare const useApiTokensActions: () => ApiTokensActions;
@@ -0,0 +1,4 @@
1
+ import { apiTokensActions, apiTokensReducers } from '@frontegg/redux-store';
2
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
+ export const useApiTokensState = stateMapper => stateHookGenerator(stateMapper, 'apiTokensState');
4
+ export const useApiTokensActions = () => reducerActionsGenerator(apiTokensActions, apiTokensReducers);
@@ -1,5 +1,5 @@
1
- import { ForgotPasswordState, ForgotPasswordActions } from '@frontegg/redux-store';
2
- import { StateHookFunction } from './hooks';
3
- export declare type ForgotPasswordStateMapper<S extends object> = (state: ForgotPasswordState) => S;
4
- export declare const useForgotPasswordState: StateHookFunction<ForgotPasswordState>;
5
- export declare const useForgotPasswordActions: () => ForgotPasswordActions;
1
+ import { ForgotPasswordState, ForgotPasswordActions } from '@frontegg/redux-store';
2
+ import { StateHookFunction } from './hooks';
3
+ export declare type ForgotPasswordStateMapper<S extends object> = (state: ForgotPasswordState) => S;
4
+ export declare const useForgotPasswordState: StateHookFunction<ForgotPasswordState>;
5
+ export declare const useForgotPasswordActions: () => ForgotPasswordActions;
@@ -0,0 +1,4 @@
1
+ import { forgotPasswordReducers, forgotPasswordActions } from '@frontegg/redux-store';
2
+ import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
+ export const useForgotPasswordState = stateMapper => stateHookGenerator(stateMapper, 'forgotPasswordState');
4
+ export const useForgotPasswordActions = () => reducerActionsGenerator(forgotPasswordActions, forgotPasswordReducers);
package/auth/hooks.d.ts CHANGED
@@ -1,62 +1,62 @@
1
- import { CaseReducerActions, SliceCaseReducers } from '@frontegg/redux-store';
2
- import { AuthActions, AuthPageRoutes, AuthState, User } from '@frontegg/redux-store';
3
- import { RedirectOptions } from '@frontegg/rest-api';
4
- export declare type AuthMapper = {
5
- state: (state: AuthState) => any;
6
- actions: (actions: AuthActions) => any;
7
- };
8
- export declare type AuthStateMapper<S extends object> = (state: AuthState) => S;
9
- export declare type AuthActionsMapper<A> = (state: AuthActions) => A;
10
- export declare type StateHookFunction<T> = (() => T) & (<S extends object>(mapper: (state: T) => S) => S);
11
- export declare const useAuth: <S extends object>(stateMapper?: AuthStateMapper<S>) => S;
12
- /**
13
- * ```jsx
14
- * export const MyFunctionComponent = () => {
15
- * const { isAuthenticated, user } = useAuth();
16
- * const loginWithRedirect = useLoginWithRedirect();
17
- *
18
- * if (!isAuthenticated) {
19
- * loginWithRedirect();
20
- * return <></>;
21
- * }
22
- *
23
- * return (<div>Hello User {user.name}</div>);
24
- * }
25
- * ```
26
- *
27
- * use this frontegg hook function to get if user is "Authenticated"
28
- */
29
- export declare const useLoginWithRedirect: () => AuthActions['requestHostedLoginAuthorize'];
30
- export declare const useAuthActions: () => AuthActions;
31
- export declare const useOnRedirectTo: () => (path: string, opts?: RedirectOptions | undefined) => void;
32
- export declare const useAuthRoutes: () => AuthPageRoutes;
33
- /**
34
- * ```jsx
35
- * export const MyFunctionComponent = () => {
36
- * const isAuthenticated = useIsAuthenticated();
37
- * return isAuthenticated ? <div>Hello User</div> : <Redirect to={'/login'}/>
38
- * }
39
- * ```
40
- *
41
- * use this frontegg hook function to get if user is "Authenticated"
42
- */
43
- export declare const useIsAuthenticated: () => boolean;
44
- /**
45
- * ```jsx
46
- * export const MyFunctionComponent = () => {
47
- * const user = useAuthUser();
48
- * return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
49
- * }
50
- * ```
51
- *
52
- * use this frontegg hook function to get the authenticated user
53
- * the return user is null if not authenticated
54
- */
55
- export declare const useAuthUser: () => User;
56
- export declare const useAuthUserOrNull: () => User | null;
57
- /**
58
- * hooks helpers
59
- */
60
- export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
61
- export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuthState) => any;
62
- export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
1
+ import { CaseReducerActions, SliceCaseReducers } from '@frontegg/redux-store';
2
+ import { AuthActions, AuthPageRoutes, AuthState, User } from '@frontegg/redux-store';
3
+ import { RedirectOptions } from '@frontegg/rest-api';
4
+ export declare type AuthMapper = {
5
+ state: (state: AuthState) => any;
6
+ actions: (actions: AuthActions) => any;
7
+ };
8
+ export declare type AuthStateMapper<S extends object> = (state: AuthState) => S;
9
+ export declare type AuthActionsMapper<A> = (state: AuthActions) => A;
10
+ export declare type StateHookFunction<T> = (() => T) & (<S extends object>(mapper: (state: T) => S) => S);
11
+ export declare const useAuth: <S extends object>(stateMapper?: AuthStateMapper<S>) => S;
12
+ /**
13
+ * ```jsx
14
+ * export const MyFunctionComponent = () => {
15
+ * const { isAuthenticated, user } = useAuth();
16
+ * const loginWithRedirect = useLoginWithRedirect();
17
+ *
18
+ * if (!isAuthenticated) {
19
+ * loginWithRedirect();
20
+ * return <></>;
21
+ * }
22
+ *
23
+ * return (<div>Hello User {user.name}</div>);
24
+ * }
25
+ * ```
26
+ *
27
+ * use this frontegg hook function to get if user is "Authenticated"
28
+ */
29
+ export declare const useLoginWithRedirect: () => AuthActions['requestHostedLoginAuthorize'];
30
+ export declare const useAuthActions: () => AuthActions;
31
+ export declare const useOnRedirectTo: () => (path: string, opts?: RedirectOptions | undefined) => void;
32
+ export declare const useAuthRoutes: () => AuthPageRoutes;
33
+ /**
34
+ * ```jsx
35
+ * export const MyFunctionComponent = () => {
36
+ * const isAuthenticated = useIsAuthenticated();
37
+ * return isAuthenticated ? <div>Hello User</div> : <Redirect to={'/login'}/>
38
+ * }
39
+ * ```
40
+ *
41
+ * use this frontegg hook function to get if user is "Authenticated"
42
+ */
43
+ export declare const useIsAuthenticated: () => boolean;
44
+ /**
45
+ * ```jsx
46
+ * export const MyFunctionComponent = () => {
47
+ * const user = useAuthUser();
48
+ * return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
49
+ * }
50
+ * ```
51
+ *
52
+ * use this frontegg hook function to get the authenticated user
53
+ * the return user is null if not authenticated
54
+ */
55
+ export declare const useAuthUser: () => User;
56
+ export declare const useAuthUserOrNull: () => User | null;
57
+ /**
58
+ * hooks helpers
59
+ */
60
+ export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
61
+ export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuthState) => any;
62
+ export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
package/auth/hooks.js ADDED
@@ -0,0 +1,79 @@
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';
7
+ import { ContextHolder } from '@frontegg/rest-api';
8
+ import { ShadowDomContext } from '../common';
9
+ const defaultMapper = {
10
+ state: state => state,
11
+ actions: actions => actions
12
+ };
13
+ export const useAuth = (stateMapper = defaultMapper.state) => {
14
+ return useSelector(state => stateMapper(state[authStoreName]), shallowEqual);
15
+ };
16
+ export const useLoginWithRedirect = () => {
17
+ const dispatch = useDispatch();
18
+ return useMemo(() => bindActionCreators(authActions.requestHostedLoginAuthorize, dispatch), [authActions.requestHostedLoginAuthorize]);
19
+ };
20
+ export const useAuthActions = () => {
21
+ const dispatch = useDispatch();
22
+ return useMemo(() => bindActionCreators(authActions, dispatch), [authActions]);
23
+ };
24
+ export const useOnRedirectTo = () => {
25
+ var _context$onRedirectTo;
26
+
27
+ const context = useContext(ShadowDomContext);
28
+ return (_context$onRedirectTo = context == null ? void 0 : context.onRedirectTo) != null ? _context$onRedirectTo : ContextHolder.onRedirectTo;
29
+ };
30
+ export const useAuthRoutes = () => useAuth(state => _extends({}, state.routes));
31
+ export const useIsAuthenticated = () => useSelector(({
32
+ [authStoreName]: {
33
+ isAuthenticated
34
+ }
35
+ }) => isAuthenticated, shallowEqual);
36
+ export const useAuthUser = () => {
37
+ const routes = useAuthRoutes();
38
+ const onRedirectTo = useOnRedirectTo();
39
+ const user = useAuthUserOrNull();
40
+
41
+ if (user == null) {
42
+ onRedirectTo(routes.loginUrl, {
43
+ refresh: true
44
+ });
45
+ return {};
46
+ }
47
+
48
+ return user;
49
+ };
50
+ export const useAuthUserOrNull = () => {
51
+ const {
52
+ user
53
+ } = useSelector(({
54
+ [authStoreName]: {
55
+ user
56
+ }
57
+ }) => ({
58
+ user
59
+ }), shallowEqual);
60
+ return user || null;
61
+ };
62
+ export const sliceReducerActionsBy = reducer => {
63
+ const reducerKeys = Object.keys(reducer);
64
+ const reducerActions = reducerKeys.map(key => ({
65
+ [key]: authActions[key]
66
+ }));
67
+ return reducerActions.reduce((p, n) => _extends({}, p, n), {});
68
+ };
69
+ export const stateHookGenerator = (stateMapper, subState) => {
70
+ return useSelector(state => {
71
+ var _stateMapper;
72
+
73
+ return (_stateMapper = stateMapper == null ? void 0 : stateMapper(state[authStoreName][subState])) != null ? _stateMapper : state[authStoreName][subState];
74
+ }, shallowEqual);
75
+ };
76
+ export const reducerActionsGenerator = (actions, reducers) => {
77
+ const dispatch = useDispatch();
78
+ return useMemo(() => bindActionCreators(_extends({}, actions, sliceReducerActionsBy(reducers)), dispatch), [dispatch]);
79
+ };
package/auth/index.d.ts CHANGED
@@ -1,16 +1,16 @@
1
- export { useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect } from './hooks';
2
- export * from './acceptInvitation';
3
- export * from './activateAccount';
4
- export * from './apiTokens';
5
- export * from './forgotPassword';
6
- export * from './resetPhoneNumber';
7
- export * from './login';
8
- export * from './mfa';
9
- export * from './profile';
10
- export * from './signup';
11
- export * from './socialLogin';
12
- export * from './team';
13
- export * from './sso';
14
- export * from './securityPolicy';
15
- export * from './tenants';
16
- export * from './roles';
1
+ export { useAuth, useAuthActions, useOnRedirectTo, useAuthRoutes, useIsAuthenticated, useAuthUser, useAuthUserOrNull, useLoginWithRedirect } from './hooks';
2
+ export * from './acceptInvitation';
3
+ export * from './activateAccount';
4
+ export * from './apiTokens';
5
+ export * from './forgotPassword';
6
+ export * from './resetPhoneNumber';
7
+ export * from './login';
8
+ export * from './mfa';
9
+ export * from './profile';
10
+ export * from './signup';
11
+ export * from './socialLogin';
12
+ export * from './team';
13
+ export * from './sso';
14
+ export * from './securityPolicy';
15
+ export * from './tenants';
16
+ export * from './roles';