To get to Facebook API you have to create a Facebook App and specify the App ID and App Secret at the time of calling the Facebook API. Follow the step-by-step guide to create a Facebook App and generate App ID and Secret in the Facebook Developers Dashboard.
Go to that project and start the Laravel server by typing following command.
php artisan serve
Server starts at URL: http://localhost:8000
You need to configure your database in .env file.
Laravel ships with by default two tables. So run the following command in your terminal after setting up with the database.
php artisan migrate
Two tables of users and password_reset tables and one migration table generated in MySQL Database
Create a Laravel Authentication
Laravel gives us basic authentication. Laravel makes implementing authentication very simple. Truth be told, nearly everything is designed for you out of the box. We simply need to run one command in our terminal, and we are prepared to go.
php artisan make:auth
You can see in the terminal that “Authentication scaffolding generated successfully”
Now, if we switch to the routes => web.phpfile then there is one added route, and also our view is configured automatically.
<?php
// web.php
Auth::routes();
now run
php artisan serve
Add Facebook App Id
Navigate to the config => services.phpfile and put one another services array by the following code.
Your App ID becomes your client_id, and App Secret becomes client_secret
We need to implement two methods in this controller, which are following.
redirect(): Redirect our users to Facebook.
callback(): Handle callback from Facebook.
<?php
// FacebookLoginController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Socialite;
class FacebookLoginController extends Controller
{
/**
* Create a redirect method to facebook api.
*
* @return void
*/
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Return a callback method from facebook api.
*
* @return callback URL from facebook
*/
public function callback()
{
}
}
Next step is going to the routes => web.phpfile and register the routes.
<?php
//web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/redirect', 'FacebookLoginController@redirect');
Route::get('/callback', 'FacebookLoginController@callback');
Route::get('/home', 'HomeController@index')->name('home');
Create View file
Now we just need to add the link to our route redirect which will further redirect the user to the Facebook. So go to the resources => views => auth => login.blade.php and add one link like Login with Facebook.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNewColumnUserTable 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');
});
}
}
Run artisan command to migrate
php artisan migrate
Modify user modal as possible
<?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;
}
}
Update controller
<?php
// FacebookLoginController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Socialite;
class FacebookLoginController extends Controller
{
/**
Create a redirect method to facebook api.
*
@return void
*/
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
/**
Return a callback method from facebook api.
*
@return callback URL from facebook
*/
public function callback()
{
<strong>try</strong> {
$user = Socialite::driver('facebook')->user();
$create['name'] = $user->getName();
$create['email'] = $user->getEmail();
$create['facebook_id'] = $user->getId();
$userModel = <strong>new</strong> User;
$createdUser = $userModel->addNew($create);
Auth::loginUsingId($createdUser->id);
<strong>return</strong> redirect()->route('home');
} <strong>catch</strong> (Exception $e) {
<strong>return</strong> redirect('redirect');
}
}
}