Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
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
The npm package secure-react-keycloak receives a total of 0 weekly downloads. As such, secure-react-keycloak popularity was classified as not popular.
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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.