-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add withAuthenticationRequired
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 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
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,66 @@ | ||
import React from "react"; | ||
|
||
import { useAuth } from "./useAuth"; | ||
import type { SigninRedirectArgs } from "oidc-client-ts"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface WithAuthenticationRequiredOptions { | ||
/** | ||
* Show a message when redirected to the login page. | ||
*/ | ||
onRedirecting?: () => JSX.Element; | ||
|
||
/** | ||
* Allows executing logic before the user is redirected to the login page. | ||
*/ | ||
onBeforeSignin?: () => Promise<void> | void; | ||
|
||
/** | ||
* Pass additional signin redirect options. | ||
*/ | ||
signinRedirectArgs?: SigninRedirectArgs; | ||
} | ||
|
||
/** | ||
* A public higher-order component to protect accessing not public content. When you wrap your components in this higher-order | ||
* component and an anonymous user visits your component, they will be redirected to the login page; after logging in, they | ||
* will return to the page from which they were redirected. | ||
* | ||
* @public | ||
*/ | ||
const withAuthenticationRequired = <P extends object>( | ||
Component: React.ComponentType<P>, | ||
options: WithAuthenticationRequiredOptions = {}, | ||
): React.FC<P> => { | ||
const { onRedirecting = (): JSX.Element => <></>, onBeforeSignin, signinRedirectArgs } = options; | ||
|
||
const displayName = `withAuthenticationRequired(${Component.displayName || Component.name})`; | ||
const C: React.FC<P> = (props) => { | ||
const auth = useAuth(); | ||
|
||
React.useEffect(() => { | ||
if (auth.isLoading || auth.isAuthenticated) { | ||
return; | ||
} | ||
void (async (): Promise<void> => { | ||
onBeforeSignin && await onBeforeSignin(); | ||
await auth.signinRedirect(signinRedirectArgs); | ||
})(); | ||
}, [ | ||
auth.isLoading, | ||
auth.isAuthenticated, | ||
onBeforeSignin, | ||
signinRedirectArgs, | ||
]); | ||
|
||
return auth.isAuthenticated ? <Component {...props} /> : onRedirecting(); | ||
}; | ||
|
||
C.displayName = displayName; | ||
|
||
return C; | ||
}; | ||
|
||
export default withAuthenticationRequired; |