Step 3: Login and Logout

Feature Description

Users can send and receive messages only after they have logged in to the Chat SDK. To log in to the Tencent backend server, a user needs to provide information, including the UserID and UserSig. For more information on these parameters, see Login Authentication.

Login

Note:
1. After a successful login, you must wait for the SDK to be in the ready state (by listening for the event TencentCloudChat.EVENT.SDK_READY) before calling interfaces that require authentication, such as sendMessage.
2. By default, multi-instance login is not supported, meaning if this account is already logged in on another page, continuing to log in successfully on the current page may kick the other pages offline. When a user is kicked offline, the event TencentCloudChat.EVENT.KICKED_OUT is triggered, and the user can handle it accordingly after listening for the event.
3. To support multi-instance login (allowing logging in to the same account concurrently on multiple webpages), log in to the Chat Console, find the target SDKAppID, and click Application > Configuration > Login and Message > Login Settings > Max Login Instances per User per Platform to configure the number of instances. The configuration will take effect in 5 minutes.
4. For mini-programs, mini-games, uni-app, and React Native (even if packaged into a native app) platforms, logins are considered web instances.
5. If this interface returns an error code 60020, it means you have not purchased a package, or the package has expired, or the purchased package is still being configured and has not yet taken effect. Please log in to the Chat purchase page to repurchase the package. After purchase, it will take effect within 5 minutes.
6. If this interface returns an error code 2801 on the mini-program platform, please check the mini-program trusted domain configuration.
API
chat.login(options);
Parameters
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Description
UserID
String
User ID.
UserSig
String
The password with which the user logs in to the Chat console. It is essentially the ciphertext generated by encrypting information such as the UserID. For the detailed generation method, see Generating UserSig.
Return value
Promise
Examples
let promise = chat.login({userID: 'your userID', userSig: 'your userSig'});
promise.then(function(imResponse) {
console.log(imResponse.data); // Logged in successfully
if (imResponse.data.repeatLogin === true) {
// This indicates that the account is already logged in.
console.log(imResponse.data.errorInfo);
}
}).catch(function(imError) {
console.warn('login error:', imError);
});

Get logged-in user information.

API
chat.getLoginUser();
Parameters
None
Return value
String
Examples
const userID = chat.getLoginUser();

Get server time

API
chat.getServerTime();
Parameters
None
Return value
Number
Examples
const serverTime = chat.getServerTime();

Whether the SDK is ready

After the SDK is ready, developers can call the SDK's APIs to send messages and use various features of the SDK.
Developers can also listen for:
SDK_READY, which is triggered when the SDK enters the ready state.
SDK_NOT_READY, which is triggered when the SDK enters the not ready state.
API
chat.isReady();
Parameters
None
Return value
Boolean
Examples
let isReady = chat.isReady();

Logout

This API is used to log out of the Chat SDK. It is usually called when you switch between accounts. This API clears the login status of the current account and all the data in the memory.
Note:
1. When calling this API, the instance publishes the SDK_NOT_READY event. In this case, the instance is logged out and cannot receive or send messages.
2. If the "Number of Concurrent Online Web Instances" setting in the Chat Console is greater than 1, and the same account is logged in to instances a1 and a2 (including mini program client), after executing a1.logout(), a1 will be logged out and cannot receive or send messages, while a2 is not affected.
3. If "Number of Concurrent Online Web Instances" is set to 2, and your account is logged in to instances a1 and a2, when you log in to instance a3, one of a1 or a2 will be kicked offline. Usually, the instance that entered the login state first is kicked offline. This is called kicked offline due to multi-instance login. If a1 is kicked offline, a logout process is executed in a1 and the KICKED_OUT event is triggered. You can listen for this event and redirect to the login page when triggered. At this time, a1 is offline, while a2 and a3 continue running.
API
chat.logout();
Parameters
None
Return value
Promise
Examples
let promise = chat.logout();
promise.then(function(imResponse) {
console.log(imResponse.data);// Logged out successfully
}).catch(function(imError) {
console.warn('logout error:', imError);
});

Terminating

You can terminate SDK instances. The SDK will first logout, then disconnect the WebSocket long connection, and finally release all resources.
API
chat.destroy();
Parameters
None
Return value
Promise
Examples
let promise = chat.destroy();

Login Settings

By default, multi-instance login is not supported. If you use an account that has been logged in on another page to log in on the current page, the account may be kicked offline on the other page, which will trigger the KICKED_OUT event. You can proceed accordingly after detecting the event through listening.
Below is an example:
let onKickedOut = function (event) {
console.log(event.data.type);
// TencentCloudChat.TYPES.KICKED_OUT_MULT_ACCOUNT (The user is forcibly logged out because the same account logs in from multiple webpages on the web client.)
// TencentCloudChat.TYPES.KICKED_OUT_MULT_DEVICE (The user is forcibly logged out because the same account logs in from multiple terminals.)
// TencentCloudChat.TYPES.KICKED_OUT_USERSIG_EXPIRED (The signature expired.)
// TencentCloudChat.TYPES.KICKED_OUT_REST_API (The user is forcibly logged out by the RESTful API.
)};
chat.on(TencentCloudChat.EVENT.KICKED_OUT, onKickedOut);