# expo-keycloak-adapter

> expo-auth-session wrapper for keycloak authentication

Latest version **1.0.0** (published 2024-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install expo-keycloak-adapter
pnpm add expo-keycloak-adapter
yarn add expo-keycloak-adapter
bun add expo-keycloak-adapter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2024-01-10 |
| First published | 2024-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 481 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | mesquitadev |
| Maintainers | mesquitadev |
| Keywords | expo, keycloak, AuthSession, oauth2, openid, react native |

## Links

- npm: https://www.npmjs.com/package/expo-keycloak-adapter
- Repository: https://github.com/mesquitadev/expo-keycloak-auth
- Homepage: https://github.com/mesquitadev/expo-keycloak-auth#readme
- Issues: https://github.com/mesquitadev/expo-keycloak-auth/issues
- npm.io page: https://npm.io/package/expo-keycloak-adapter

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^18.2.0
- [expo-auth-session](https://npm.io/package/expo-auth-session.md) ^5.2.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2024-01-10

## README

# expo-keycloak-auth

> expo-auth-session wrapper for keycloak

[![NPM](https://img.shields.io/npm/v/expo-keycloak-adapter.svg)](https://www.npmjs.com/package/expo-keycloak-adapter)

## Install

Install peer dependencies

```bash
expo install @react-native-async-storage/async-storage expo-auth-session expo-random
```

Install this library

```bash
expo install expo-keycloak-adapter
```

## Usage

```jsx
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 Name                   | Usage        | Default        | Description                                                                                                                                                                 |
| ---------------------------- | ------------ | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| clientId (string)            | **required** |                | One of your keycloak clientId                                                                                                                                               |
| realm (string)               | **required** |                | One of your keycloak realm name                                                                                                                                             |
| url (string)                 | **required** |                | Your 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)              | optional     |                | Your 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) | optional     | false          |                                                                                                                                                                             |
| nativeRedirectPath (string)  | optional     | undefined      | Path to override default redirect path                                                                                                                                      |
| refreshTimeBuffer (number)   | optional     | 20             | time buffer in seconds to invoke `AuthSession.refreshAsync()` before token expires.                                                                                         |
| tokenStorageKey (string)     | optional     | keycloak_token | AsyncStorage key to save your token responses.                                                                                                                              |
| extraParams (object)         | optional     | undefined      | Extra 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 © [mesquitadev](https://github.com/mesquitadev)

---
_Source: https://npm.io/package/expo-keycloak-adapter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
