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.
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:
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.
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…..