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 in configuration 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);
Parameter
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.
Returned value
Promise
Sample
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 login user information.

API
chat.getLoginUser();
Parameter
None
Returned value
String
Sample
const userID = chat.getLoginUser();

Get server time

API
chat.getServerTime();
Parameter
None
Returned value
Number
Sample
const serverTime = chat.getServerTime();

Whether 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();
Parameter
None
Returned value
Boolean
Sample
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 automatically logged out and cannot receive or send messages.
2. Assume that the value of the Web configuration item in the Chat Console is greater than 1, and the same account has been used to log in to instances a1 and a2. After a1.logout() is executed, a1 will be automatically logged out and cannot receive or send messages, while a2 will not be affected.
3. Assume that the Online Web Instances is set to 2, and your account has been used to log in to instances a1 and a2. When you use this account to log in to instance a3, either a1 or a2 will be forcibly logged out. In most cases, the instance that first entered the login state is forcibly logged out. This is called kicked offline due to multi-account login. If a1 is forcibly logged out, a logout process is executed within a1 and the KICKED_OUT event is triggered. You can listen for this event and redirect to the login page when the event is triggered. At this time, a1 is forcibly logged out, whereas a2 and a3 can continue to run properly.
API
chat.logout();
Parameter
None
Returned value
Promise
Sample
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, including logout, disconnecting persistent connection, and releasing all resources.
API
chat.destroy();
Parameter
None
Returned value
Promise
Sample
let promise = chat.destroy();

Login Settings

By default, multi-account 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 forcibly logged out 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);