Expo and React Native SDK

Link to this section

The Kinde React Native SDK allows developers to quickly and securely integrate a new or an existing React Native application into the Kinde platform. This SDK is for people using Expo.

Supported versions

Link to this section

We support Expo with React Native versions 0.70 and higher.

Use this document for Expo and React Native.

To use just React Native see React Native SDK. You can also view the React Native docs and React Native starter kit in GitHub.

Register for Kinde

Link to this section

If you haven’t already got a Kinde account, register for free here (no credit card required). Registering gives you a Kinde domain, which you need to get started, e.g. yourapp.kinde.com.

Before you install

Link to this section

You will need Node, the React Native command line interface, a JDK, Android Studio (for Android) and Xcode (for iOS).

Follow the installation instructions for your chosen OS to install dependencies.

Installation with Expo Managed Workflow

Link to this section
# npm
npm i @kinde-oss/react-native-sdk-0-7x
# yarn
yarn add @kinde-oss/react-native-sdk-0-7x
# pnpm
pnpm i @kinde-oss/react-native-sdk-0-7x

Installation with Bare React Native

Link to this section
  1. The SDK can be installed using your package manager or expo cli

    # npm
    npm i @kinde-oss/react-native-sdk-0-7x
    # yarn
    yarn add @kinde-oss/react-native-sdk-0-7x
    # pnpm
    pnpm i @kinde-oss/react-native-sdk-0-7x
    
    # With Expo CLI
    expo install @kinde-oss/react-native-sdk-0-7x
    # With Expo CLI (npx)
    npx expo install @kinde-oss/react-native-sdk-0-7x
    
  2. Configure the deep link support for your app

    npx uri-scheme add <your scheme>
    
    # npx uri-scheme add myapp
    
  3. Edit android/settings.gradle

    ...
    
    include ':react-native-keychain'
    project(':react-native-keychain').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keychain/android')
    
    include ':react-native-inappbrowser-reborn'
    project(':react-native-inappbrowser-reborn').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-inappbrowser-reborn/android')
    ...
    
  4. Edit android/app/build.gradle

    apply plugin: 'com.android.application'
    
    android {
      ...
    }
    
    dependencies {
      ...
    
      implementation project(':react-native-keychain')
    	implementation project(':react-native-inappbrowser-reborn')
    
      ...
    }
    
  5. Edit MainApplication.java

    ...
    import com.oblador.keychain.KeychainPackage;
    import com.proyecto26.inappbrowser.RNInAppBrowserPackage;
    ...
    
    public class MainApplication extends Application implements ReactApplication {
        private final ReactNativeHost mReactNativeHost =
          new ReactNativeHost(this) {
            ...
            @Override
            protected List<ReactPackage> getPackages() {
    					...
              List<ReactPackage> packages = new PackageList(this).getPackages();
              packages.add(new KeychainPackage());
              packages.add(new RNInAppBrowserPackage());
    					...
              return packages;
            }
            ...
          };
    
      ...
    }
    ...
    

    In React Native version 0.73 or above, the MainApplication.java file has been replaced with MainApplication.kt

    import com.oblador.keychain.KeychainPackage;
    import com.proyecto26.inappbrowser.RNInAppBrowserPackage;
    ...
    
    class MainApplication : Application(), ReactApplication {
        override val reactNativeHost: ReactNativeHost =
          object : DefaultReactNativeHost(this) {
            override fun getPackages(): List<ReactPackage> =
                PackageList(this).packages.apply {
                  // Packages that cannot be autolinked yet can be added manually here, for example:
                  // add(MyReactNativePackage())
                  add(KeychainPackage());
                  add(RNInAppBrowserPackage());
                }
    
            ...
          }
    
      ...
    }
  6. Execute the command yarn run android or npx expo run:android

  1. Install the CocoaPods. We recommend installing CocoaPods using Homebrew.

    # Install CocoaPods via brew
    brew install cocoapods
    
  2. Install the SDK

    expo install @kinde-oss/react-native-sdk-0-7x expo-secure-store expo-web-browser expo-constants
    // or
    npx expo install @kinde-oss/react-native-sdk-0-7x expo-secure-store expo-web-browser expo-constants
    
  3. Add the following to your Podfile and run pod update:

    pod 'RNKeychain', :path => '../node_modules/react-native-keychain'
    pod 'RNInAppBrowser', :path => '../node_modules/react-native-inappbrowser-reborn'
    
    cd ios && pod update
    
  4. Execute the command yarn run ios or npx expo run:ios

