-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add withAuth higher order component
- Loading branch information
Showing
6 changed files
with
90 additions
and
24 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,26 @@ | ||
# Hooks | ||
|
||
OIDC React comes with a HOC withAuth! withAuth is a great way, much better then using our [Consumer](https://reactjs.org/docs/context.html#contextconsumer). | ||
|
||
## `withAuth` | ||
|
||
withAuth returns a component wrapped in [authContext](../docs/interfaces/authcontextprops.md). Let's look at an example of use. | ||
|
||
```typescript | ||
import { useAuth } from 'oidc-react'; | ||
|
||
class Hello extends React.PureComponent<{}, {}> { | ||
render( = () => { | ||
return ( | ||
<p>Hello {this.props.authData.profile.name}!</p> | ||
); | ||
} | ||
} | ||
|
||
|
||
export default withAuth(Hello); | ||
``` | ||
The example above uses `id_token` object (`profile`) and displays the name of the user. | ||
It should be as easy as that! |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './AuthContext'; | ||
export * from './useAuth'; | ||
export * from './useAuth'; | ||
export * from './withAuth'; |
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,21 @@ | ||
import { AuthContextProps } from './AuthContext'; | ||
import { useAuth } from './useAuth'; | ||
import React from 'react'; | ||
|
||
/** | ||
* A public higher-order component to access the imperative API | ||
*/ | ||
export default function withAuth<P extends AuthContextProps>( | ||
Component: React.ComponentType<P>, | ||
) { | ||
const displayName = `withAuth(${Component.displayName || Component.name})`; | ||
const C: React.FC<Omit<P, keyof AuthContextProps>> = (props) => { | ||
const auth = useAuth(); | ||
|
||
return <Component {...(props as P)} {...auth} />; | ||
}; | ||
|
||
C.displayName = displayName; | ||
|
||
return C; | ||
} |
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