@frontegg/react-hooks 6.155.0-alpha.0 → 6.155.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.
package/auth/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export * from './apiTokens';
5
5
  export * from './forgotPassword';
6
6
  export * from './resetPhoneNumber';
7
7
  export * from './login';
8
- export * from './stepUp';
8
+ export * from './stepUp/stepUp';
9
9
  export * from './mfa';
10
10
  export * from './profile';
11
11
  export * from './signup';
package/auth/index.js CHANGED
@@ -5,7 +5,7 @@ export * from './apiTokens';
5
5
  export * from './forgotPassword';
6
6
  export * from './resetPhoneNumber';
7
7
  export * from './login';
8
- export * from './stepUp';
8
+ export * from './stepUp/stepUp';
9
9
  export * from './mfa';
10
10
  export * from './profile';
11
11
  export * from './signup';
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The required ACR (Authorization Context Reference) value for the step up flow
3
+ */
4
+ export declare const ACR_VALUE = "http://schemas.openid.net/pape/policies/2007/06/multi-factor";
5
+ /**
6
+ * One of the required AMR (Authentication Methods References) values for the step up flow
7
+ */
8
+ export declare const AMR_MFA_VALUE = "mfa";
9
+ /**
10
+ * One of the required AMR (Authentication Methods References) values should be from the array for the step up flow
11
+ */
12
+ export declare const AMR_ADDITIONAL_VALUE: string[];
13
+ /**
14
+ * The name of the query param that contains the max age of the step up
15
+ */
16
+ export declare const STEP_UP_MAX_AGE_PARAM_NAME = "maxAge";
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The required ACR (Authorization Context Reference) value for the step up flow
3
+ */
4
+ export const ACR_VALUE = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor';
5
+
6
+ /**
7
+ * One of the required AMR (Authentication Methods References) values for the step up flow
8
+ */
9
+ export const AMR_MFA_VALUE = 'mfa';
10
+
11
+ /**
12
+ * One of the required AMR (Authentication Methods References) values should be from the array for the step up flow
13
+ */
14
+ export const AMR_ADDITIONAL_VALUE = ['otp', 'sms', 'hwk'];
15
+
16
+ /**
17
+ * The name of the query param that contains the max age of the step up
18
+ */
19
+ export const STEP_UP_MAX_AGE_PARAM_NAME = 'maxAge';
@@ -0,0 +1,8 @@
1
+ import { StepUpState } from '@frontegg/redux-store';
2
+ export declare type StepUpStateMapper<S> = (state: StepUpState) => S;
3
+ /**
4
+ * Step up options (for stepUp function)
5
+ */
6
+ export interface StepUpOptions {
7
+ maxAge?: number;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { StepUpState, StepUpActions } from '@frontegg/redux-store';
2
+ import { RedirectOptions } from '@frontegg/rest-api';
3
+ import { StepUpStateMapper } from './interfaces';
4
+ export declare function useStepUpState(): StepUpState;
5
+ export declare function useStepUpState<S>(stateMapper: StepUpStateMapper<S>): S;
6
+ export declare const useStepUpActions: () => StepUpActions;
7
+ /**
8
+ * Redirects to the step up url with the max age param and set the redirect url in the local storage
9
+ * The redirect url will be used after the step up flow is done
10
+ * @param stepUpUrl - step up url to redirect to
11
+ * @param onRedirectTo - redirect to function
12
+ * @param maxAge - max age of step up
13
+ */
14
+ export declare const redirectByStepUpUrl: (stepUpUrl: string, onRedirectTo: (path: string, opts?: RedirectOptions | undefined) => void, maxAge?: number | undefined) => void;
15
+ /**
16
+ * @returns max age from the query param as a number or null if not exists
17
+ */
18
+ export declare const getMaxAgeFromQueryParam: () => number | undefined;
19
+ /**
20
+ * @returns step up function that redirects to the step up url with the max age param and set the redirect url in the local storage
21
+ */
22
+ export declare const useStepUp: () => any;
23
+ /**
24
+ * @param options.maxAge - max age of step up
25
+ * @returns true when the user is stepped up, false otherwise
26
+ */
27
+ export declare const useIsSteppedUp: ({ maxAge }?: {
28
+ maxAge?: number | undefined;
29
+ }) => boolean;
@@ -0,0 +1,69 @@
1
+ import { FRONTEGG_AFTER_AUTH_REDIRECT_URL, stepUpReducers, stepUpActions, getSearchParam } from '@frontegg/redux-store';
2
+ import { reducerActionsGenerator, stateHookGenerator, useAuthRoutes, useAuthUser, useOnRedirectTo } from '../hooks';
3
+ import { useCallback } from 'react';
4
+ import { ACR_VALUE, AMR_MFA_VALUE, AMR_ADDITIONAL_VALUE, STEP_UP_MAX_AGE_PARAM_NAME } from './consts';
5
+ const defaultMapper = state => state;
6
+ export function useStepUpState(stateMapper = defaultMapper) {
7
+ return stateHookGenerator(stateMapper, 'stepUpState');
8
+ }
9
+ export const useStepUpActions = () => reducerActionsGenerator(stepUpActions, stepUpReducers);
10
+
11
+ /**
12
+ * Redirects to the step up url with the max age param and set the redirect url in the local storage
13
+ * The redirect url will be used after the step up flow is done
14
+ * @param stepUpUrl - step up url to redirect to
15
+ * @param onRedirectTo - redirect to function
16
+ * @param maxAge - max age of step up
17
+ */
18
+ export const redirectByStepUpUrl = (stepUpUrl, onRedirectTo, maxAge) => {
19
+ const encodedRedirectUrl = window.location.pathname + window.location.search;
20
+ const maxAgePart = maxAge !== undefined ? `?${STEP_UP_MAX_AGE_PARAM_NAME}=${maxAge}` : '';
21
+ window.localStorage.setItem(FRONTEGG_AFTER_AUTH_REDIRECT_URL, encodedRedirectUrl);
22
+ onRedirectTo(`${stepUpUrl}${maxAgePart}`, {
23
+ refresh: false
24
+ });
25
+ };
26
+
27
+ /**
28
+ * @returns max age from the query param as a number or null if not exists
29
+ */
30
+ export const getMaxAgeFromQueryParam = () => {
31
+ const str = getSearchParam(STEP_UP_MAX_AGE_PARAM_NAME);
32
+ return str === undefined ? undefined : +str;
33
+ };
34
+
35
+ /**
36
+ * @returns step up function that redirects to the step up url with the max age param and set the redirect url in the local storage
37
+ */
38
+ export const useStepUp = () => {
39
+ const {
40
+ stepUpUrl
41
+ } = useAuthRoutes();
42
+ const onRedirectTo = useOnRedirectTo();
43
+ return useCallback(options => {
44
+ redirectByStepUpUrl(stepUpUrl, onRedirectTo, options == null ? void 0 : options.maxAge);
45
+ }, [stepUpUrl, onRedirectTo]);
46
+ };
47
+
48
+ /**
49
+ * @param options.maxAge - max age of step up
50
+ * @returns true when the user is stepped up, false otherwise
51
+ */
52
+ export const useIsSteppedUp = ({
53
+ maxAge
54
+ } = {}) => {
55
+ const {
56
+ amr = [],
57
+ acr = '',
58
+ auth_time
59
+ } = useAuthUser();
60
+ if (maxAge && auth_time) {
61
+ // when user is logged in for a long time (more than maxAge, but jwt is still valid because it's not refreshed yet)
62
+ const isMaxAgeValid = Date.now() / 1000 - auth_time <= maxAge;
63
+ if (!isMaxAgeValid) return false;
64
+ }
65
+ const isACRValid = acr === ACR_VALUE;
66
+ const isAMRIncludesMFA = amr.indexOf(AMR_MFA_VALUE) !== -1;
67
+ const isAMRIncludesMethod = AMR_ADDITIONAL_VALUE.find(method => amr.indexOf(method)) !== undefined;
68
+ return isACRValid && isAMRIncludesMFA && isAMRIncludesMethod;
69
+ };
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Frontegg v6.155.0-alpha.0
1
+ /** @license Frontegg v6.155.0-alpha.2
2
2
  *
3
3
  * This source code is licensed under the MIT license found in the
4
4
  * LICENSE file in the root directory of this source tree.
@@ -134,7 +134,7 @@ Object.keys(_login).forEach(function (key) {
134
134
  }
135
135
  });
136
136
  });
137
- var _stepUp = require("./stepUp");
137
+ var _stepUp = require("./stepUp/stepUp");
138
138
  Object.keys(_stepUp).forEach(function (key) {
139
139
  if (key === "default" || key === "__esModule") return;
140
140
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.STEP_UP_MAX_AGE_PARAM_NAME = exports.AMR_MFA_VALUE = exports.AMR_ADDITIONAL_VALUE = exports.ACR_VALUE = void 0;
7
+ /**
8
+ * The required ACR (Authorization Context Reference) value for the step up flow
9
+ */
10
+ const ACR_VALUE = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor';
11
+
12
+ /**
13
+ * One of the required AMR (Authentication Methods References) values for the step up flow
14
+ */
15
+ exports.ACR_VALUE = ACR_VALUE;
16
+ const AMR_MFA_VALUE = 'mfa';
17
+
18
+ /**
19
+ * One of the required AMR (Authentication Methods References) values should be from the array for the step up flow
20
+ */
21
+ exports.AMR_MFA_VALUE = AMR_MFA_VALUE;
22
+ const AMR_ADDITIONAL_VALUE = ['otp', 'sms', 'hwk'];
23
+
24
+ /**
25
+ * The name of the query param that contains the max age of the step up
26
+ */
27
+ exports.AMR_ADDITIONAL_VALUE = AMR_ADDITIONAL_VALUE;
28
+ const STEP_UP_MAX_AGE_PARAM_NAME = 'maxAge';
29
+ exports.STEP_UP_MAX_AGE_PARAM_NAME = STEP_UP_MAX_AGE_PARAM_NAME;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useStepUpActions = exports.useStepUp = exports.useIsSteppedUp = exports.redirectByStepUpUrl = exports.getMaxAgeFromQueryParam = void 0;
7
+ exports.useStepUpState = useStepUpState;
8
+ var _reduxStore = require("@frontegg/redux-store");
9
+ var _hooks = require("../hooks");
10
+ var _react = require("react");
11
+ var _consts = require("./consts");
12
+ const defaultMapper = state => state;
13
+ function useStepUpState(stateMapper = defaultMapper) {
14
+ return (0, _hooks.stateHookGenerator)(stateMapper, 'stepUpState');
15
+ }
16
+ const useStepUpActions = () => (0, _hooks.reducerActionsGenerator)(_reduxStore.stepUpActions, _reduxStore.stepUpReducers);
17
+
18
+ /**
19
+ * Redirects to the step up url with the max age param and set the redirect url in the local storage
20
+ * The redirect url will be used after the step up flow is done
21
+ * @param stepUpUrl - step up url to redirect to
22
+ * @param onRedirectTo - redirect to function
23
+ * @param maxAge - max age of step up
24
+ */
25
+ exports.useStepUpActions = useStepUpActions;
26
+ const redirectByStepUpUrl = (stepUpUrl, onRedirectTo, maxAge) => {
27
+ const encodedRedirectUrl = window.location.pathname + window.location.search;
28
+ const maxAgePart = maxAge !== undefined ? `?${_consts.STEP_UP_MAX_AGE_PARAM_NAME}=${maxAge}` : '';
29
+ window.localStorage.setItem(_reduxStore.FRONTEGG_AFTER_AUTH_REDIRECT_URL, encodedRedirectUrl);
30
+ onRedirectTo(`${stepUpUrl}${maxAgePart}`, {
31
+ refresh: false
32
+ });
33
+ };
34
+
35
+ /**
36
+ * @returns max age from the query param as a number or null if not exists
37
+ */
38
+ exports.redirectByStepUpUrl = redirectByStepUpUrl;
39
+ const getMaxAgeFromQueryParam = () => {
40
+ const str = (0, _reduxStore.getSearchParam)(_consts.STEP_UP_MAX_AGE_PARAM_NAME);
41
+ return str === undefined ? undefined : +str;
42
+ };
43
+
44
+ /**
45
+ * @returns step up function that redirects to the step up url with the max age param and set the redirect url in the local storage
46
+ */
47
+ exports.getMaxAgeFromQueryParam = getMaxAgeFromQueryParam;
48
+ const useStepUp = () => {
49
+ const {
50
+ stepUpUrl
51
+ } = (0, _hooks.useAuthRoutes)();
52
+ const onRedirectTo = (0, _hooks.useOnRedirectTo)();
53
+ return (0, _react.useCallback)(options => {
54
+ redirectByStepUpUrl(stepUpUrl, onRedirectTo, options == null ? void 0 : options.maxAge);
55
+ }, [stepUpUrl, onRedirectTo]);
56
+ };
57
+
58
+ /**
59
+ * @param options.maxAge - max age of step up
60
+ * @returns true when the user is stepped up, false otherwise
61
+ */
62
+ exports.useStepUp = useStepUp;
63
+ const useIsSteppedUp = ({
64
+ maxAge
65
+ } = {}) => {
66
+ const {
67
+ amr = [],
68
+ acr = '',
69
+ auth_time
70
+ } = (0, _hooks.useAuthUser)();
71
+ if (maxAge && auth_time) {
72
+ // when user is logged in for a long time (more than maxAge, but jwt is still valid because it's not refreshed yet)
73
+ const isMaxAgeValid = Date.now() / 1000 - auth_time <= maxAge;
74
+ if (!isMaxAgeValid) return false;
75
+ }
76
+ const isACRValid = acr === _consts.ACR_VALUE;
77
+ const isAMRIncludesMFA = amr.indexOf(_consts.AMR_MFA_VALUE) !== -1;
78
+ const isAMRIncludesMethod = _consts.AMR_ADDITIONAL_VALUE.find(method => amr.indexOf(method)) !== undefined;
79
+ return isACRValid && isAMRIncludesMFA && isAMRIncludesMethod;
80
+ };
81
+ exports.useIsSteppedUp = useIsSteppedUp;
package/node/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Frontegg v6.155.0-alpha.0
1
+ /** @license Frontegg v6.155.0-alpha.2
2
2
  *
3
3
  * This source code is licensed under the MIT license found in the
4
4
  * LICENSE file in the root directory of this source tree.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@frontegg/react-hooks",
3
- "version": "6.155.0-alpha.0",
3
+ "version": "6.155.0-alpha.2",
4
4
  "main": "./node/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Frontegg LTD",
7
7
  "dependencies": {
8
8
  "@babel/runtime": "^7.18.6",
9
- "@frontegg/redux-store": "6.155.0-alpha.0",
10
- "@frontegg/types": "6.155.0-alpha.0",
9
+ "@frontegg/redux-store": "6.155.0-alpha.2",
10
+ "@frontegg/types": "6.155.0-alpha.2",
11
11
  "@types/react": "*",
12
12
  "get-value": "^3.0.1",
13
13
  "react-redux": "^7.x"
package/auth/stepUp.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { StepUpState, StepUpActions } from '@frontegg/redux-store';
2
- import { RedirectOptions } from '@frontegg/rest-api';
3
- export declare type StepUpStateMapper<S> = (state: StepUpState) => S;
4
- export declare function useStepUpState(): StepUpState;
5
- export declare function useStepUpState<S>(stateMapper: StepUpStateMapper<S>): S;
6
- export declare const useStepUpActions: () => StepUpActions;
7
- export declare const redirectByStepUpUrl: (stepUpUrl: string, onRedirectTo: (path: string, opts?: RedirectOptions | undefined) => void, maxAge?: number | undefined) => void;
8
- export declare const getMaxAgeFromQueryParam: () => number | null;
9
- export declare const useStepUp: () => any;
10
- export declare const useIsSteppedUp: () => boolean;
package/auth/stepUp.js DELETED
@@ -1,55 +0,0 @@
1
- import { STEP_UP_REDIRECT_URL_QUERY_PARAM, stepUpReducers, stepUpActions, getQueryParam } from '@frontegg/redux-store';
2
- import { reducerActionsGenerator, stateHookGenerator, useAuth, useAuthRoutes, useOnRedirectTo } from './hooks';
3
- import { useCallback } from 'react';
4
- const defaultMapper = state => state;
5
- const STEP_UP_MAX_AGE_PARAM_NAME = 'maxAge';
6
- export function useStepUpState(stateMapper = defaultMapper) {
7
- return stateHookGenerator(stateMapper, 'stepUpState');
8
- }
9
- export const useStepUpActions = () => reducerActionsGenerator(stepUpActions, stepUpReducers);
10
- export const redirectByStepUpUrl = (stepUpUrl, onRedirectTo, maxAge) => {
11
- const encodedRedirectUrl = encodeURIComponent(window.location.pathname + window.location.search);
12
- onRedirectTo(`${stepUpUrl}?${STEP_UP_REDIRECT_URL_QUERY_PARAM}=${encodedRedirectUrl}&${STEP_UP_MAX_AGE_PARAM_NAME}=${maxAge}`, {
13
- refresh: false
14
- });
15
- };
16
- export const getMaxAgeFromQueryParam = () => {
17
- const str = getQueryParam(STEP_UP_MAX_AGE_PARAM_NAME);
18
- return str === null ? null : +str;
19
- };
20
- export const useStepUp = () => {
21
- // also custom url?
22
- const {
23
- stepUpUrl
24
- } = useAuthRoutes();
25
- const onRedirectTo = useOnRedirectTo();
26
-
27
- // SHOULD CHECK IF USER IS AUTHENTICATED? If not what to do?
28
- // WHAT TO DO IF USER IS NOT AUTHENTICATED AND ENTERED DIRECTLY TO THE ROUTE OF STEP UP?
29
-
30
- return useCallback(options => {
31
- // SSR handling?
32
-
33
- // A hack because of double calls to useEffect.
34
- // It keeps us also when there is a usage of multiple HOCs on the same page -> multiple calls to stepUp
35
- // TODO no window for SSR - what to do?
36
- if (window.location.search.indexOf(STEP_UP_REDIRECT_URL_QUERY_PARAM) !== -1) return;
37
- redirectByStepUpUrl(stepUpUrl, onRedirectTo, options == null ? void 0 : options.maxAge);
38
- }, [stepUpUrl, onRedirectTo]);
39
- };
40
- const ACR_VALUE = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor';
41
- const AMR_MFA_VALUE = 'mfa';
42
- const AMR_ADDITIONAL_VALUE = ['otp', 'sms', 'hwk'];
43
- export const useIsSteppedUp = () => {
44
- // only if don't have amr acr ?? max age???
45
- const {
46
- amr = [],
47
- acr = ''
48
- } = useAuth(({
49
- user
50
- }) => user || {});
51
- const isACRValid = acr === ACR_VALUE;
52
- const isAMRIncludesMFA = amr.indexOf(AMR_MFA_VALUE) !== -1;
53
- const isAMRIncludesMethod = AMR_ADDITIONAL_VALUE.find(method => amr.indexOf(method)) !== undefined;
54
- return isACRValid && isAMRIncludesMFA && isAMRIncludesMethod;
55
- };
@@ -1,67 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useStepUpActions = exports.useStepUp = exports.useIsSteppedUp = exports.redirectByStepUpUrl = exports.getMaxAgeFromQueryParam = void 0;
7
- exports.useStepUpState = useStepUpState;
8
- var _reduxStore = require("@frontegg/redux-store");
9
- var _hooks = require("./hooks");
10
- var _react = require("react");
11
- const defaultMapper = state => state;
12
- const STEP_UP_MAX_AGE_PARAM_NAME = 'maxAge';
13
- function useStepUpState(stateMapper = defaultMapper) {
14
- return (0, _hooks.stateHookGenerator)(stateMapper, 'stepUpState');
15
- }
16
- const useStepUpActions = () => (0, _hooks.reducerActionsGenerator)(_reduxStore.stepUpActions, _reduxStore.stepUpReducers);
17
- exports.useStepUpActions = useStepUpActions;
18
- const redirectByStepUpUrl = (stepUpUrl, onRedirectTo, maxAge) => {
19
- const encodedRedirectUrl = encodeURIComponent(window.location.pathname + window.location.search);
20
- onRedirectTo(`${stepUpUrl}?${_reduxStore.STEP_UP_REDIRECT_URL_QUERY_PARAM}=${encodedRedirectUrl}&${STEP_UP_MAX_AGE_PARAM_NAME}=${maxAge}`, {
21
- refresh: false
22
- });
23
- };
24
- exports.redirectByStepUpUrl = redirectByStepUpUrl;
25
- const getMaxAgeFromQueryParam = () => {
26
- const str = (0, _reduxStore.getQueryParam)(STEP_UP_MAX_AGE_PARAM_NAME);
27
- return str === null ? null : +str;
28
- };
29
- exports.getMaxAgeFromQueryParam = getMaxAgeFromQueryParam;
30
- const useStepUp = () => {
31
- // also custom url?
32
- const {
33
- stepUpUrl
34
- } = (0, _hooks.useAuthRoutes)();
35
- const onRedirectTo = (0, _hooks.useOnRedirectTo)();
36
-
37
- // SHOULD CHECK IF USER IS AUTHENTICATED? If not what to do?
38
- // WHAT TO DO IF USER IS NOT AUTHENTICATED AND ENTERED DIRECTLY TO THE ROUTE OF STEP UP?
39
-
40
- return (0, _react.useCallback)(options => {
41
- // SSR handling?
42
-
43
- // A hack because of double calls to useEffect.
44
- // It keeps us also when there is a usage of multiple HOCs on the same page -> multiple calls to stepUp
45
- // TODO no window for SSR - what to do?
46
- if (window.location.search.indexOf(_reduxStore.STEP_UP_REDIRECT_URL_QUERY_PARAM) !== -1) return;
47
- redirectByStepUpUrl(stepUpUrl, onRedirectTo, options == null ? void 0 : options.maxAge);
48
- }, [stepUpUrl, onRedirectTo]);
49
- };
50
- exports.useStepUp = useStepUp;
51
- const ACR_VALUE = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor';
52
- const AMR_MFA_VALUE = 'mfa';
53
- const AMR_ADDITIONAL_VALUE = ['otp', 'sms', 'hwk'];
54
- const useIsSteppedUp = () => {
55
- // only if don't have amr acr ?? max age???
56
- const {
57
- amr = [],
58
- acr = ''
59
- } = (0, _hooks.useAuth)(({
60
- user
61
- }) => user || {});
62
- const isACRValid = acr === ACR_VALUE;
63
- const isAMRIncludesMFA = amr.indexOf(AMR_MFA_VALUE) !== -1;
64
- const isAMRIncludesMethod = AMR_ADDITIONAL_VALUE.find(method => amr.indexOf(method)) !== undefined;
65
- return isACRValid && isAMRIncludesMFA && isAMRIncludesMethod;
66
- };
67
- exports.useIsSteppedUp = useIsSteppedUp;