If you encounter any errors during the SDK installation process, you can refer to the General tips section at the bottom of this document, for troubleshooting guidance.

Kinde configuration

Link to this section
  1. In Kinde, go to Settings > Applications.
  2. Select View details on the Frontend app.
  3. Scroll down to the Callback URLs section.
  4. Add in the callback URLs for your React Native app, which should look something like this:
    • Allowed callback URLs - <myapp://localhost:3000>/kinde_callback
    • Allowed logout redirect URLs - <myapp://localhost:3000>/kinde_callback

Make sure you press the Save button at the bottom of the page!

Note: The <myapp://localhost:3000>/kinde_callback/kinde_callback is used as an example of local URL Scheme, change to the local URL Scheme or production URL Scheme that you use.

If you would like to use our Environments feature as part of your development process. You will need to create them first within your Kinde account. In this case you would use the Environment subdomain in the code block above.

Configure your app

Link to this section

Environment variables

Link to this section

Put these variables in your .env file. You can find these variables on the same page as where you set the callback URLs.

  • KINDE_ISSUER_URL - your Kinde domain
  • KINDE_POST_CALLBACK_URL - After the user authenticates we will callback to this address. Make sure this URL is under your allowed callback URLs
  • KINDE_POST_LOGOUT_REDIRECT_URL - where you want users to be redirected to after logging out. Make sure this URL is under your allowed logout redirect URLs
  • KINDE_CLIENT_ID - you can find this on the App Keys page
KINDE_ISSUER_URL=https://<your_kinde_subdomain>.kinde.com
KINDE_POST_CALLBACK_URL=<myapp://localhost:3000>/kinde_callback
KINDE_POST_LOGOUT_REDIRECT_URL=<myapp://localhost:3000>/kinde_callback
KINDE_CLIENT_ID=<your_kinde_client_id>

Note: The myapp://localhost:3000/kinde_callback is used as an example of local URL Scheme, change to the local URL Scheme or production URL Scheme that you use.

Link to this section

Linking to your app

Link to this section

To link to your development build or standalone app, you need to specify a custom URL scheme for your app. You can register a scheme in your Expo config (app.jsonapp.config.js) by adding a string under the scheme key:

{
  "expo": {
    "scheme": "myapp",
    ...
  }
}

Linking to Expo Go

Link to this section

Expo Go uses the exp:// scheme, however, if we link to exp:// without any address afterward, it will open the app to the home screen.

In development, your app will live at a url like exp://127.0.0.1:19000. When published, an experience will be hosted at a URL like  exp://u.expo.dev/[project-id]?channel-name=channel-name]&runtime-version=[runtime-version], where u.expo.dev/[project-id] is the hosted URL that Expo Go fetches from.

Caution: You also should update your callback URL to both your app and Kinde.

KINDE_ISSUER_URL=https://your_kinde_domain.kinde.com
KINDE_POST_CALLBACK_URL=exp://your_machine_ip:your_machine_port // f.e: exp://127.0.0.1:19000
KINDE_CLIENT_ID=your_kinde_client_id
KINDE_POST_LOGOUT_REDIRECT_URL=exp://your_machine_ip:your_machine_port // f.e: exp://127.0.0.1:19000

Integrate with your app

Link to this section

To create a new instance of the KindeSDK object, execute the code below.

...
import { KindeSDK } from '@kinde-oss/react-native-sdk-0-7x';
...

...
const client = new KindeSDK(YOUR_KINDE_ISSUER, YOUR_KINDE_REDIRECT_URI, YOUR_KINDE_CLIENT_ID, YOUR_KINDE_LOGOUT_REDIRECT_URI);
...

Sign in and registration

Link to this section

Kinde provides methods for easily implementing a login / register flow.

As an example if you add buttons as follows.

<View>
    <View>
        <Button title="Sign In" onPress={handleSignIn} />
    </View>
    <View>
        <Button title="Sign Up" color="#000" onPress={handleSignUp} />
    </View>
</View>

Then define new functions that match for each button.

Note: Make sure you’ve already defined KindeSDK as client in the state.

...
const handleSignUp = async () => {
  const token = await client.register();
  if (token) {
    // User was authenticated
  }
};

const handleSignIn = async () => {
  const token = await client.login();
  if (token) {
    // User was authenticated
  }
};
...

