Browse SDKs · React Native
SDKsReact Native

Authenticate and manage a session

Initialize the OpenIM React Native SDK, sign in, check login status, and log out the current account.

Copy

Before starting this page, complete the preparation in Before you start and React Native environment integration. The OpenIM React Native SDK is initialized with initSDK(), then login() signs in the current user and establishes the server connection.

The complete flow is:

  1. Call initSDK() to initialize the SDK.
  2. Register listeners for onConnecting, onConnectSuccess, and onConnectFailed.
  3. Call login() to sign in the current user.
  4. Wait for onConnectSuccess, which indicates that sign-in succeeded and the SDK is connected to the server.
  5. Call logout() when the user actively signs out or switches accounts.

Initialize the SDK

Call initSDK() with the server addresses, an application-writable data directory, and logging options. dataDir and logFilePath should use a persistent directory that the application can write to.

React Native CLI

import OpenIMSDK, { LogLevel } from '@openim/rn-client-sdk';
import RNFS from 'react-native-fs';

const dataDir = RNFS.DocumentDirectoryPath;

await OpenIMSDK.initSDK({
  apiAddr: 'http://your-server-ip:10002',
  wsAddr: 'ws://your-server-ip:10001',
  dataDir,
  logFilePath: dataDir,
  logLevel: LogLevel.Info,
  isLogStandardOutput: true,
});

Expo

Use the application document directory provided by expo-file-system. Its URI includes the file:// prefix, which must be removed before passing it to the SDK:

import OpenIMSDK, { LogLevel } from '@openim/rn-client-sdk';
import * as FileSystem from "expo-file-system/legacy";

const dataDir = FileSystem.documentDirectory.replace(/^file:\/\//, '').replace(/\/$/, '');

await OpenIMSDK.initSDK({
  apiAddr: 'http://your-server-ip:10002',
  wsAddr: 'ws://your-server-ip:10001',
  dataDir,
  logFilePath: dataDir,
  logLevel: LogLevel.Info,
  isLogStandardOutput: true,
});

Parameter reference

initSDK() accepts the following parameters:

ParameterTypeDescription
apiAddrstringOpenIMServer HTTP API address.
wsAddrstringOpenIMServer WebSocket address.
dataDirstringSDK local data directory in an application-writable path.
logFilePathstringSDK log directory in an application-writable path.
logLevelLogLevelLog level configuration. Use the LogLevel enum.
isLogStandardOutputbooleanWhether SDK logs are written to the platform standard output.

Register connection events

Register connection listeners before calling login(). Event names can use values from OpenIMEvent or the corresponding event strings; this example uses strings to show the login flow:

const handleConnecting = () => {
  console.log("onConnecting")
};

const handleConnectSuccess = () => {
  console.log("onConnectSuccess")
};

const handleConnectFailed = (error: OpenIMEventBaseErrorType) => {
  console.log("onConnectFailed", error.errCode, error.errMsg)
};

// Set listeners
OpenIMSDK.on('onConnecting', handleConnecting);
OpenIMSDK.on('onConnectSuccess', handleConnectSuccess);
OpenIMSDK.on('onConnectFailed', handleConnectFailed);

Sign in the current user

Call login() with the userID and token returned by your trusted backend:

await OpenIMSDK.login({
  userID,
  token,
});

After calling login(), wait for the onConnectSuccess event. This event indicates that sign-in succeeded and the SDK connected to the server. Call business APIs after this event.

Check login status

Use getLoginStatus() to query the current login status:

const loginStatus = await OpenIMSDK.getLoginStatus();

LoginStatus contains the following states:

StateDescription
LoginStatus.LogoutThe SDK instance is not signed in.
LoginStatus.LoggingThe sign-in flow is in progress.
LoginStatus.LoggedThe SDK instance is signed in.

Log out and switch accounts

Call logout() when the user actively signs out or switches accounts, and clear the current account data stored by the application:

await OpenIMSDK.logout();

When switching accounts, finish logging out the previous account before calling login() with the new account's parameters. There is no need to call initSDK() again. Do not sign in to a new account while the previous account is still signed in.

Remove event listeners

When logging out, switching accounts, or destroying the related state, call off() with the same event names and function references used during registration:

// Remove listeners
OpenIMSDK.off('onConnecting', handleConnecting);
OpenIMSDK.off('onConnectSuccess', handleConnectSuccess);
OpenIMSDK.off('onConnectFailed', handleConnectFailed);

Next step