Implementing Third-Party WeChat Login in Laravel with Socialite

This tutorial walks through installing Laravel Socialite and the WeChat provider, configuring environment and service files, adding routes and controller methods, and handling user authentication to enable seamless third‑party WeChat login in a Laravel application.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Implementing Third-Party WeChat Login in Laravel with Socialite

This guide demonstrates how to add WeChat third‑party login to a Laravel project using the Socialite package.

1. Install Socialite composer require laravel/socialite 2. Register the provider and alias in config/app.php:

'providers' => [
    Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],

3. Install the WeChat provider : composer require socialiteproviders/weixin Then add the provider class to config/app.php:

'providers' => [
    SocialiteProviders\Manager\ServiceProvider::class,
],

Register the event listener in app/Providers/EventServiceProvider.php:

protected $listen = [
    SocialiteProviders\Manager\SocialiteWasCalled::class => [
        'SocialiteProviders\Weixin\WeixinExtendSocialite@handle',
    ],
];

4. Add configuration :

In .env set WEIXIN_KEY, WEIXIN_SECRET and WEIXIN_REDIRECT_URI (do not prepend http:// to the redirect URI).

In config/services.php add:

'weixin' => [
    'client_id' => env('WEIXIN_KEY'),
    'client_secret' => env('WEIXIN_SECRET'),
    'redirect' => env('WEIXIN_REDIRECT_URI'),
    'auth_base_uri' => 'https://open.weixin.com/connect/qrconnect',
],

5. Define routes in routes/web.php:

// Initiate WeChat login
Route::get('/weixin', 'WeixinController@weixin')->name('weixin');

// Callback after authorization
Route::get('/weixin/callback', 'WeixinController@weixinlogin');

6. Implement controller app/Http/Controllers/WeixinController.php with two methods:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class WeixinController extends Controller
{
    public function weixin()
    {
        return Socialite::with('weixin')->redirect();
    }

    public function weixinlogin()
    {
        $user = Socialite::driver('weixin')->user();
        $check = User::where('uid', $user->id)
                     ->where('provider', 'qq_connect')
                     ->first();
        if (! $check) {
            $customer = User::create([
                'uid' => $user->id,
                'provider' => 'qq_connect',
                'name' => $user->nickname,
                'email' => 'qq_connect' . $user->id . '@example.com',
                'password' => bcrypt(Str::random(60)),
                'avatar' => $user->avatar,
            ]);
        } else {
            $customer = $check;
        }
        Auth::login($customer, true);
        return redirect('/');
    }
}

After the callback, the authenticated user information can be accessed, completing the WeChat login integration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPWeChatOAuthLaravelSocialite
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.