Five lines of code to access WeChat login in a small program

Authing provides a way for developers to quickly obtain user information and complete login in a small program through the SDK. Through the SDK of Authing, the user identification provided by WeChat can be easily obtained, and an account system based on mobile phone numbers can be quickly established.

  • Application scenario: small program;

  • Overview: Used in the WeChat applet, the WeChat authorization box pops up, and the user can obtain the current user's information after authorization;

  • Check the official WeChat documentation.

Five lines of code to access WeChat login in a small program

Step 1: Create a small program on the WeChat public platform

Please go to the WeChat public platform guide to create a WeChat applet. You need to record the App ID and App Secret of the application, which will be used later. If you need to obtain the user's mobile phone number, you need to pass WeChat authentication. And add core.authing.cn to WeChat’s request legal domain name.

Five lines of code to access WeChat login in a small program

Step 2: Configure the WeChat applet application in the Authing console

On the social login configuration page of the console, find the WeChat applet application and fill in the following configuration:

  • App ID: Mini Program application ID;

  • App Secret: Mini program application secret.

After the configuration is complete, click "OK" to save the information.

Step 3: Start to develop access

Starting from the Mini Program Basic Library version 2.2.1 or above, and Developer Tools 1.02.1808300 or above, the Mini Program supports the use of npm to install third-party packages. For details, please see: npm Support | WeChat Open Document .

Install npm package

Use npm:

npm install authing-wxapp-sdk

Or use yarn:

yarn add authing-wxapp-sdk

Click the menu bar in the developer tools: Tools --\> Build npm:

Five lines of code to access WeChat login in a small program

Check the "Use npm module" option:

Five lines of code to access WeChat login in a small program

Initialize SDK

AuthenticationClient initialization needs to pass in AppId (application ID):

You can view your own application list in the application of the console.

const { AuthenticationClient } = require("authing-wxapp-sdk");

const authing = new AuthenticationClient({
  appId: "YOUR_APP_ID",
});

Invoke the login method

const { code } = await wx.login()
// 无需用户授权
const user = await authing.loginByCode(code); // 成功登录,将 token 写入微信 Storage
// 登录之后可以进行此操作
await authing.updateProfile(
  nickname: 'Bob'
)

After the user logs in, the SDK will write the user's token to WeChat's Storage, and subsequent requests will automatically carry the token for access.

Five lines of code to access WeChat login in a small program

Subsequent users open the applet again, and if the user's token is saved in the applet's Storage, the request to access authing will automatically carry the token.

// 该请求可以成功,因为该用户出于登录状态。
await authing.updateProfile(
  nickname: 'Mick'
)

For details, please refer to the document: Mini Program SDK ( https://docs.authing.cn/v2/reference/sdk-for-wxapp.html .

Next

After obtaining the user information, you can get the user's identity credential (the token field of the user information). You can carry this token in the subsequent request sent by the client to the back-end server. Take axios as an example:

const axios = require("axios");
axios
  .get({
    url: "https://yourdomain.com/api/v1/your/resources",
    headers: {
      Authorization: "Bearer YOUR_JWT_TOKN",
    },
  })
  .then((res) => {
    // custom codes
  });

The legitimacy of this token needs to be verified in the back-end interface to verify the user's identity. For details of the verification method, please refer to the document " Verify User Identity Credentials (token) ". After identifying the user, you may also need to perform permission management on the user to determine whether the user has operating permissions for this API.

Guess you like

Origin blog.51cto.com/14931994/2679565