keycloak-provider

1.0.120 • Public • Published

Logo

Keylcoak Provider


React binding for Keycloak

Table of Contents


  • Install
  • Getting Started
    • Setup Keycloak Configuration
    • Setup KeycloakAuthProvider
    • Context Usage
    • AccessControll Function Usage

Install


React keycloak-provider requires:

  • keycloak-provider @latest
npm install keycloak-provider

Getting Started


Setup Keycloak Configuration

Create a any const veriable like const KeycloakConfig in the App component

 const keycloakConfig = {
    url: "",
    realm: "",
    clientId: "",
  };

Setup KeycloakAuthProvider

Created const KeycloakConfig veriable we will used in KeycloakAuthProvider in the App component

import { KeycloakAuthProvider } from "keycloak-provider"

// Wrap everything inside KeycloakAuthProvider
const App = () => {
  return <ReactKeycloakProvider keycloakConfig={keycloakConfig}>...</ReactKeycloakProvider>
}
  1. If your using other providers (such as react-redux) it is recommended to place them inside KeycloakAuthProvider.
  2. If your using <React.StrictMode></React.StrictMode> please make sure that you place them inside KeycloakAuthProvider otherwise it will cause infinite call to keycloak token .

KeycloakAuthProvider automatically invokes keycloak.init() method .

Context Usage

Import context from import { KeycloakContext } from "keycloak-provider"

When a component requires access to Keycloak, you can use the KeycloakContext and you can get all function provided by keycloak object using this like (keycloak.logout,kycloak.token).

  • This context provide you two veriables
    • keycloak
    • authenticated
import { useContext } from "react";
import { KeycloakContext } from "keycloak-provider"

export default () => {
  // Using Object destructuring
  const {keycloak,authenticated} = useContext(KeycloakContext);

  return (
    <div>
      <div>{`User is ${
        !keycloak.authenticated ? 'NOT ' : ''
      }authenticated`}</div>

      {!!keycloak.authenticated && (
        <button type="button" onClick={() => keycloak.logout()}>
          Logout
        </button>
      )}
    </div>
  )
}

AccessControll Function Usage

import accessControll function from import { accessControll } from "keycloak-provider"

when you required to specify access from the scope provided by backend API's then you can do this by using this function to controll the access to ui components.

//this function will check that User Resource has read access to that role
  using token and return true and false value accordingly
  const Access = accessControll("User", "read", keyclaokToken)

After you can set it in useState hook or any veriable and add condition accordingly in your UI.

ex.

import { accessControll } from "keycloak-provider"


export default () => {

  const [buttonAccess,setButtonAccess] = useState(false);
  new Promise((resolve, reject) => {
     resolve(accessControll("LogoutButton", "read", keycloak?.token));
    }).then((response)=>{
     setButtonAccess(response)
  })

  return (
    <div>
      <div>{`User is ${
        !keycloak.authenticated ? 'NOT ' : ''
      }authenticated`}</div>

      {!!keycloak.authenticated && (
        {buttonAccess && <button type="button" onClick={() => keycloak.logout()}>
          Logout
        </button>}
      )}
    </div>
  )
}

Copyright (c) 2022. GESSA (www.gessa.io)

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.1204latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.1204
1.0.1190
1.0.1180
1.0.1170
1.0.1160
1.0.1150
1.0.1140
1.0.1130
1.0.1120
1.0.1110
1.0.1100
1.0.1090
1.0.1080
1.0.1070
1.0.1060
1.0.1050
1.0.1040
1.0.1030
1.0.1020
1.0.1010
1.0.1000
1.0.990
1.0.980
1.0.970
1.0.960
1.0.950
1.0.940
1.0.930
1.0.920
1.0.910
1.0.900
1.0.890
1.0.880
1.0.870
1.0.850
1.0.840
1.0.830
1.0.820
1.0.810
1.0.800
1.0.790
1.0.780
1.0.770
1.0.760
1.0.750
1.0.740
1.0.730
1.0.720
1.0.710
1.0.700
1.0.690
1.0.680
1.0.670
1.0.660
1.0.650
1.0.640
1.0.630
1.0.620
1.0.610
1.0.600
1.0.590
1.0.580
1.0.570
1.0.560
1.0.550
1.0.540
1.0.520
1.0.510
1.0.500
1.0.490
1.0.480
1.0.470
1.0.460
1.0.450
1.0.440
1.0.430
1.0.420
1.0.410
1.0.400
1.0.390
1.0.380
1.0.370
1.0.360
1.0.350
1.0.340
1.0.330
1.0.320
1.0.310
1.0.300
1.0.290
1.0.280
1.0.270
1.0.260
1.0.250
1.0.240
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.71
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.11
1.0.00

Package Sidebar

Install

npm i keycloak-provider

Weekly Downloads

6

Version

1.0.120

License

ISC

Unpacked Size

115 kB

Total Files

8

Last publish

Collaborators

  • motiramshinde