内容纲要

使用 Facebook 登录,在 https://developers.facebook.com/apps 创建一个新应用

在 Laravel 应用下安装
[code]
composer require laravel/socialite
[/code]

配置 config/app.php

[code language=”php”]
‘providers’ => [

….

Laravel\Socialite\SocialiteServiceProvider::class,

],

‘aliases’ => [

….

‘Socialite’ => Laravel\Socialite\Facades\Socialite::class,

],
[/code]

配置 config/services.php

[code language=”php”]
return [
….
‘facebook’ => [
‘client_id’ => env(‘FB_CLIENT_ID’),
‘client_secret’ => env(‘FB_CLIENT_SECRET’),
‘redirect’ => env(‘FB_CALLBACK_URL’),
],
]
[/code]

配置 .env

[code]
FB_CLIENT_ID=xxxxxxxxx
FB_CLIENT_SECRET=xxxxxxx
FB_CALLBACK_URL=http://localhost:8000/auth/facebook/callback
[/code]

修改 database/migrations 下 user 表

[code language=”php”]
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(’email’)->nullable();
$table->string(‘password’, 60)->nullable();
$table->string(‘provider’);
$table->string(‘provider_id’);
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(‘users’);
}
}
[/code]

[code]
php artisan migrate
[/code]

修改用户模型 app/User.php

[code language=”php”]
[…]
protected $fillable = [
‘name’, ’email’, ‘password’, ‘provider’, ‘provider_id’
];
[…]
[/code]

修改路由

[code language=”php”]
Route::get(‘auth/{provider}’, ‘Auth\SocialAuthController@redirect’);
Route::get(‘auth/{provider}/callback’, ‘Auth\SocialAuthController@callback’);
[/code]

修改登录控制器 `app/Http/Controllers/Auth/SocialAuthController.php`

[code language=”php”]
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Socialite;
use Exception;
use Auth;

class SocialAuthController extends Controller
{

/**
* Create a new controller instance.
*
* @return void
*/
public function redirect($provider)
{
return Socialite::driver($provider)->redirect();
}

/**
* Get the user info from provider and check if user exist for specific provider
* then log them in otherwise
* create a new user then log them in
* Once user is logged in then redirect to authenticated home page
*
* @return Response
*/
public function callback($provider)
{
try {
$user = Socialite::driver($provider)->user();
$input[‘name’] = $user->getName();
$input[’email’] = $user->getEmail();
$input[‘provider’] = $provider;
$input[‘provider_id’] = $user->getId();

$authUser = $this->findOrCreate($input);
Auth::loginUsingId($authUser->id);

return redirect()->route(‘home’);

} catch (Exception $e) {

return redirect(‘auth/’.$provider);

}
}
public function findOrCreate($input){
checkIfExist = User::where(‘provider’,$input[‘provider’])
->where(‘provider_id’,$input[‘provider_id’])
->first();

if($checkIfExist){
return $checkIfExist;
}

return User::create($input);
}
}
[/code]

修改视图 `resources/views/login.blade.php`

[code language=”html”]
[…]
<div class="container">
<div class="row">
<div class="col-md-12">
<a href="{{ url(‘auth/facebook’) }}" class="btn btn-primary">
<i class="fa fa-facebook"></i>Login With Facebook
</a>
</div>
</div>
</div>
[…]
[/code]

Try this..

发表评论

电子邮件地址不会被公开。 必填项已用*标注