Authenticate and manage a session
Initialize the OpenIM React Native SDK, sign in, check login status, and log out the current account.
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:
- Call
initSDK()to initialize the SDK. - Register listeners for
onConnecting,onConnectSuccess, andonConnectFailed. - Call
login()to sign in the current user. - Wait for
onConnectSuccess, which indicates that sign-in succeeded and the SDK is connected to the server. - 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:
| Parameter | Type | Description |
|---|---|---|
apiAddr | string | OpenIMServer HTTP API address. |
wsAddr | string | OpenIMServer WebSocket address. |
dataDir | string | SDK local data directory in an application-writable path. |
logFilePath | string | SDK log directory in an application-writable path. |
logLevel | LogLevel | Log level configuration. Use the LogLevel enum. |
isLogStandardOutput | boolean | Whether 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:
| State | Description |
|---|---|
LoginStatus.Logout | The SDK instance is not signed in. |
LoginStatus.Logging | The sign-in flow is in progress. |
LoginStatus.Logged | The 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
Was this page helpful?