Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access Wechat login in Mini Program with code

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use code to access WeChat login in Mini programs. The content of the article is of high quality, so Xiaobian shares it with you for reference. I hope you will have a certain understanding of relevant knowledge after reading this article.

Authing provides developers with a quick way to get user information and login in Mini programs through the SDK. Through Authing SDK, you can easily obtain the user ID provided by WeChat and quickly establish an account system based on mobile phone number.

Application scenario: Mini programs;

Overview: When used in Weixin Mini Programs (Mini), the WeChat authorization box pops up. After the user is authorized, the information of the current user can be obtained;

View official WeChat documentation.

Step 1: Create a Mini programs on WeChat public platform

Please go to the WeChat public platform to create a Weixin Mini Programs (Mini). You need to record the App ID and App Secret of the application, which you need to use 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.

Step 2: Configure Weixin Mini Programs on Authing Console

On the social login configuration page of the console, find the Weixin Mini Programs (Mini) application and fill in the following configuration:

App ID: App ID of Mini programs;

App Secret: Mini programs App Key.

After configuration, click OK to save the information.

Step 3: Start developing access

Starting with applet base library version 2.2.1 or above and developer tools version 1.02.1808300 or above, applets support installing third-party packages using npm, see:npm support for details| WeChat Open Document.

Install the npm package using npm:npm install authing-wxapp-sdk or yarn:yarn add authing-wxapp-sdk

Click on the menu bar in Developer Tools: Tools--> Build npm:

Check the "Use npm module" option:

initializing the SDK

AuthenticationClient initialization requires passing AppId:

You can view your list of apps in the apps section of the console.

const { AuthenticationClient } = require("authing-wxapp-sdk");const authing = new AuthenticationClient({ appId: "YOUR_APP_ID",});

Call login method

const { code } = await wx.login()//No user authorization required const user = await authing.loginByCode(code); //Login successfully, write token to WeChat Storage//Login to wait authing.updateProfile( nickname: 'Bob')

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

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

//The request can succeed because the user is logged in. await authing.updateProfile( nickname: 'Mick') Next

After obtaining the user information, you can get the user's identity credentials (token field of user information), and you can carry this token in the subsequent request sent by the client to the backend 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 });

In the backend interface, you need to verify the legitimacy of this token to verify the user's identity. For details of the authentication method, see the document "Verify User Identity Credentials (token)". After identifying the user, you may also need to manage the user's permissions to determine whether the user has permissions to operate the API.

About how to use code to access WeChat login in Mini programs, I hope the above content can help you to some extent and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report