React Native
This document describes how to integrate Galaxy ID for React Native.
Software Name: Galaxy ID
Software Version: 0.1
Acronyms and Terms
No.
Words
Descriptions
1
GID
Galaxy ID
2
SSO
Single Sign-On
4
Client
An application that integrates for GID
Summary
Galaxy ID is an SSO system. It provides endpoints that enable clients to authenticate and authorize in many places.
Integrate
Install
or
Setup Deep links (iOS)
To navigate back from webview to your app, you have to configure deep linking.
And in AppDelegate.m
, add these lines:
N.B.: replace myapp
with the name of your app
Setup Deep links (Android)
To configure the external linking in Android, you can create a new intent in the manifest.
The easiest way to do this is with the uri-scheme
package:
If you want to add it manually, open up YourApp/android/app/src/main/AndroidManifest.xml
, and make the following adjustments:
Add the new intent-filter inside the MainActivity entry with a VIEW type action:
N.B.: replace myapp
with the name of your app
Setup RNKeycloak instance
Create a keycloak.ts
file in the src
folder of your project (where App.ts
is located) with the following content:
keycloak.json
(will be provided by admin):
Setup ReactNativeKeycloakProvider
Wrap your App inside KeycloakProvider
and pass the keycloak
instance as prop
N.B. If you're using other providers (such as react-redux
) it is recommended to place them inside ReactNativeKeycloakProvider
.
ReactNativeKeycloakProvider
automatically invokes keycloak.init()
method when needed and supports the following props:
LoadingComponent
, a component to be displayed whilekeycloak
is being initialized, if not provided child components will be rendered immediately. Defaults tonull
isLoadingCheck
, an optional loading check function to customize LoadingComponent display condition. Returntrue
to display LoadingComponent,false
to hide it. Can be implemented as follows:
onEvent
, a handler function that receives events launched bykeycloak
, defaults tonull
. It can be implemented as follows:
Published events are:
onReady
onInitError
onAuthSuccess
onAuthError
onAuthRefreshSuccess
onAuthRefreshError
onTokenExpired
onAuthLogout
onTokens
, a handler function that receiveskeycloak
tokens as an object every time they change, defaults tonull
. Keycloak tokens are returned as follows:
Hook Usage
When a component requires access to Keycloak
, you can use the useKeycloak
Hook.
External Usage (Advanced)
If you need to access keycloak
instance from non-React
files (such as sagas
, utils
, providers
...), you can import the instance directly from the keycloak.ts
file.
The instance will be initialized by react-keycloak
but you'll need to be careful when using the instance and avoid setting/overriding any props, you can however freely access the exposed methods (such as refreshToken
, login
, etc...).
Get user information
With the access token, we can get user info with tokenParsed property: keycloak.tokenParsed (recommend).
With Rest API:
URL: <host>/realms/<realmName>/protocol/openid-connect/userinfo
Method: GET
Header: Authorization: Bearer <access token> (keycloak)
Example request:
Example response:
Last updated