laravel 怎么實(shí)現(xiàn)第三方登錄景描?——以QQ為例
- 例如我們需要qq的第三方登錄支持券时,首先需要升級(jí)sociaite,那么我們就需要安裝一個(gè)composer包伏伯,打開(kāi)laragon終端橘洞。執(zhí)行安裝命令。
composer require socialiteproviders/qq //后面的為key可以換成weibo等
- 跑完命令之后我們需要加入事件監(jiān)聽(tīng)器:在
app/Providers/EventServiceProvide.php
中protected $listen=[];
中加入:
Registered::class => [
SendEmailVerificationNotification::class,
],
'SocialiteProviders\Manager\SocialiteWasCalled' => [
'SocialiteProviders\QQ\QqExtendSocialite@handle',
],
];
- 去
.env
里面配置一下key
和secret
QQ_KEY=your key
QQ_SECRET=your secret
QQ_REDIRECT_URI=http://your-callback-url,
- 去
config/services.php
中加入
'client_id' => env('QQ_KEY'), // Your QQ Client ID
'client_secret' => env('QQ_SECRET'), // Your QQ Client Secret
'redirect' =>env('QQ_REDIRECT_URI'), //這個(gè)地址很重要
],
- 設(shè)計(jì)路由:在
web.php
當(dāng)中,思考:我們需要定義兩個(gè)路由说搅,一個(gè)是跳轉(zhuǎn)到qq登錄給予權(quán)限的頁(yè)面炸枣,和qq回退過(guò)來(lái)之后的頁(yè)面,路由設(shè)計(jì)如下:
Route::namespace('Auth')->prefix('auth/qq')->group(function () {
Route::get('/', 'SocialitesController@qq');
Route::get('callback', 'SocialitesController@callback');
});
前綴和命名空間可以隨意更改,打印路由終端命令:php artisan r:l
- 在
login.blade.php
當(dāng)中加入QQ登錄按鈕,給一個(gè)a
便簽加入上面設(shè)計(jì)的路由地址:<a href="/auth/qq></a>"
- 新建第三方登錄管理控制器弄唧,終端命令:
php artisan make:controller Auth\SocialitesController
- 打開(kāi)控制器輸入一下代碼:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Socialite;
use App\User;
use Illuminate\Support\Str;
class SocialitesController extends Controller
{
public function qq()
{
return Socialite::with('qq')->redirect();
}
//用戶授權(quán)后适肠,跳轉(zhuǎn)回來(lái)
public function callback()
{
$message= Socialite::driver('qq')->user();
dump($message);exit;
}
}
查看見(jiàn)qq返回過(guò)來(lái)的信息。
會(huì)得到如下信息候引。
<pre class="sf-dump" id="sf-dump-616717864" data-indent-pad=" " tabindex="0" style="display: block; white-space: pre-wrap; padding: 5px; overflow: initial !important; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 400 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; position: relative; z-index: 99999; word-break: break-all; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><abbr title="SocialiteProviders\Manager\OAuth2\User" class="sf-dump-note" style="text-decoration: none; border: none; cursor: pointer; color: rgb(18, 153, 218);">User</abbr> {#361 ▼ <samp data-depth="1" class="sf-dump-expanded">+accessTokenResponseBody: array:3 [?]
+token: "2B2A83C5B937C1139B03BBBBB7F2DAB6"
+refreshToken: "B553B05ECBC7981B8AE18C5E85117A68"
+expiresIn: "7776000"
+id: "4BB34E789377567E0323577F5233D7F2"
+nickname: "****"
+name: null
+email: null
+avatar: "http://thirdqq.qlogo.cn/g?b=oidb&k=tHUTU6J82j7LSnmjPUKUAw&s=100&t=1560692607"
+user: array:21 [▼ <samp data-depth="2" class="sf-dump-expanded">"ret" => 0
"msg" => ""
"is_lost" => 0
"nickname" => "***"
"gender" => "***"
"province" => "***"
"city" => "***"
"year" => "****"
"constellation" => ""
"figureurl" => "http://qzapp.qlogo.cn/qzapp/"
"figureurl_1" => "http://qzapp.qlogo.cn/qzapp/"
"figureurl_2" => "http://qzapp.qlogo.cn/qzapp/"
"figureurl_qq_1" => "http://thirdqq.qlogo.cn"
"figureurl_qq_2" => "http://thirdqq.qlogo.cn"
"figureurl_qq" => "http://thirdqq.qlogo.cn"
"figureurl_type" => "1"
"is_yellow_vip" => "0"
"vip" => "0"
"yellow_vip_level" => "0"
"level" => "0"
"is_yellow_year_vip" => "0"</samp> ]
+"unionid": ""</samp> }</pre>
這些就是QQ給你返回的用戶信息
我們需要把這些得到的信息選取有用字段插入到我們的users表中侯养。
- 檢查模型白名單:
protected $fillable = ['填寫要插入的字段名'];
- 將
dump
屏蔽改為以下代碼
$user = User::where('provider', 'qq')->where('uid', $info->id)->first();
//這里是判斷user是否存在,如果存在就直接登錄了
//不存在直接執(zhí)行插入
if (!$user) {
//直接插入數(shù)據(jù)庫(kù)的對(duì)應(yīng)字段澄干,注意寫白名單
$user = User::create([
'username'=>$message->nickname,
'provider' => 'qq', //寫死了逛揩,這里隨意
'uid' => $message->id,
'email' => 'qq+' . $message->id . '@qq.com', //這里拼的是個(gè)假的郵箱地址防止報(bào)錯(cuò),但要符合郵箱格式麸俘,做了驗(yàn)證也需要唯一
'password' => bcrypt(Str::random(10)), //這里也是設(shè)置了一個(gè)假的密碼辩稽,隨機(jī)生成一個(gè)10位的密碼,防止報(bào)錯(cuò)
'name' => $message->nickname,
'avatar' => $message->avatar,
]);
}
//Auth::login($user);
Auth::login($user, true);
return redirect('/admin'); //這里是個(gè)調(diào)轉(zhuǎn) 登錄完后跳轉(zhuǎn)到后臺(tái)首頁(yè)
- 以上搞完基本OK从媚、
'redirect' =>env('QQ_REDIRECT_URI'), //這個(gè)地址很重要
這個(gè)地址需要和你申請(qǐng)qq互聯(lián)應(yīng)用的那個(gè)回調(diào)地址對(duì)應(yīng)逞泄,并且訪問(wèn)也需要與前面的地址對(duì)應(yīng),不然會(huì)報(bào)no message
的錯(cuò)誤