In-app Chat
SDK Error Codes
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.

// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.

try{
    ZIMLoginConfig loginConfig = ZIMLoginConfig();
    // The user's nickname. Leave empty if you do not want to modify it.
    loginConfig.userName = 'userName';
    // If using token as the login authentication method, fill in this parameter. Otherwise, leave it empty.
    loginConfig.token = ''; 
    // Whether this login is an offline login. For details, refer to the offline login documentation.
    loginConfig.isOfflineLogin = false;
    await ZIM.getInstance()?.login('zego', loginConfig);
    // Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
    // Login failed.
    // For the error code of the login failure, refer to the error code documentation.
    onError.code;
    // The error message of the login failure.
    onError.message;
}

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.

// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.

try{
    ZIMLoginConfig loginConfig = ZIMLoginConfig();
    // The user's nickname. Leave empty if you do not want to modify it.
    loginConfig.userName = 'userName';
    loginConfig.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.
    // Whether this login is an offline login. For details, refer to the offline login documentation.
    loginConfig.isOfflineLogin = true;
    await ZIM.getInstance()?.login('zego', loginConfig);
    // Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
    // Login failed.
    // For the error code of the login failure, refer to the error code documentation.
    onError.code;
    // The error message of the login failure.
    onError.message;
}
  • 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.
zimService.setEventHandler(
  ZIMEventHandler(
    onConnectionStateChanged: (state, event, extendedData) {
      updateUserLoginState(state);
    },
  ),
);

void updateUserLoginState(ZIMConnectionState state) {
  if (state == ZIMConnectionState.disconnected) {
    currentLoginState = UserLoginState.notLogin;
  } else if (state == ZIMConnectionState.reconnecting) {
    currentLoginState = UserLoginState.offline;
  } else if (state == ZIMConnectionState.connected) {
    currentLoginState = UserLoginState.online;
  }
}

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

Overview

On this page

Back to top