¶ Authentication in Mini Program
In Mini Program, in addition to basic operations such as obtaining and modifying user information, logging in with email and mobile phone numbers, and adding user-defined fields, you can also use methods unique to the Mini Program environment such as Getting user mobile phone numbers through WeChat authorization, Login with WeChat authorization, and Login with WeChat-authorized mobile phone numbers.
¶ Configure Mini Program login in GenAuth
In order to use GenAuth Mini Program SDK in Mini Program, you need to apply for a Mini Program in WeChat Open Platform (opens new window) and fill in the configuration of the Mini Program in GenAuth console (opens new window).
For details, please see: [Configure applet login in GenAuth](/reference/sdk-for-wxapp.md#Configure applet login in -localeConfig-brandName-).
¶ Import GenAuth applet SDK
For detailed installation process, please see: Install applet SDK.
The first step is to initialize AuthenticationClient, which requires passing in AppId (application ID):
You can view your application list in Application in the console.
const { AuthenticationClient } = require("authing-wxapp-sdk");
const authing = new AuthenticationClient({
appId: "GEN_AUTH_APP_ID",
});
Next, you can use the AuthenticationClient method, such as using WeChat authorization login:
const { code } = await wx.login();
const data = await authing.loginByCode(code);
- If the user logs in to the mini program for the first time, and the user has not logged in using the WeChat application bound to the same subject as the mini program, a new account will be created.
- If the user logs in to the mini program for the first time, but the user has logged in using the WeChat application bound to the same subject as the mini program, the corresponding WeChat account will be returned.
For more information, see: Mini Program SDK.
