-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1082 from frontegg/FR-14220-step-up-hoc
FR-14220 - Added SteppedUpContent HOC
- Loading branch information
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { ReactNode, useEffect, useRef } from 'react'; | ||
|
||
import { useStepUp, useIsSteppedUp, useIsAuthenticated } from '@frontegg/react-hooks'; | ||
|
||
export interface SteppedUpProps { | ||
maxAge?: number; | ||
preventSteppingUp?: boolean; | ||
render?: (isSteppedUp: boolean) => React.ReactNode | null; | ||
children?: ReactNode; | ||
} | ||
|
||
/** | ||
* Stepped up content component that shows the wrapped content only when the user is stepped up | ||
* The component triggers the step up flow if the user is not stepped up | ||
* @param props.maxAge maximum time in second that the login is valid | ||
* @param props.preventSteppingUp true when the step up flow should not be triggered automatically when the user is not stepped up, default to false | ||
* @param props.render render function to be called when user is stepped up | ||
* @param props.children to be shown when user is stepped up (only if render not provided) | ||
* | ||
* Pay attention, when shouldTriggerStepUp is true, two instances of SteppedUpContent can be rendered in the same page on the same render cycle. | ||
*/ | ||
export const SteppedUpContent: React.FC<SteppedUpProps> = (props) => { | ||
const isAuthenticated = useIsAuthenticated(); | ||
if (!isAuthenticated) return null; | ||
|
||
return <SteppedUpContentInternal {...props} />; | ||
}; | ||
|
||
const SteppedUpContentInternal: React.FC<SteppedUpProps> = ({ maxAge, preventSteppingUp, render, children }) => { | ||
const isSteppedUp = useIsSteppedUp({ maxAge }); | ||
const stepUp = useStepUp(); | ||
const isStepUpCalled = useRef(false); | ||
|
||
useEffect(() => { | ||
if (isSteppedUp) { | ||
isStepUpCalled.current = false; | ||
return; | ||
} | ||
|
||
if (isStepUpCalled.current) return; | ||
|
||
if (!preventSteppingUp) { | ||
stepUp({ maxAge }); | ||
} | ||
|
||
isStepUpCalled.current = true; | ||
}, [isSteppedUp, maxAge, preventSteppingUp, stepUp]); | ||
|
||
if (typeof render === 'function') { | ||
return <>{render(isSteppedUp)}</>; | ||
} | ||
|
||
return isSteppedUp ? <>{children}</> : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters