Created
June 3, 2018 11:19
-
-
Save xiaoysh8/e90d6bc4f4c1a3333a6c638a6c025464 to your computer and use it in GitHub Desktop.
laravel Create a Facebook Test App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Create a Facebook Test App | |
| After creating your Facebook app, you can connect it to your Laravel app by simply editing the | |
| config/services.php file. Add this: | |
| 1 'facebook' => [ | |
| 2 'client_id' => 'yourFacebookAppID', | |
| 3 'client_secret' => 'yourFacebookAppSecret', | |
| 4 'redirect' => 'http://yourLaravelAppURL/login/facebook/callback', | |
| 5 ], | |
| Open database/migrations/timestamps_create_users_table.php file and update the up method: | |
| 1 public function up() | |
| 2 { | |
| 3 Schema::create('users', function (Blueprint $table) { | |
| 4 $table->increments('id'); | |
| 5 $table->string('facebook_id')->unique(); | |
| 6 $table->string('name'); | |
| 7 $table->string('email')->unique(); | |
| 8 $table->string('password', 60); | |
| 9 $table->rememberToken(); | |
| 10 $table->timestamps(); | |
| 11 }); | |
| 12 } | |
| php artisan make:migration update_users_table | |
| Open the update_users_table file and update the code as follows: | |
| 1 <?php | |
| 2 | |
| 3 use Illuminate\Database\Schema\Blueprint; | |
| 4 use Illuminate\Database\Migrations\Migration; | |
| 5 | |
| 6 class UpdateUsersTable extends Migration | |
| 7 { | |
| 8 public function up() | |
| 9 { | |
| 10 if(Schema::hasColumn('users', 'facebook_id')) { | |
| 11 | |
| 12 } else { | |
| 13 Schema::table('users', function ($table) { | |
| 14 $table->string('facebook_id')->unique(); | |
| 15 }); | |
| 16 } | |
| 17 } | |
| 18 | |
| 19 public function down() | |
| 20 { | |
| 21 Schema::table('users', function ($table) { | |
| 22 $table->dropColumn('facebook_id'); | |
| 23 }); | |
| 24 } | |
| 25 } | |
| 1 protected $fillable = [ | |
| 2 'name', 'email', 'password', 'facebook_id', | |
| 3 ]; | |
| The redirectToFacebook method: | |
| 1 public function redirectToFacebook() | |
| 2 { | |
| 3 return Socialite::with('facebook')->redirect(); | |
| 4 } | |
| The getFacebookCallback method: | |
| 1 public function getFacebookCallback() | |
| 2 { | |
| 3 | |
| 4 $data = Socialite::with('facebook')->user(); | |
| 5 $user = User::where('email', $data->email)->first(); | |
| 6 | |
| 7 if(!is_null($user)) { | |
| 8 Auth::login($user); | |
| 9 $user->name = $data->user['name']; | |
| 10 $user->facebook_id = $data->id; | |
| 11 $user->save(); | |
| 12 } else { | |
| 13 $user = User::where('facebook_id', $data->id)->first(); | |
| 14 if(is_null($user)){ | |
| 15 // Create a new user | |
| 16 $user = new User(); | |
| 17 $user->name = $data->user['name']; | |
| 18 $user->email = $data->email; | |
| 19 $user->facebook_id = $data->id; | |
| 20 $user->save(); | |
| 21 } | |
| 22 | |
| 23 Auth::login($user); | |
| 24 } | |
| 25 return redirect('/')->with('success', 'Successfully logged in!'); | |
| 26 } | |
| 1 @section('content') | |
| 2 <div class="container"> | |
| 3 <div class="content"> | |
| 4 <div class="title">Home Page</div> | |
| 5 @if(!Auth::check()) | |
| 6 <div class="quote">Our Home page!</div> | |
| 7 @else | |
| 8 <div class="quote">You are now logged in!</div> | |
| 9 @endif | |
| 10 </div> | |
| 11 </div> | |
| 12 @endsection | |
| 1 Route::group(['middleware' => ['web']], function () { | |
| 2 Route::get('login/facebook', 'Auth\AuthController@redirectToFacebook'); | |
| 3 Route::get('login/facebook/callback', 'Auth\AuthController@getFacebookCallba\ | |
| 4 ck'); | |
| 5 | |
| 6 Route::get('/', function () { | |
| 7 return view('home'); | |
| 8 }); | |
| 9 | |
| 10 Route::get('/about', 'PagesController@about'); | |
| 11 Route::get('/contact', 'PagesController@contact'); | |
| 12 | |
| 13 Route::get('users/register', 'Auth\AuthController@getRegister'); | |
| 14 Route::post('users/register', 'Auth\AuthController@postRegister'); | |
| 15 | |
| 16 }); | |
| 1 <a href="/login/facebook"> <div class="btn btn-md btn-primary"> <i class="fa fa-\ | |
| 2 facebook"></i> Login with Facebook </div></a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment