安裝socialite包
composer require laravel/socialite
修改config/app.php
'providers' => [
// Other service providers...
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
//...
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]
數(shù)據(jù)庫
- 修改
database/migrations/create_users_table.php
乃正,或者添加新的migration進(jìn)行修改
$table->string('email')->unique()->nullable();
2.執(zhí)行php artisan make:model SocialAccount -m
生成Model和migrations
修改create_social_accounts_table
:
Schema::create('social_accounts', function (Blueprint $table) {
$table->integer('user_id');
$table->string('provider_user_id');
$table->string('provider');
$table->timestamps();
});
修改SocialAccount類
namespace App;
use Illuminate\Database\Eloquent\Model;
class SocialAccount extends Model
{
protected $fillable = ['user_id', 'provider_user_id', 'provider'];
public function user()
{
return $this->belongsTo(User::class);
}
}
運(yùn)行php artisan migrate
創(chuàng)建登陸Controller
php artisan make:controller SocialAuthController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\User as ProviderUser;
use Socialite;
class SocialAuthController extends Controller
{
public function redirect($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback($provider)
{
$user = $this->createOrGetUser(Socialite::driver($provider)->user(), $provider);
Auth::login($user);
return redirect()->to('/home');
}
private function createOrGetUser(ProviderUser $providerUser, $provider)
{
$account = SocialAccount::whereProvider($provider)
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user;
} else {
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => $provider
]);
$email = $providerUser->getEmail();
if ($email) {
$user = User::whereEmail($providerUser->getEmail())->first();
if (!$user) {
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'password' => bcrypt('123456')
]);
}
} else {
$user = User::create([
'name' => $providerUser->getName(),
'password' => bcrypt('123456')
]);
}
$account->user()->associate($user);
$account->save();
return $user;
}
}
}
路由
在route.php
(<=5.2) 或者web.php
(5.3) 中加入
Route::get('/{provider}/redirect', 'SocialAuthController@redirect')->name('redirect');
Route::get('/{provider}/callback', 'SocialAuthController@callback')->name('callback');
至此巩割,準(zhǔn)備工作都完成了走趋,接下來進(jìn)行各個(gè)平臺(tái)的oauth開發(fā)
第三方平臺(tái)
Facebook Login
打開Facebook開發(fā)者頁面(https://developers.facebook.com/)
創(chuàng)建一個(gè)Website應(yīng)用
輸入應(yīng)用名和類型沃呢,接著跳過Quick Start步驟。
點(diǎn)擊進(jìn)入應(yīng)用設(shè)置頁面君珠,添加website平臺(tái)囚企,并加入本地服務(wù)器用于測試
從該頁面取得client_id和client_secret吗讶,打開config/services.php
,添加:
'facebook' => [
'client_id' => '690344774435367',
'client_secret' => 'ebc50d3fd1d2f7286e02d247e5751ef4',
'redirect' => 'http://localhost:8000/facebook/callback',
],
在生產(chǎn)環(huán)節(jié)中曹抬,必須使用env()函數(shù)來獲取client_id和client_secret
在resources/views/auth/login.blade.php
中加入代碼:
<a href="route('redirect', 'facebook')">Facebook</a>
在windows環(huán)境下有可能會(huì)遇到**Error: [cURL error 60: SSL certificate in Laravel 5 while Facebook authentication] **
可以下載 cacert.pemhttps://gist.github.com/VersatilityWerks/5719158/download溉瓶,然后解壓拿到cacert.pem
放到任意位置,然后修改文件php.ini
:curl.cainfo = "path_to_cert\cacert.pem"
, 這里path_to_cert就是放置cacert.pem的路徑
這樣使用Facebook應(yīng)該就沒什么問題了谤民。
Github
Github的比較簡單堰酿,登陸github,打開Settings->Developer settings->OAuth applications张足,
然后創(chuàng)建新應(yīng)用即可
然后回到Laravel触创,只需在
config/services.php
中添加:
'github' => [
'client_id' => '690344774435367',
'client_secret' => 'ebc50d3fd1d2f7286e02d247e5751ef4',
'redirect' => 'http://localhost:8000/github/callback',
],
在resources/views/auth/login.blade.php
中加入代碼:
<a href="route('redirect', 'github')">Github</a>
完成!
登陸Google開發(fā)者为牍,進(jìn)入Google API Console[https://console.developers.google.com/apis/library]哼绑,
創(chuàng)建憑據(jù):
用于本地測試需要額外的步驟:
1.登陸(https://admin.google.com/) [Google Admin console]
2.點(diǎn)擊Applications > Others Google services > Google+ > avance settings.
3.激活A(yù)PI Google+
然后回來Laravel
config/services.php
中添加:
'google' => [
'client_id' => '763269637355-dj6d21427p78r17l4f263lq84p23as4q.apps.googleusercontent.com',
'client_secret' => 'ebc50d3fd1d2f7286e02d247e575xxxx',
'redirect' => 'http://localhost:8000/google/callback',
],
在resources/views/auth/login.blade.php
中加入代碼:
<a href="route('redirect', 'google')">Google</a>
完成