Home > Article > Web Front-end > Tips for using WeChat mini program login authentication
This time I will bring you your WeChat mini program login authentication usage skills. What are the precautions when using your WeChat mini program login authentication? The following is a practical case, let’s take a look.
Preface
In order to facilitate mini program applications to use the WeChat login state for authorized login, the WeChat mini program provides an open interface for login authorization. At first glance, I feel that the document is very reasonable, but when it comes to implementation, it is really confusing and I don’t know how to manage and maintain the login state. This article will teach you step by step how to access and maintain the WeChat login status in business. I won’t go into more details below. Let’s take a look at the detailed introduction.
Access process
The flow chart in the official document here is clear enough, so we will directly elaborate and supplement it.
First of all, when you see this picture, you will definitely notice that the mini program communicates and interacts not only with the mini program front end and our own server, but also with the WeChat third-party server Also involved, so what role does the WeChat server play in it? Let’s go through the login authentication process together and we’ll understand.
1. Call wx.login to generate code
wx.login() The function of this API is to generate a temporary login for the current user Credentials, this temporary login credential is only valid for five minutes. After we get this login credentials, we can proceed to the next step: Get openid and session_key
wx.login({ success: function(loginRes) { if (loginRes.code) { // example: 081LXytJ1Xq1Y40sg3uJ1FWntJ1LXyth } } });
2. Get openid and session_key
Let’s first introduce openid. Children’s shoes who have used public accounts should be familiar with this logo. In the public platform, it is used to identify each user’s subscription account, service account, and mini program. A unique identifier for different applications, that is to say, the openid of each user in each application is inconsistent, so in the mini program, we can use openid to identify the uniqueness of the user.
So what is session_key used for? With the user ID, we need to let the user log in, then the session_key ensures the validity of the current user's session operation. This session_key is distributed to us by the WeChat server. In other words, we can use this identifier to indirectly maintain the login status of our applet users. So how did we get this session_key? We need to request the third-party interface provided by WeChat on our own server https://api.weixin.qq.com/sns/jscode2session. This interface needs to bring four parameter fields:
Parameters | Value |
---|---|
Appid## of the applet | |
The secret of the mini program | |
The code sent by the previous call to wx.login | |
'authorization_code' |
The above is the detailed content of Tips for using WeChat mini program login authentication. For more information, please follow other related articles on the PHP Chinese website!