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.
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.
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
// 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>;
Parameter/Callback | Type | Required | Description |
---|---|---|---|
userID | string | Yes | The user ID for the previous online login. It can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '< ', '= ', '. ', '> ', '?', '@', '[', ']', '^', '_', '{','}' '|', '~'. |
config | ZIMLoginConfig | Yes | Login configuration. |
Promise | void | No | Login operation callback. |
The config
parameter needs to be configured with the following parameters of the ZIMLoginConfig class:
Parameter | Type | Required | Description |
---|---|---|---|
userName | string | No | The username, which can contain up to 256 bytes in length. If the value is empty, the UserName for the previous login is used. |
token | string | No | The token.
|
isOfflineLogin | boolean | No | Specifies whether this login is offline login. If the value is empty, the login is online login. |
Sample call
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
});
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.
Category | Method |
---|---|
User | |
Group | |
Message |
|
Call invitation | queryCallInvitationList |
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
).