This is implemented in much the same way as signing in or registering. The Kinde SPA client comes with a logout method:

const handleLogout = async () => {
    const loggedOut = await client.logout();
    if (loggedOut) {
        // User was logged out
    }
};

We have also implemented an API for token revocation. Pass true as an argument in the logout function. This flag will assist in revoking the token without having to open the website within your apps.

const handleLogout = async () => {
    const loggedOut = await client.logout(true);
    if (loggedOut) {
        // User was logged out
    }
};

Note: Handle redirects are now deprecated

Starting from version 1.1 of the SDK, the need to handle redirects has been eliminated. Authentication is now performed by launching a web browser within your app instead of relying on an external browser. For a comprehensive example of how to handle authentication, see below.

Full code sample for authentication

Link to this section
const checkAuthenticate = async () => {
    // Using `isAuthenticated` to check if the user is authenticated or not
    if (await client.isAuthenticated) {
        // Need to implement, e.g: call an api, etc...
    } else {
        // Need to implement, e.g: redirect user to sign in, etc..
    }
};

useEffect(() => {
    checkAuthenticate();
}, []);

const handleSignIn = async () => {
    const token = await client.login();

    if (token) {
        // Need to implement, e.g: call an api, etc...
    }
};

const handleSignUp = async () => {
    const token = await client.register();

    if (token) {
        // Need to implement, e.g: call an api, etc...
    }
};

const handleLogout = async () => {
    // With open web in your apps
    const isLoggedOut = await client.logout();

    if (isLoggedOut) {
        // Need to implement, e.g: redirect user to login screen, etc...
    }

    // Without open web in your apps
    const isLoggedOut = await client.logout();

    if (isLoggedOut) {
        // Need to implement, e.g: redirect user to login screen, etc...
    }
};

Get user information

Link to this section

To access the user information, use the getUserDetails helper function.

const userProfile = await client.getUserDetails();
console.log(userProfile);
// output: {"given_name":"Dave","id":"abcdef","family_name":"Smith","email":"dave@smith.com"}

View users in Kinde

Link to this section

Go to the Users page within Kinde to see your newly registered user.

User Permissions

Link to this section

Once a user has been verified, your product/application will return the JWT token with an array of permissions for that user. You will need to configure your product/application to read permissions and unlock the respective functions.

Set permissions in your Kinde account. Here’s an example set of permissions.

const permissions = [
    "create:todos",
    "update:todos",
    "read:todos",
    "delete:todos",
    "create:tasks",
    "update:tasks",
    "read:tasks",
    "delete:tasks"
];

We provide helper functions to more easily access permissions.

await client.getPermission("create:todos");
// {orgCode: "org_1234", isGranted: true}

await client.getPermissions();
// {orgCode: "org_1234", permissions: ["create:todos", "update:todos", "read:todos"]}

A practical example in code might look something like.

if ((await client.getPermission("create:todos")).isGranted) {
    // show Create Todo button in UI
}

An audience is the intended recipient of an access token - for example the API for your application. The audience argument can be passed to the Kinde client to request an audience be added to the token.

The audience of a token is the intended recipient of the token.

const client = new KindeSDK(
    YOUR_KINDE_ISSUER,
    YOUR_KINDE_REDIRECT_URI,
    YOUR_KINDE_CLIENT_ID,
    YOUR_KINDE_LOGOUT_REDIRECT_URI,
    YOUR_SCOPES,
    {
        audience: "api.yourapp.com"
    }
);

For details on how to connect, see Register an API.

Overriding scope

Link to this section

By default the KindeSDK requests the following scopes:

  • profile
  • email
  • offline
  • openid

You can override this by passing scope into the KindeSDK

const client = new KindeSDK(
    YOUR_KINDE_ISSUER,
    YOUR_KINDE_REDIRECT_URI,
    YOUR_KINDE_CLIENT_ID,
    YOUR_KINDE_LOGOUT_REDIRECT_URI,
    "profile email offline openid"
);

Getting claims

Link to this section

We have provided a helper to grab any claim from your id or access tokens. The helper defaults to access tokens:

await client.getClaim("aud");
// ["api.yourapp.com"]

await client.getClaim("given_name", "id_token");
// "David"

For information about how organizations work in Kinde, see Kinde organizations for developers.

Create an organization

Link to this section

To create a new organization within your application, you will need to run a similar function to this.

<Button title="Create Organization" onPress={handleCreateOrg} />

