Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
secure-react-keycloak
Advanced tools
Secure React Routes & Component with Keycloak This is a library which provides utilities to restrict React routes & component/function This library uses React Keycloak library as a based library. If you feel lazy to write your own utilities, this is the library you looking for.
npm install --save secure-react-keycloak
Create a keycloak.js
file in the src folder of your project with the following content to set up a Keycloak instance as needed.
import Keycloak from 'keycloak-js'
const keycloakConfig = {
url: 'http://localhost:8080/auth',
realm: 'Demo',
clientId: 'react-app'
}
const keycloak = new Keycloak(keycloakConfig);
export default keycloak
KeycloakProvider
and pass the keycloak
instance as propimport React from 'react';
import { KeycloakProvider } from 'secure-react-keycloak';
import keycloak from './keycloak'
// Wrap everything inside KeycloakProvider
const App = () => {
return (
<KeycloakProvider keycloak={keycloak}>
<div className="App"> ... </div>
</KeycloakProvider>
)
}
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import Menu from '../components/Menu';
import HomePage from '../pages/HomePage';
import ProtectedPage from '../pages/ProtectedPage';
import { PrivateRoute, useKeycloak } from 'secure-react-keycloak';
export const AppRouter = () => {
const [, initialized] = useKeycloak();
if (!initialized) {
return <h3>Loading ... !!!</h3>;
}
return (<>
<Menu />
<BrowserRouter>
<Switch>
<Route exact path="/" component={HomePage} />
<PrivateRoute roles={['RealmAdmin']} path="/protected" component={ProtectedPage} />
</Switch>
</BrowserRouter>
</>
);
};
import React from 'react';
import { useKeycloak, AuthorizedElement } from 'secure-react-keycloak'
const HomePage = () => {
const [keycloak, initialized] = useKeycloak();
return (<div>
<h1>Home Page</h1>
<strong>Anyone can access this page</strong>
<h2>Only Realm Admin Can Access below Button</h2>
<AuthorizedElement roles={['RealmAdmin']}><h1>Realm Admin </h1></AuthorizedElement>
<h2>Only Client Admin Can Access below Button</h2>
<AuthorizedElement roles={['ClientAdmin']}><h1>Client Admin</h1></AuthorizedElement>
{initialized ? keycloak.authenticated && <pre>{JSON.stringify(keycloak, undefined, 2)}</pre> :
<h2>keycloak initializing ....!!!!</h2>}
</div>)
}
export default HomePage
import AuthorizedFunction from 'secure-react-keycloak';
export type currentUserRole = any[] | undefined;
export default function MyButton({ roles, children }: any) {
return AuthorizedFunction(roles) && <Button>MyButton</Button>;
}
MIT © cagline
FAQs
Secure React Routes & Component with Keycloak
We found that secure-react-keycloak demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.