
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@digital-wafa/react-keycloak
Advanced tools
a keycloak adapter for react with a protected route component that support react-router and reach-router
npm i @digital-wafa/react-keycloak
you should create an instance of keyclaok using the keycloak-js
package or importing the link from your keycloak server directly which is recommanded to avoid compatibily issues.
const keycloakInstance = new Keycloak({
url: "your keycloak url",
realm: "your realm",
clientId: "your client id",
....any other parameters
})
Then wrape your component with the keycloak provider, KeycloakProvider
accept the following props :
Props | Required | Description |
---|---|---|
keycloak | yes | the keycloak instance you created |
loginOptions | no | keycloak login options |
initOptions | no | keycloak init options |
loadingComponent | only if you use the included ProtectedRoute component | the loading component to show when redirecting to keycloak login page |
import { KeycloakProvider } from "@digital-wafa/react-keycloak";
<KeycloakProvider
keycloak={keycloak}
loginOptions={{
idpHint: "ei",
scope: "phone",
}}
>
<App />
</KeycloakProvider>
finaly you can access the keycloak instance using the useKeycloakContext
hook which return the following object:
{
keycloak, // keycloak instance
loginOptions, // keycloak login options
initOptions, // keycloak init options
authenticated // is the user authenticated or not
}
import { useKeycloakContext } from "@digital-wafa/react-keycloak";
const {keycloak, authenticated, loginOptions, initOptions} = useKeycloakContext();
or import the context if you are using a class component
import { keycloakContext } from "@digital-wafa/react-keycloak"
...
class YourClassComponent extends React.Component {
static contextType = keycloakContext;
const {keycloak, authenticated, loginOptions, initOptions} = this.context;
}
NB: the token is refreshed automatically when it expire using the onTokenExpired
event.
A useful component to protect your routes, it's compatible with react-router-dom
and @reach/router
if the user is authenticated, he will see the desired page, if not he will be redirected to the keycloak login page.
react-router-dom
example:
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { ProtectedRoute } from "@digital-wafa/react-keycloak";
<Router>
<ProtectedRoute
RouteComponent={Route}
ComponentToRender={YourPageComponent}
path="/your-path"
/>
<Router>
@reach/router
example:
when using @reach/router
you don't need to use the ComponentToRender
props
import { Router } from "@reach/router";
import { ProtectedRoute } from "@digital-wafa/react-keycloak";
<Router>
<ProtectedRoute
RouteComponent={YourPageComponent}
path="/your-path"
/>
<Router>
Get the tokens which are stored automatically in localStorage
after the login and the refreshToken actions.
To use outside your components
import { getToken, getRefreshToken } from "@digital-wafa/react-keycloak";
const token = getToken();
const refreshToken = getRefreshToken()
[1.1.0] - 2020-09-15
FAQs
a keycloak library for react
The npm package @digital-wafa/react-keycloak receives a total of 27 weekly downloads. As such, @digital-wafa/react-keycloak popularity was classified as not popular.
We found that @digital-wafa/react-keycloak demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.