Selain facebook, Socialite juga support
- Github
- Bitbucket
INSTALL SOCIALITE
Pertama install socialite
composer require laravel/socialite
atau lagkah alternatif, edit file composer.json, tambah pada require section
"laravel/socialite~2.0"
dan jangan lupa run composer update.
EDIT CONFIG/APP.PHP
Edit config/app.php, tambah code berikut pada bahagian array provider
Laravel\Socialite\SocialiteServiceProvider::class,
Tambah Socialide facade pada bahagian array aliases
'Socialite'=> Laravel\Socialite\Facades\Socialite::class,
CREATE FACEBOOK APP
Daftar app baru di https://developers.facebook.com.
Dapatkan
- API ID
- API SECRET
EDIT CONFIG/SERVICE.PHP
Tambah code berikut
'facebook' => [ 'client_id' =>'yourFacebookAppID', 'client_secret' =>'yourFacebookAppSecret', 'redirect' =>'http://yourLaravelAppURL/login/facebook/callback', ],
EDIT FAIL .ENV
'facebook' =[ 'client_id' =>env('FACEBOOK_ID'), 'client_secret' =>env('FACEBOOK_SECRET'), 'redirect' =>env('FACEBOOK_URL'), ],
CREATE/UPDATE TABLE USER
Untuk create table biasanya akan guna migration.
Biasanya untuk fresh installation dah ada migration create_users_table.
Cuma kena update sikit utk masukkan facebook unique id.
Edit database/migrations/timestamps_create_users_table.php
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('facebook_id')->unique(); $table->string('name'); $table->string('email')->unique(); $table->string('password', 60); $table->rememberToken(); $table->timestamps(); }); }
Untuk create table run command berikut
php artisan migrate
Kalau table user korang dah exist lain citer.
Kena create migration update_user_table.
Run command berikut, utk hasilkan migration table
php artisan make:migration update_users_table
edit database/migrations/timestamps_update_users_table.php
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class UpdateUsersTable extends Migration { 8 public function up(){ if(Schema::hasColumn('users', 'facebook_id')) { } else { Schema::table('users', function ($table) { $table->string('facebook_id')->unique(); }); } } public function down() { Schema::table('users', function ($table) { $table->dropColumn('facebook_id'); }); } }
UPDATE APP/USER.PHP
protected $fillable = [ 'name', 'email', 'password', 'facebook_id', ];
UPDATE ROUTE
Route::get('login/facebook', 'Auth\AuthController@redirectToFacebook'); Route::get('login/facebook/callback', 'Auth\AuthController@getFacebookCallback');
Route ini akan membawa (redirectToFacebook) kita ke facebook untuk login, dan kembali(getFacebookCallback) ke page pada host kita.
EDIT Auth/AuthController
Tambah method
- redirectToFacebook()
- getFacebookCallback()
public function redirectToFacebook() { return Socialite::with('facebook')->redirect(); } public function getFacebookCallback() { $data = Socialite::with('facebook')->user(); $user = User::where('email', $data->email)->first(); if(!is_null($user)) { Auth::login($user); $user->name = $data->user['first_name'].' '.$data->user['last_name']; $user->facebook_id = $data->id; $user->save(); } else { $user = User::where('facebook_id', $data->id)->first(); if(is_null($user)){ // Create a new user $user = new User(); //$user->name = $data->user['first_name'].' '.$data->user['last_name']; $user->name = $data->user['name']; $user->facebook_id = $data->id; $user->email = $data->email; $user->save(); } Auth::login($user); } return redirect('/')->with('success', 'Successfully logged in!'); }
Disebabkan class ini menggunakan Socialite dan Auth, sila tambah di atas class AuthController.
use Socialite; use Auth;
VISIT
Cuba visit http://cookbook.app/login/facebook
Kita akan dibawa ke page login FB, jika tiada masalah kita akan diredirect ke web kita semula.
EDIT HOMEPAGE VIEW
@extends('master') @section('title', 'Home') @section('content') <div class="container"> <div class="content"> @if(!Auth::check()) <div class="quote">Our Home page!</div> @else <div class="quote">You are now logged in!</div> @endif</div> </div> @endsection
Fungsi Auth::check() adalah untuk semak sama ada kita mempunyai access di Facebook atau tidak.
Edit Route.php
Untuk menggunakan Session dalam Laravel 5.2 perlu letakkan route dalam group web middleware seperti berikut
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ //Route::get('/', function () {return view('home'); Route::group(['middleware' => ['web']], function () { Route::get('/','PagesController@home'); Route::get('/about','PagesController@about'); Route::get('/contact','PagesController@contact'); Route::get('/test','PagesController@test'); Route::get('login/facebook', 'Auth\AuthController@redirectToFacebook'); Route::get('login/facebook/callback', 'Auth\AuthController@getFacebookCallback'); });
Leave a Reply