Then define the function of the button.

Make sure you’ve already defined KindeSDK as the client in the state.

const handleCreateOrg = () => {
    client.createOrg();
};

// You can also pass `org_name` as your organization
client.createOrg({org_name: "Your Organization"});

Sign in and sign up to organizations

Link to this section

Kinde has a unique code for every organization. You’ll have to pass this code through when you register a new user.

Example function below:

client.register({org_code: "your_org_code"});

If you want a user to sign in into a particular organization, pass this code along with the sign in method.

client.login({org_code: "your_org_code"});

Following authentication, Kinde provides a json web token (jwt) to your application. Along with the standard information we also include the org_code and the permissions for that organization (this is important as a user can belong to multiple organizations and have different permissions for each). Example of a returned token:

Example of a returned token:

{
    "aud": [],
    "exp": 1658475930,
    "iat": 1658472329,
    "iss": "https://your_subdomain.kinde.com",
    "jti": "123457890",
    "org_code": "org_1234",
    "permissions": ["read:todos", "create:todos"],
    "scp": ["openid", "profile", "email", "offline"],
    "sub": "kp:123457890"
}

The id_token will also contain an array of organizations that a user belongs to - this is useful if you wanted to build out an organization switcher for example.

{
  ...
  "org_codes": ["org_1234", "org_4567"]
  ...
}

There are two helper functions you can use to extract information.

await client.getOrganization();
// {orgCode: "org_1234"}

await client.getUserOrganizations();
// {orgCodes: ["org_1234", "org_abcd"]}

Once the user has successfully authenticated, you’ll have a JWT and a refresh token and that has been stored securely. E.g. using the getAccessToken method of the Storage class to get an access token.

...
import { Storage } from '@kinde-oss/react-native-sdk-0-7x'
...

const accessToken = await Storage.getAccessToken();
console.log('access_token', accessToken);

We’re using the expo-secure-store for Expo.

The storage handler can be found at: Storage class

Run the test suite using the following command at the root of your React Native.

npm run test

Note: Ensure you have already run npm install

SDK API Reference

Link to this section

Either your Kinde URL or your custom domain. e.g https://yourapp.kinde.com.

Type: string

Required: Yes

The URL that the user will be returned to after authentication.

Type: string

Required: Yes

The unique ID of your application in Kinde.

Type: string

Required: Yes

logoutRedirectUri

Link to this section

Where your user will be redirected when they sign out.

Type: string

Required: No

The scopes to be requested from Kinde.

Type: boolean

Required: No

Default: openid profile email offline

additionalParameters

Link to this section

Additional parameters that will be passed in the authorization request.

Type: object

Required: No

Default: {}

additionalParameters - audience

Link to this section

The audience claim for the JWT.

Type: string

Required: No

KindeSDK methods

Link to this section

Constructs a redirect URL and sends the user to Kinde to sign in.

Arguments:

{org_code?: string}

Usage:

await kinde.login();
or;
await kinde.login({org_code: "your organization code"});

Allow org_code to be provided if a specific organization is being signed into.

Sample output:

{
	"access_token": "eyJhbGciOiJSUzI...",
	"expires_in": 86400,
	"id_token": "eyJhbGciOiJSU...",
	"refresh_token": "yXI1bFQKbXKLD7AIU...",
	"scope": "openid profile email offline",
	"token_type": "bearer"
}

Constructs a redirect URL and sends the user to Kinde to sign up.

Arguments:

{org_code?: string}

Usage:

await kinde.register();
or;
await kinde.register({org_code: "your organization code"});

Allow org_code to be provided if a specific organization is being registered to.

Sample output:

{
	"access_token": "eyJhbGciOiJSUzI...",
	"expires_in": 86400,
	"id_token": "eyJhbGciOiJSU...",
	"refresh_token": "yXI1bFQKbXKLD7AIU...",
	"scope": "openid profile email offline",
	"token_type": "bearer"
}

Logs the user out of Kinde.

Arguments:

isRevoke: boolean; // default is false

Usage:

await kinde.logout();
or;
await kinde.logout(true);

Sample output: true or false

Returns the raw Access token from URL after logged from Kinde.

Arguments:

url?: string

Usage:

await kinde.getToken(url);
or;
await kinde.getToken();

You need to have already authenticated. Otherwise, an error will occur.

Sample output:

