Skip to content

Webtuts

Online Web Tutorial

  • Laravel
  • Laravel 8
  • jQuery
  • CodeIgniter
  • CodeIgniter 4
Login with Facebook in Laravel

How to Integrate Login with Facebook in Laravel

September 4, 2021 web-tuts Laravel

In this tutorial, I would like to share with you how to login with facebook in laravel website. here I will use the login with facebook in laravel Socialite developer package to log in via FB. so just follow the cover every step of Facebook verification for you laravel app.

Today, social validation is important to use on our website to increase traffic or do marketing, so you can connect with social networking sites such as facebook, twitter, google +, gitbub etc.

In this post I want to share with you how to integrate login with facebook in laravel and how to sign up with facebook. Laravel 5.6 provides the easiest way to use to login with your facebook account and register with your fb id. Laravel 5 offers us a Socialite package that is useful for public verification. In this post we will make an example like bellow preview and you can do that easily by using the following few steps:

Integrate Login with Facebook in Laravel

Step 1: Install Socialite Package

In first step we will install Socialite Package that provide fb api to connect with facebook. So, first open your terminal and run follow command:

composer require laravel/socialite

After install above package we should add providers and aliases in config file, Now open config/app.php file and add service provider and alias.

Read Also :  Laravel 8 Multi Step Form Example

config/app.php

'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],

'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],

Step 2: Create Facebook App

In this step we need the facebook app id and password so we can get another user’s information. so if you do not have a facebook app account you can create from here: https://developers.facebook.com/apps and after creating an account you can copy the client id and privacy.

You should now set the app id, privacy and call url in the configuration file to open the config / services.php and .env file and set the id and private as follows:

config/services.php

return [
....
 'facebook' => [
  'client_id' => env('FACEBOOK_CLIENT_ID'),
  'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
  'redirect' => env('FACEBOOK_CALLBACK_URL'),
 ],
]

.env

FACEBOOK_CLIENT_ID=xxxxxxxxx
FACEBOOK_CLIENT_SECRET=xxxxxxx
FACEBOOK_CALLBACK_URL=http://localhost:8000/auth/facebook/callback

Step 3: Create Migration and Model

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNewColunmUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */

public function up()
{
Schema::table("users", function (Blueprint $table) {
 $table->string('facebook_id');
});
}
/**
 * Reverse the migrations.
 *
 * @return void
 */

public function down()
{
Schema::table("users", function (Blueprint $table) {
 $table->dropColumn('facebook_id');
});
}
}

Now add addNew () to the user model, that method will check if the facebook id already exists and will retrieve the item and if not, then create a new user and retrieve the user item. open user model and enter bold code:

Read Also :  Laravel Arr shuffle() function Example

app/User.php

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
'name', 'email', 'password', 'facebook_id'
];
/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */

protected $hidden = [
'password', 'remember_token',
];

public function addNew($input)
{
 $check = static::where('facebook_id',$input['facebook_id'])->first();
if(is_null($check)){
return static::create($input);
}
return $check;
}
}

Step 4: Create New Routes

In this step we need to create routes for facebook login, so you need to add following route on follow file.

routes/web.php

Route::get('facebook', function () {
return view('facebook');
});

Route::get('auth/facebook', 'Auth\FacebookController@redirectToFacebook');
Route::get('auth/facebook/callback', 'Auth\FacebookController@handleFacebookCallback');

Step 5: Create New FacebookController

we need to add a new controller and facebook auth method and that method will manage the facebook callback url and so on, start by entering the bellow code in your FacebookController.php file.

Read Also :  Laravel 8 One to Many Eloquent Relationship Tutorial

app/Http/Controllers/Auth/FacebookController.php

<?php

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

class FacebookController extends Controller
{

/**
 * Create a new controller instance.
 *
 * @return void
 */

public function redirectToFacebook()
{
return Socialite::driver('facebook')->redirect();
}

/**
 * Create a new controller instance.
 *
 * @return void
 */

public function handleFacebookCallback()
{
try {
 $user = Socialite::driver('facebook')->user();
 $create['name'] = $user->getName();
 $create['email'] = $user->getEmail();
 $create['facebook_id'] = $user->getId();
 $userModel = new User;
 $createdUser = $userModel->addNew($create);
Auth::loginUsingId($createdUser->id);
return redirect()->route('home');
} catch (Exception $e) {
return redirect('auth/facebook');
}
}
}

