Laravel 5.6 – Login with Facebook with Socialite
In this post we will give you information about Laravel 5.6 – Login with Facebook with Socialite. Hear we will give you detail about Laravel 5.6 – Login with Facebook with SocialiteAnd how to use it also give you demo for it if it is necessary.
Hello Artisan,
In this tutorial, I would like to share with you how to login with facebook account on your laravel website. here I will use Socialite composer package for sign in with FB. so just follow bellow all step for authentication with Facebook in you laravel application.
In today’s, Social authentication is important to implement on our website for increase traffic or making marketing, so you can connect with a Social network like facebook, twitter, google+, gitbub etc.
In this post i want to share with you how to do sign in with facebook and how to do sign up with facebook. Laravel 5.6 provide very easy way to implement login with your facebook account and register with your fb id. Laravel 5 provide us Socialite package that is help to social authentication. In this post we will make example same as like bellow preview and you can do that easily by using few following step:
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 bellow 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' => [
....
LaravelSocialiteSocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => LaravelSocialiteFacadesSocialite::class,
],
Step 2: Create Facebook App
In this step we need facebook app id and secret that way we can get information of other user. so if you don’t have facebook app account then you can create from here : https://developers.facebook.com/apps and after create account you can copy client id and secret.
Now you have to set app id, secret and call back url in config file so open config/services.php and .env file then set id and secret this way:
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
In this step first we have to create migration for add facebook_id in your user table. so let’s create new migration and bellow column this way:
Migration:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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() in User model, that method will check if facebook id already exists then it will return object and if not exists then create new user and return user object. so open user model and put bellow code:
app/User.php
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser 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 bellow file.
routes/web.php
Route::get('facebook', function () {
return view('facebook');
});
Route::get('auth/facebook', 'AuthFacebookController@redirectToFacebook');
Route::get('auth/facebook/callback', 'AuthFacebookController@handleFacebookCallback');
Step 5: Create New FacebookController
we need to add new controller and method of facebook auth that method will handle facebook callback url and etc, first put bellow code on your FacebookController.php file.
app/Http/Controllers/Auth/FacebookController.php
<?php
namespace AppHttpControllersAuth;
use AppUser;
use AppHttpControllersController;
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
Ok, now at last we need to add blade view so first create new file facebook.blade.php file and put bellow code:
resources/views/facebook.blade.php
@extends('layouts.app')
@section('content')
<div >
<div >
<div >
<a href="{{ url('auth/facebook') }}" >
<strong>Login With Facebook</strong>
</a>
</div>
</div>
</div>
@endsection
Ok, now you are ready to use open your browser and check here : URL + ‘/facebook’.
I hope it can help you…..
Hope this code and post will helped you for implement Laravel 5.6 – Login with Facebook with Socialite. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs