Login (Web & Mini Programs)

Last updated: 2020-05-13 14:56:59

PDF

Login

You can send and receive messages in the Instant Messaging (IM) console only after logging in to the IM SDK. To log in to the IM SDK, you need to provide information including the UserID and UserSig. For more information, see Login Authentication. After successful login, to call APIs that require authentication, such as sendMessage, you must wait until the SDK enters the ready state. You can obtain the status of the SDK by listening on events. For more information, see TIM.EVENT.SDK_READY.

By default, multi-device 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 previous page may be forcibly logged out. When the previous page is forcibly logged out, the event TIM.EVENT.KICKED_OUT is triggered. You can proceed accordingly after detecting the event through listening. The following shows an example of listening on multi-device login:

let onKickedOut = function (event) {
  console.log(event.data.type); // mutipleAccount (The same account that is used to log in on multiple pages on the same device is forcibly logged out.)
};
tim.on(TIM.EVENT.KICKED_OUT, onKickedOut);

To support multi-device login (which means that the same account can be used to concurrently log in on multiple pages), log in to the Instant Messaging Console, and then click the SDKAppID of the desired app in My IM Apps. Choose Feature Configuration -> Login and Message. On the Login and Message page, click Edit in the Login settings area. Set Online Web Instances as required. The configuration will take effect within 50 minutes.

API name

tim.login(options)

Request parameters

Name Type Description
UserID String The ID of the user.
UserSig String The password with which the user logs in to the IM console. It is essentially the ciphertext generated by encrypting the information such as the UserID.
For the detailed generation method, see Generating UserSig.

Returned values

This API returns a Promise object.

Sample

let promise = tim.login({userID: 'your userID', userSig: 'your userSig'});
promise.then(function(imResponse) {
  console.log(imResponse.data); // Login succeeded.
}).catch(function(imError) {
  console.warn('login error:', imError); // Information about login failure.
});

Logout

This API is usually called when you switch between accounts. It clears the login status of the current account and all the data in the memory.

  • 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.
  • If the value of Online Web Instances configured in the Instant Messaging Console is greater than 1, and the same account has been used to log in to instances a1 and a2 (including Mini Program instances), after a1.logout() is executed, a1 is automatically logged out and cannot receive or send messages, whereas a2 is not affected.
  • Multi-device login: if 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 enters the login state is forcibly logged out. Assuming that a1 is forcibly logged out, a logout process is internally executed in a1 and the KICKED_OUT event is triggered. The access side can listen on this event and redirect to the login page when the event is triggered. In this case, instance a1 is forcibly logged out, whereas instances a2 and a3 can continue to run properly.

API name

tim.logout();

Request parameters

None.

Returned values

This API returns a Promise object. The callback functions are as follows:

  • The callback function parameter for then is IMResponse. IMResponse.data is a null object, indicating that logout succeeded.

  • The callback function parameter for catch is IMError.

Sample

let promise = tim.logout();
promise.then(function(imResponse) {
  console.log(imResponse.data); // Logout succeeded.
}).catch(function(imError) {
  console.warn('logout error:', imError);
});