On this page

Manage user login

2026-04-21

This article describes how to implement user login, determine user login status, and handle login status exceptions using the ZIM SDK.

Implement user login

First-time user login

Set isOfflineLogin to false in ZIMLoginConfig.

// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
// For Token authentication, pass in your Token. For details, refer to [Use Token authentication] or use a temporary Token.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token field as an empty string.
config.token = ""; 
config.isOfflineLogin = false;

zim_->login(userID, config, [=](zim::ZIMError errorInfo){
    // You can obtain the login result here and execute user code based on the error code.
});

Auto-login on app startup

If the user has previously logged in and has not actively logged out, you can choose one of the following methods based on your business requirements when the app starts:

  • Auto-login as the previous user

Create a ZIM instance on the launch page and call login with isOffline set to true.

// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
config.token = ""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
config.isOffline = true;

zim_->login(userID, config, [=](zim::ZIMError errorInfo){
    // You can obtain the login result here and execute user code based on the error code.
});
  • No auto-login

Navigate to the login page. Refer to First-time user login for the login process.

Login upon receiving an offline push

When receiving an offline push of iOS VoIP or Android CallStyle type:

  1. Create a ZIM instance and call login with isOffline set to false.
  2. After the state changes to CONNECTED in onConnectionStateChanged (the user's login status has changed to Online), call callAccept, callReject, callEnd, and other APIs.

Determine user login status

ZIM does not provide a callback for user login status changes in ZIMEventHandler. You can determine the current user's login status through the onConnectionStateChanged connection status change callback.

When the user's status changes, use the following conditions to determine the current user login status:

User statusCondition
OnlineonConnectionStateChanged is triggered, and the connection state is CONNECTED.
OfflineonConnectionStateChanged is triggered, and the connection state is RECONNECTING.
Not Logged InonConnectionStateChanged has never been triggered (i.e., login has never been called), or the connection state is DISCONNECTED.
zim_->setEventHandler([=](zim::ZIM *zim,
                         zim::ZIMConnectionState state,
                         zim::ZIMConnectionEvent event,
                         const std::string &extendedData) {
    updateUserLoginState(state);
});

void updateUserLoginState(zim::ZIMConnectionState state) {
    if (state == zim::ZIMConnectionState::kDisconnected) {
        currentLoginState = UserLoginState::kNotLogin;
    } else if (state == zim::ZIMConnectionState::kReconnecting) {
        currentLoginState = UserLoginState::kOffline;
    } else if (state == zim::ZIMConnectionState::kConnected) {
        currentLoginState = UserLoginState::kOnline;
    }
}

Handle user login exceptions

Network exceptions

When the user's device experiences a weak network or network disconnection, the SDK automatically triggers reconnection, and onConnectionStateChanged returns the state of RECONNECTING and the event of LOGIN_INTERRUPTED.

Starting from version 2.9.0, the SDK automatically reconnects at an appropriate frequency. Developers do not need to call the login API for reconnection at the business layer.

Reconnection results:

  • Reconnection succeeded: The state of CONNECTED and the event of SUCCESS are returned.
  • Reconnection failed: The state of DISCONNECTED and the corresponding event are returned. Calling other APIs will report an error with the error code 6000121 (not logged in).

Developers should display a UI prompt to the user when a network disconnection event is detected. If the SDK cannot reconnect, implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.

Account kicked out

When multi-device login is not enabled and another device logs in with the same account (userID) already logged in on this device, this device will be kicked offline, and the SDK will not automatically reconnect.

At this time, onConnectionStateChanged returns the state of DISCONNECTED and the event of KICKED_OUT. Calling other APIs will report an error with the error code 6000121 (not logged in).

Display a prompt to the user and implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.

Token expired

When the AppID is configured with Token authentication and the Token is about to expire, the onTokenWillExpire callback is triggered.

  • After obtaining a new Token, pass it to the SDK through the renewToken API.
  • If no new Token is passed in, the SDK will disconnect from the server after the Token expires. onConnectionStateChanged returns the state of DISCONNECTED and the event of TokenExpired. Calling other APIs will report an error with the error code 6000121 (not logged in).

Display a prompt to the user and implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.

Reconnection mechanism description

For more information about the reconnection mechanism, refer to Does ZIM SDK support reconnection.

2026-04-21

Previous

Subscribe to user online status

Next

Migration solution

On this page

Back to top