logo
On this page

Offline login


Function introduction

Offline login is typically used when the application is cleared from memory, and the user relaunches the application by clicking on the application icon. In this scenario, the user can directly access the local SDK data without network connectivity or a successful login.

Note

Users can only perform an offline login using the UserID from their previous successful login; otherwise, the login will not succeed.

Technical principle

When the offline login interface is called, the ZIM SDK performs verification (UserID verification; if token-based login is used, it also verifies the token's validity period). After successful verification, the SDK returns a login success callback, allowing users to query local data, while the SDK internally attempts to establish a connection with the backend service automatically.

Implementation Steps

1. Offline Login

In the case of no network and the app being cleared, you can call the login interface with the isOfflineLogin parameter set to true in ZIMLoginConfig to achieve offline login using the user information from the last login. After successful login, you can access the local SDK data.

Note

We recommend caching the user information used for each login. When the app is opened, read the cache and implement the following logic:

  • If it is determined that the user did not use the UserID from the last online login, the isOfflineLogin should be set to false, and offline login will fail, not allowing the user to access the local SDK data for that UserID.
  • If the user used the UserID from the last online login, in order to achieve automatic login, speed up entering the app's main page, and pre-render the UI, the isOfflineLogin should be set to true to achieve offline login.

Definition of the Login Interface

Untitled
// This Login interface is the new login interface. The original login interface has been deprecated since version 2.13.0 and is no longer maintained.
login(userID: string, config: ZIMLoginConfig): Promise<void>;
1
Copied!
Parameter/CallbackTypeRequiredDescription
userIDstringYesThe user ID for the previous online login. It can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '< ', '= ', '. ', '> ', '?', '@', '[', ']', '^', '_', '{','}' '|', '~'.
configZIMLoginConfigYesLogin configuration.
PromisevoidNoLogin operation callback.

The config parameter needs to be configured with the following parameters of the ZIMLoginConfig class:

ParameterTypeRequiredDescription
userNamestringNoThe username, which can contain up to 256 bytes in length. If the value is empty, the UserName for the previous login is used.
tokenstringNoThe token.
  • This parameter is required for token-based authentication.
  • Do not specify it for AppSign-based authentication.
isOfflineLoginbooleanNoSpecifies whether this login is offline login. If the value is empty, the login is online login.

Sample call

Untitled
var userID = '';
var config = {
    token: '', // The validity period cannot exceed 24 days. Please request the developer server to obtain it.
               // Do not specify this parameter for AppSign-based authentication.
    userName: '',
    isOfflineLogin: true
};

zim.login(userID, config)
    .then(function() {
        // Offline login is successful, and local data of the SDK can be queried.
    })
    .catch(function(err) {
        // Login failed
    });
1
Copied!

2. Access local data of the SDK

After successful offline login, the following methods of the ZIM SDK 2.15.0 or earlier can be called when the network is disconnected.

CategoryMethod
User
Group
Message
Call invitationqueryCallInvitationList
Conversation

3. Listen for the login event

Listen for the connectionStateChanged event to obtain the status of connection between the ZIM SDK and the ZIM backend.

After a successful offline login, the value of ZIMConnectionState in connectionStateChanged is Connecting (enumeration value: 1).

You can call network methods to synchronize backend data only when the ZIM SDK automatically connects to the ZIM backend upon network reconnection and the value of ZIMConnectionState in connectionStateChanged is Connected (enumeration value: 2).

Previous

Multi-device login

Next

Blacklist management