# Wechat Mini 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 identity provided by WeChat can be easily obtained, and an account system based on mobile phone numbers can be quickly established.

As follows:

# Ready to work

  1. Authing developer account
  2. Apply for WeChat Mini Program
  3. Fill in your applet information in the Authing background

# Apply for WeChat Mini Program

  1. First go to WeChat Open Platform (opens new window) to register a WeChat Mini Program development account -**If you need to get the user's mobile phone number, you need to pass WeChat authentication. ** -Add core.authing.cn to WeChat's request legal domain name:

  1. In the Authing console (opens new window), open the WeChat applet social login.
  • Get AppId and AppSecret of WeChat Mini Program

  • Fill in the AppId and AppSecret of the applet, and click Save.

# Install Authing SDK

Starting from 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](https://developers .weixin.qq.com/miniprogram/dev/devtools/npm.html).

# Install npm package

Use npm:

npm install authing-wxapp-sdk

Use yarn:

yarn add authing-wxapp-sdk

# Build npm in Mini Program Developer Tools

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

Check the Use npm module option:

# Initialize the Authing SDK

AuthenticationClient needs to pass in the user pool ID (userPoolId):

You can learn about How to get UserPoolId (opens new window) here. If you are not familiar with the concept of user pool, you can click here Understand the core concept of Authing system (opens new window).

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

const authing = new AuthenticationClient({
  userPoolId: "YOUR_USERPOOL_ID",
});

# Instructions

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

const { code } = await wx.login();
// No user authorization required
const user = await authing.loginByCode(code); // Successful login, write token to WeChat Storage

// You can do this after logging in
await authing.updateProfile((nickname: "Bob"));

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

// The request can be successful because the user is logged in.
await authing.updateProfile((nickname: "Mick"));

For details, please refer to the Authing applet SDK:

SDK for WeChat Mini Program