1.0.120 • Published 3 years ago

keycloak-provider v1.0.120

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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)

1.0.120

3 years ago

1.0.119

3 years ago

1.0.118

3 years ago

1.0.117

3 years ago

1.0.116

3 years ago

1.0.115

3 years ago

1.0.114

3 years ago

1.0.113

3 years ago

1.0.112

3 years ago

1.0.111

3 years ago

1.0.110

3 years ago

1.0.109

3 years ago

1.0.108

3 years ago

1.0.107

3 years ago

1.0.106

3 years ago

1.0.105

3 years ago

1.0.104

3 years ago

1.0.103

3 years ago

1.0.102

3 years ago

1.0.101

3 years ago

1.0.100

3 years ago

1.0.99

3 years ago

1.0.98

3 years ago

1.0.97

3 years ago

1.0.96

3 years ago

1.0.95

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.92

3 years ago

1.0.91

3 years ago

1.0.90

3 years ago

1.0.89

3 years ago

1.0.88

3 years ago

1.0.87

3 years ago

1.0.85

3 years ago

1.0.84

3 years ago

1.0.83

3 years ago

1.0.82

3 years ago

1.0.81

3 years ago

1.0.80

3 years ago

1.0.79

3 years ago

1.0.78

3 years ago

1.0.77

3 years ago

1.0.76

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.69

3 years ago

1.0.68

3 years ago

1.0.67

3 years ago

1.0.66

3 years ago

1.0.65

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.62

3 years ago

1.0.61

3 years ago

1.0.60

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.52

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.49

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago