Laravel6 achieve third-party micro letter Login

At present, many of the site there will be a lot of interactive features, thereby reducing the difficulty of the operation of the user, hereby bring the project to develop the actual functionality of third-party micro letter login. For the development of the present example, the use of the content is not native, but directly written using someone else packaged library.

1. Installation laravel / socialite

composer require laravel/socialite

2) In your  config/app.php add the following configuration information file

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

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

2. Installation socialiteProviders / weixin

1) Direct Expansion Pack run the following command to install

composer require socialiteproviders/weixin

2) In your  config/app.php add the following configuration information file

'providers' => [

     \SocialiteProviders\Manager\ServiceProvider::class,
],

3) In your  app/Providers/EventServiceProvider.php add the following event handler file

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

3. Add Configuration

1) In your  .env add the following configuration file

WEIXIN_KEY = your AppID 
WEIXIN_SECRET = your appsecret 
WEIXIN_REDIRECT_URI = your callback address

2) In your  config/services.php add the following configuration file

'Weixin' => [
    'client_id' => the env ( 'WEIXIN_KEY'), 
   'client_secret' => the env ( 'WEIXIN_SECRET'), 
   'the redirect' => the env ( 'WEIXIN_REDIRECT_URI'), # This line configuration is very important and must be to write this address. 
   'auth_base_uri' => 'https://open.weixin.qq.com/connect/qrconnect', 
] ,

   

Code calls

After preparations have been made, now on to the interface, docking phase. 

// WeChat a key to register 
the Route :: GET ( '/ Weixin', '@ Weixin WeixinController') -> name ( 'Weixin' ); 
the Route :: GET ( '/ Weixin / the callback', 'WeixinController weixinlogin @');

2) In your  app/Http/Controllers/WeixinController.php add the following method file

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

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

The final step is to print callback  oauthUser results  

 

 

 If you have thought about operational problems, I want to communicate work issues, and can be added to the code: 647 617 935

Guess you like

Origin www.cnblogs.com/winner192/p/11827953.html
Recommended