1.4.7 • Published 2 months ago

@anzeblabla/expo-keycloak-auth v1.4.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

expo-keycloak-auth

expo-auth-session wrapper for keycloak

This library is based on balgamat/expo-keycloak written in Javascript with re-implementation of automatic token refresh handling.

NPM

Publish

npm publish --access public

Install

Install peer dependencies

expo install expo-auth-session

Install this library

expo install expo-keycloak-auth

Usage

import React from "react";
import {
  Text,
  View,
  Button,
  StyleSheet,
  ActivityIndicator,
  TextInput,
} from "react-native";
import { KeycloakProvider, useKeycloak } from "expo-keycloak-auth";
import AppConfig from "./app.json";

export default function App() {
  const keycloakConfiguration = {
    clientId: "your-keycloak-clientId",
    realm: "master", // your realm name
    url: "https://your.keycloak.url.com/auth", // This is usually a url ending with /auth
    scheme: AppConfig.expo.scheme,
  };

  return (
    <KeycloakProvider {...keycloakConfiguration}>
      <View style={styles.container}>
        <Auth />
      </View>
    </KeycloakProvider>
  );
}

export const Auth = () => {
  const {
    ready, // If the discovery is already fetched and ready to prompt authentication flow
    login, // The login function - opens the browser
    isLoggedIn, // Helper boolean to use e.g. in your components down the tree
    token, // Access token, if available
    logout, // The logout function - Logs the user out
  } = useKeycloak();
  if (!ready) return <ActivityIndicator />;

  if (!isLoggedIn)
    return (
      <View style={{ margin: 24 }}>
        <Button onPress={login} title="Login" />
      </View>
    );

  return (
    <View style={{ margin: 24 }}>
      <Text style={{ fontSize: 17, marginBottom: 24 }}>Logged in!</Text>
      <Text>Your Access Token</Text>
      <TextInput value={token}></TextInput>
      <Button onPress={logout} title={"Logout"} style={{ marginTop: 24 }} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

Configuration

Pass this configuration to KeycloakProvider as props:

Props NameUsageDefaultDescription
clientId (string)requiredOne of your keycloak clientId
realm (string)requiredOne of your keycloak realm name
url (string)requiredYour keycloak url. This is usually a url ending with /auth. This props (with realm) used to generate url ${url}/realms/${realm} for expo AuthSession.useAutoDiscovery()
scheme (string)optionalYour app scheme defined in app.json. This props used to generate redirect uri scheme for standalone app. default redirect_uri = ${scheme}://redirect/auth
disableAutoRefresh (boolean)optionalfalse
nativeRedirectPath (string)optionalundefinedPath to override default redirect path
refreshTimeBuffer (number)optional20time buffer in seconds to invoke AuthSession.refreshAsync() before token expires.
tokenStorageKey (string)optionalkeycloak_tokenSecureStore key to save your token responses.
extraParams (object)optionalundefinedExtra query params that'll be added to the query string

NOTE: You must add the scheme value to your valid redirect URLs on Keycloak admin console. It has to be like: ${scheme}://* being ${scheme} the current selected value from AppConfig.

License

MIT © rubhiauliatirta

1.4.6

2 months ago

1.4.5

2 months ago

1.4.4

2 months ago

1.4.3

2 months ago

1.4.2

2 months ago

1.4.7

2 months ago

1.3.2

10 months ago

1.3.1

10 months ago

1.3.0

11 months ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18-beta

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago