Laravel6 for third-party WeChat login

Keywords: Programming PHP Laravel Redis kafka

At present, many websites will have a lot of interactive functions, which reduces the user's operational difficulty, thus bringing the third-party login of WeChat to the project real-world function development.For the development content in this example, instead of using native content, use the encapsulated class libraries written by others directly.

1. Install laravel/socialite

composer require laravel/socialite

2). Add the following configuration information to your config/app.php file

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

2. Install socialiteProviders/weixin

1). Run the following command directly to install the Extension Pack

composer require socialiteproviders/weixin

2). Add the following configuration information to your config/app.php file

'providers' => [


3). Add the following event handlers to your app/Providers/EventServiceProvider.php file

protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [

3. Add Configuration

1). Add the following configuration to your.env file

WEIXIN_REDIRECT_URI=your callback address

2). Add the following configuration to your config/services.php file

'weixin' => [
   'client_id'     => env('WEIXIN_KEY'),
   'client_secret' => env('WEIXIN_SECRET'),
   'redirect'      => env('WEIXIN_REDIRECT_URI'),

   # This line configuration is important and must be written as this address.
   'auth_base_uri' => '',

Code Call

After all the preparations have been completed, the interface docking phase is now in progress.

/WeChat One-Click Login
Route::get('/weixin', 'WeixinController@weixin')->name('weixin');
Route::get('/weixin/callback', 'WeixinController@weixinlogin');

2). Add the following methods to your app/Http/Controllers/WeixinController.php file


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();
//        dd($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 . '',
                'password' => bcrypt(Str::random(60)),
                'avatar' => $user->avatar
        } else {
            $customer = $check;

        Auth::login($customer, true);
        return redirect('/');

Finally, the result of printing oauthUser after callback

The above content is intended to help you. Many PHPer s will encounter some problems and bottlenecks when they are advanced. Business code is written too much and has no sense of direction. I don't know where to start to improve. I have sorted out some data about it, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, SwooAdvanced and advanced dry goods needs such as le, Swoft, Kafka, Mysql optimization, shell script, Docker, micro-service, Nginx, etc. can be shared for free, need stamp here Free access to PHP Advanced Architect >> videos, interview documents

Posted by johnoc on Tue, 24 Mar 2020 19:58:08 -0700