Mechanism of login user
1. Understand login mechanism
2. Encrypted user data generation session
preface
Tencent is the earliest and largest instant messaging developer in China. QQ and wechat have become essential applications for every Internet user. In line with the trend of digital transformation in the industry, Tencent will open its highly concurrent and reliable instant messaging capabilities. You can easily integrate instant messaging functions into the App according to the SDK provided by Tencent to meet various needs of your business.
According to the needs of developers at different stages and different scenarios, the instant messaging IM team provides a series of solutions, including SDK components of Android, iOS, Windows and Web, server-side integrated REST API interface, third-party callback interface, etc. Using these components and capabilities, developers can easily and quickly build highly reliable and stable instant messaging products to reach the world as they want.
Official development documents: Click go
Official example DEMO: Gitee
1, The meaning of UserSign
The UserSign carried by the user after logging in is the key information of the instantiation model operation information, which can be used as the token information of HTTP/HTTPS requests we are familiar with. If it fails or does not exist, tim instantiation interface will determine the invalid request and prompt that the current user is not logged in and cannot process the interface temporarily.
2, Generate UserSign
1. Import and storage
The code is as follows (example):
// IM Web SDK npm install tim-js-sdk --save // Upload plug-ins required to send pictures, files and other messages npm install tim-upload-plugin --save
2. Diagram
3. Instantiation model
//APP.VUE onLaunch(){ let that = this; let options = { SDKAppID:that.globalData.SDKAPPID }; let tim = TIM.create(options); //Initialize and generate tim instance console.log(tim,'instantiation ') tim.setLogLevel(1); //log level tim.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin }); //Listen tim not ready let onSdkNotReady = function (event) { console.log('tim not ready:', event); that.globalData.tim_ready = false; }; tim.on(TIM.EVENT.SDK_NOT_READY, onSdkNotReady); console.log(this.globalData.tim_ready); that.$tim = tim; }
The JS files required for page login articles are here
GenerateTestUserSig / / configure sdkappid & secret key for encryption method
Lib generate test usersig es.min.js / / encrypt the file
https://gitee.com/zhou_fan/tencent-im-demo?_from=gitee_search
legend
import { genTestUserSig } from "../../utils/GenerateTestUserSig.js"; // const TIM = require('../../utils/tim-wx.js'); import TIM from 'tim-js-sdk'; let onSdkReady; //Listen to tim ready //Sign in login: function (e) { let that = this; let userID = e; // Request the interface to get the UserSign of the user session let data = genTestUserSig(userID); //Generate userSig let promise = app.$tim.login({ userID: userID, userSig: data.userSig }); promise.then(function (imResponse) { //Login succeeded console.log('Login succeeded:', imResponse.data); that.login_user = userID; app.globalData.login_user = userID; app.globalData.tim_login_info = imResponse.data; //Listen to tim ready onSdkReady = function (event) { console.log('tim ready:', event); app.globalData.tim_ready = true; //Record tim ready uni.switchTab({url:"./index"}) }; app.$tim.on(TIM.EVENT.SDK_READY, onSdkReady); if (imResponse.data.repeatLogin === true) { //The identification account has been logged in. This login operation is repeated login. Support from v2.5.1 console.log('Repeat login:', imResponse.data.errorInfo); } }).catch(function (imError) { //Information about login failure console.warn('login error:', imError); }); },
The user's session needs to be maintained during business execution, and the ws link is normal
summary
1. Collect parameter sdkappid secret key
2. Generate usersign by encrypting parameters
3. Verify that the session token is valid
4. Execute login https://web.sdk.qcloud.com/im/doc/zh-cn/SDK.html#login