PHPackages                             tourze/wechat-mini-program-auth-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. tourze/wechat-mini-program-auth-bundle

ActiveSymfony-bundle[Authentication &amp; Authorization](/categories/authentication)

tourze/wechat-mini-program-auth-bundle
======================================

微信小程序用户授权管理组件，支持 OAuth 登录和权限管理

2.0.0(4mo ago)09968MITPHPCI passing

Since May 10Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/tourze/wechat-mini-program-auth-bundle)[ Packagist](https://packagist.org/packages/tourze/wechat-mini-program-auth-bundle)[ RSS](/packages/tourze-wechat-mini-program-auth-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (64)Versions (9)Used By (8)

wechat-mini-program-auth-bundle
===============================

[](#wechat-mini-program-auth-bundle)

[![PHP Version](https://camo.githubusercontent.com/acffb6ae1962992d26e4466782832787e79504a6250f80d732c4283458b9f497/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e312d626c75652e737667)](https://www.php.net)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)[![Symfony](https://camo.githubusercontent.com/5deb666a7576e7c1fcbc7eaef09cf087fbbd97fc11e66d274daa2486350a5ba7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73796d666f6e792d253545362e342d626c75652e737667)](https://symfony.com)[![Build Status](https://camo.githubusercontent.com/280d6a8b0e729882b4a69c062537a84c6bdd8adb58c510e16ca48c5940a858ce/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f746573742e796d6c3f6272616e63683d6d6173746572)](https://github.com/tourze/php-monorepo/actions)[![Code Coverage](https://camo.githubusercontent.com/83f9bb21cdd80cfba1e605e63af9b92ee389ca4e847866e98494d2721727c718/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746f75727a652f7068702d6d6f6e6f7265706f2f6d6173746572)](https://codecov.io/gh/tourze/php-monorepo)

[English](README.md) | [中文](README.zh-CN.md)

WeChat Mini Program Authentication Bundle for Symfony

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [1. Code to Session](#1-code-to-session)
    - [2. Get Current User](#2-get-current-user)
    - [3. Upload Phone Number](#3-upload-phone-number)
- [Advanced Usage](#advanced-usage)
    - [Custom Event Handlers](#custom-event-handlers)
    - [Integration with User Management Systems](#integration-with-user-management-systems)
- [Entities](#entities)
- [Events](#events)
- [Procedures](#procedures)
- [Security](#security)
- [Error Handling](#error-handling)
- [Requirements](#requirements)
- [License](#license)

Features
--------

[](#features)

- WeChat Mini Program user authentication
- Code to session conversion
- User profile management
- Phone number binding and verification
- Data encryption/decryption service
- Event-driven architecture for customization
- Comprehensive logging for debugging

Installation
------------

[](#installation)

```
composer require tourze/wechat-mini-program-auth-bundle
```

Configuration
-------------

[](#configuration)

### 1. Register the Bundle

[](#1-register-the-bundle)

Register the bundle in your `config/bundles.php`:

```
return [
    // ...
    WechatMiniProgramAuthBundle\WechatMiniProgramAuthBundle::class => ['all' => true],
];
```

### 2. Configure Services

[](#2-configure-services)

The bundle provides auto-configuration for all services. Key services include:

- `EncryptService`: Handles WeChat data decryption
- `WechatTextFormatter`: Formats WeChat-specific text
- `UserService`: Manages WeChat Mini Program user creation and persistence
- `UserTransformService`: Transforms between WeChat users and system users

Usage
-----

[](#usage)

### 1. Code to Session

[](#1-code-to-session)

Convert WeChat authorization code to session:

```
use WechatMiniProgramAuthBundle\Procedure\WechatMiniProgramCodeToSession;

// Via JSON-RPC
$result = $procedure->execute([
    'code' => 'authorization_code',
    'rawData' => '{"nickName":"User",...}',
    'signature' => 'signature_string',
    'encryptedData' => 'encrypted_data',
    'iv' => 'initialization_vector'
]);
```

### 2. Get Current User

[](#2-get-current-user)

Get the currently authenticated WeChat Mini Program user:

```
use WechatMiniProgramAuthBundle\Procedure\GetCurrentWechatMiniProgramUser;

$user = $procedure->execute();
```

### 3. Upload Phone Number

[](#3-upload-phone-number)

Upload and bind user phone number:

```
use WechatMiniProgramAuthBundle\Procedure\UploadWechatMiniProgramPhoneNumber;

$result = $procedure->execute([
    'encryptedData' => 'encrypted_phone_data',
    'iv' => 'initialization_vector'
]);
```

Advanced Usage
--------------

[](#advanced-usage)

### Custom Event Handlers

[](#custom-event-handlers)

Listen to authentication events:

```
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WechatMiniProgramAuthBundle\Event\CodeToSessionResponseEvent;

class AuthenticationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CodeToSessionResponseEvent::class => 'onUserAuthenticated',
        ];
    }

    public function onUserAuthenticated(CodeToSessionResponseEvent $event): void
    {
        // Custom logic after user authentication
        $user = $event->getWechatUser();
        // ...
    }
}
```

### Integration with User Management Systems

[](#integration-with-user-management-systems)

Extend user repository for custom user creation:

```
use WechatMiniProgramAuthBundle\Repository\UserRepository;
use Tourze\UserServiceContracts\UserManagerInterface;

class CustomUserRepository extends UserRepository implements UserManagerInterface
{
    public function createUser(string $identifier, string $nickName, string $avatar): UserInterface
    {
        // Custom user creation logic
        return new CustomUser($identifier, $nickName, $avatar);
    }
}
```

Entities
--------

[](#entities)

The bundle provides the following entities:

- `User`: WeChat Mini Program user entity
- `AuthLog`: Authentication log records
- `CodeSessionLog`: Code to session conversion logs
- `PhoneNumber`: User phone number records

Events
------

[](#events)

The bundle dispatches the following events:

- `CodeToSessionRequestEvent`: Before code to session conversion
- `CodeToSessionResponseEvent`: After successful session creation
- `GetPhoneNumberEvent`: When retrieving phone number
- `ChangePhoneNumberEvent`: When changing phone number

Procedures
----------

[](#procedures)

Available JSON-RPC procedures:

- `WechatMiniProgramCodeToSession`: Convert authorization code to session
- `GetCurrentWechatMiniProgramUser`: Get current authenticated user
- `UploadWechatMiniProgramPhoneNumber`: Upload and bind phone number
- `ReportWechatMiniProgramAuthorizeResult`: Report authorization scope results

Security
--------

[](#security)

### Data Protection

[](#data-protection)

- All sensitive data is encrypted using WeChat's encryption standards
- Phone numbers are stored with proper validation and sanitization
- User tokens are managed securely with proper expiration

### Best Practices

[](#best-practices)

- Always validate WeChat signatures before processing data
- Use HTTPS for all communications with WeChat APIs
- Implement proper rate limiting for authentication endpoints
- Regularly audit authentication logs for suspicious activity

### Security Considerations

[](#security-considerations)

- Never store session keys in plain text
- Implement proper session management with appropriate timeouts
- Use environment variables for sensitive configuration
- Regularly update dependencies to patch security vulnerabilities

Error Handling
--------------

[](#error-handling)

The bundle provides custom exceptions:

- `DecryptException`: Data decryption failures
- `UserManagerNotAvailableException`: User manager service unavailable
- `SystemUserNotFoundException`: System user not found
- `UserRepositoryException`: User repository operation errors

Requirements
------------

[](#requirements)

- PHP 8.1+
- Symfony 6.4+
- Doctrine ORM 3.0+

License
-------

[](#license)

MIT License

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance73

Regular maintenance activity

Popularity14

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~32 days

Recently: every ~50 days

Total

8

Last Release

149d ago

Major Versions

0.0.4 → 1.0.02025-11-05

1.0.2 → 2.0.02025-12-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-wechat-mini-program-auth-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-wechat-mini-program-auth-bundle/health.svg)](https://phpackages.com/packages/tourze-wechat-mini-program-auth-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

7310.3k29](/packages/open-dxp-opendxp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