Step 6: Create Blade File

resources/views/facebook.blade.php

@extends('layouts.app')
@section('content')

<div class="container">
<div class="row">
<div class="col-md-12 row-block">
 <a href="{{ url('auth/facebook') }}" class="btn btn-lg btn-primary btn-block">
  <strong>login with facebook in laravel</strong>
 </a>
</div>
</div>
</div>

@endsection

I hope it can help you…..

Related posts:

  • Laravel 7 Socialite Login with Google Account Example
  • Laravel 7 Login with Github Example
  • Laravel 7 Login with Linkedin using Socialite
  • Laravel Firebase Push Notification Example
  • How to Flash Message in Laravel Example
  • Laravel Google Pie Chart Example
  • How to Generate Dynamic PDF from HTML in Laravel
  • Laravel Livewire Dependant Dropdown Example
  • Google Line Chart Example in Laravel
  • Laravel Dynamic Google Pie Chart Example
ExampleLaravelLaravel Login with Facebook SocialiteLogin with Facebook

Post navigation

Previous Post:PayPal Integration in Laravel Example
Next Post:Laravel Firebase Push Notification Example

Recent Posts

  • How To Seed Data In Laravel 9
  • Marge Multiple PDF In Laravel 9
  • Laravel 9 CRUD Operation Tutorial For Beginner
  • Laravel 9 Generate PDF File using DomPDF
  • Laravel 9 Mail | Laravel 9 Send Email Example
  • Laravel 9 Create Zip File And Download
  • Laravel 9 Socialite Login with Google Example
  • Laravel 9 Image Upload Example
  • How to Get Client IP Address in Laravel 9?
  • Laravel 8 Print Last Query Example
  • Laravel Select with Sum Query Example
  • Laravel 8 Auth Login with Username or Email
  • Laravel 8 Find Nearest Location By Latitude and Longitude
  • 404 Not Found but Route Exist in Laravel
  • Laravel 8 Clear Cache of Route, View & Config
  • Laravel 8 Change Password with Current Password Validation
  • Laravel Carbon Get First Day of Specific Month Example
  • Laravel 8 Database Seeder Example
  • Laravel Eloquent doesntHave() Condition Example
  • Restore Back Deleted Records in Laravel 8
  • Laravel 8 Multiple Database Connections
  • Laravel 8 Download File Example
  • PHP Array Push Key Value Example
  • How To Add Days To Current Date In Laravel Blade
  • Moment JS Get Last Date of Month Example
  • Laravel Carbon Convert String to Date Example
  • Moment Get Current Week Number Example
  • Laravel Carbon Get Year from Date Example
  • JavaScript Array Remove Element
  • Laravel Carbon Get Last Day of Month Example
  • How To Validate Upload File Type Using JavaScript
  • How to Create Multilingual Website in Laravel 8
  • Laravel 8 Automatic Database Encryption Decryption
  • How To File Upload In CodeIgniter 4 Example
  • How to Get Month Name from Date in Laravel 8
  • CRUD with Image Upload in Laravel 8 Example
  • CodeIgniter 4 Watermark Image Example
  • jQuery Get All Checked Checkbox Values in Array
  • CodeIgniter 4 Google Bar & Line Charts Example
  • Codeigniter 4 Ajax Image Upload With Preview Example
  • Space Not Allowed Validation in Laravel Example
  • Laravel 8 View Render Example
  • Laravel 8 Form Validation Example
  • Laravel 8 Signature Pad Example
  • Laravel 8 cURL HTTP Request Example
  • Laravel 8 Datatables Filter with Dropdown
  • Laravel 8 Pagination Example
  • Laravel 8 QR Code Generate Example
  • Laravel 8 Highcharts Example
  • Laravel 8 Multi Step Form Example
© Copyright 2020 Web-tuts.com. All rights reserved. Home Privacy Policy Contact Us Terms of Use