{
	"access_token": "eyJhbGciOiJSUzI...",
	"expires_in": 86400,
	"id_token": "eyJhbGciOiJSU...",
	"refresh_token": "yXI1bFQKbXKLD7AIU...",
	"scope": "openid profile email offline",
	"token_type": "bearer"
}

Constructs a redirect URL and sends the user to Kinde to sign up and create a new organization in your business.

Arguments:

{org_name?: string}

Usage:

await kinde.createOrg();
or;
await kinde.createOrg({org_name: "your organization name"});

Allow org_name to be provided if you want a specific organization name when you create.

Sample output:

{
	"access_token": "eyJhbGciOiJSUzI...",
	"expires_in": 86400,
	"id_token": "eyJhbGciOiJSU...",
	"refresh_token": "yXI1bFQKbXKLD7AIU...",
	"scope": "openid profile email offline",
	"token_type": "bearer"
}

Gets a claim from an access or ID token.

Arguments:

claim: string; tokenKey?: string

Usage:

await kinde.getClaim("given_name", "id_token");

Sample output:

"David";

Returns the state of a given permission.

Arguments: key: string

Usage:

await kinde.getPermission("read:todos");

Sample output:

{
	"orgCode": "org_1234",
	"isGranted": true
}

getPermissions

Link to this section

Returns all permissions for the current user for the organization they are signed into.

Usage:

await kinde.getPermissions();

Sample output:

{
	"orgCode":"org_1234",
	"permissions": ["create:todos","update:todos","read:todos"]
}

getOrganization

Link to this section

Get details for the organization your user is signed into.

Usage:

await kinde.getOrganization();

Sample output:

{
	"orgCode": "org_1234"
}

getUserDetails

Link to this section

Returns the profile for the current user.

Usage:

await kinde.getUserDetails();

Sample output:

{
	"given_name": "Dave",
	"id": "abcdef",
	"family_name": "Smith",
	"email": "dave@smith.com"
}

getUserOrganizations

Link to this section

Gets an array of all organizations the user has access to.

Usage:

await kinde.getUserOrganizations();

Sample output:

{ "orgCodes": ["org1234", "org5678"] }

isAuthenticated

Link to this section

Return the boolean to demonstrate whether the user is authenticated or not.

Usage:

await kinde.isAuthenticate;

Sample output: true or false

Building Issues

Link to this section
  • 'value' is unavailable: introduced in iOS 12.0

    If you got the error ‘value’ is unavailable: introduced in iOS 12.0 when trying to build the app, you can follow the below steps to fix that:

    1. In your Xcode project navigator, select Pods.
    2. Under Targets, select React-Codegen.
    3. Select the Build Settings tab**.**
    4. Under Deployment, set iOS Deployment Target to 12.4.
    5. Clean project and rebuild: Product > Clean Build Folder, Product > Build.
  • Dependency 'androidx.browser:browser:1.6.0-beta01' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs.

    The solution is add androidXBrowser = "1.4.0" in your project.

    // android/build.gradle
    buildscript {
        ...
        ext {
            // ...
            androidXBrowser = "1.4.0"
            // ....
        }
        ...
    }
    
    
  • Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.10 (org.jetbrains.kotlin:kotlin-stdlib:1.8.10) and jetified-kotlin-stdlib-jdk8-1.7.22 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.22)

    The solution is add org.jetbrains.kotlin:kotlin-bom:1.8.0 dependency in your project.

    // android/app/build.grade
    dependencies {
        ...
        implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
        ...
    }
    
    

Caching Issues

Link to this section

Sometimes there will be issues related to caching when you develop React Native. There are some recommendations for cleaning the cache:

  1. Remove node_modules, yarn.lock or package-lock.json.
  2. Clean cache: yarn cache clean or npm cache clean --force.
  3. Make sure you have changed values in .env file.
  4. Trying to install packages again: yarn install or npm install.
  5. Run Metro Bundler: yarn start --reset-cache or npm start --reset-cache.

Assume your StarterKit path is <StarterKit_PATH>.

Clean cache for Android

  1. Run this:

    cd <StarterKit_PATH>/android
    ./gradlew clean
    

Clean cache for iOS

  1. Run this:

    cd <StarterKit_PATH>/ios
    rm -rf Pods && rm Podfile.lock
    
  2. Clean build folders on Xcode.

If you need help connecting to Kinde, please contact us at support@kinde.com.


Talk to us

If you can’t find what you’re looking for in our help center — email our team

Contact support