Login with Facebook In Laravel 6 -Example

Login with Facebook In Laravel 6 -Example

Overview

In this laravel login with facebook tutorial, we will discuss and show you how to implement facebook login with laravel applications. This tutorial shows you step by step how you can log in with facebook in your laravel application.

We will show you each things step by step an easy way. Just follow few steps and learn how to integrate Facebook social login with laravel.

We will discuss how to add the facebook social login button in your laravel project and how to simple authenticate (log in) users using a login button in our app. Also, we learn facebook register on the laravel app. We would love to show you each thing step by step about laravel facebook socialite.

Laravel Facebook Login Example

  • Install Fresh Laravel Setup
  • Setup Database
  • Download Socialite Package
  • Get the secret id and secret key
  • Make Route
  • Create Controller & Methods
  • Create Blade View
  • Run Development Server
  • Conclusion

1. Install Fresh Laravel App

First, we need to download the latest laravel fresh setup. Use the below command and download fresh new laravel setup :

composer create-project --prefer-dist laravel/laravel blog

2. Setup Database

After successfully install laravel latest Application, Go to your project .env file and set up database credential :

  DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

3. Download Socialite Package

Now In this step, we will install the socialite package for facebook login. You can use the below command and install this laravel socialite package :

composer require laravel/socialite

After successfully install socialite package, we need to configure the aliases and provider in config/app.php :

'providers' => [
// Other service providers…
Laravel\Socialite\SocialiteServiceProvider::class,
],

'aliases' => [
// Other aliases…
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],

4. Get secret id and secret key from facebook

First of all, We need to required the CLIENT ID and CLIENT SECRET to add social facebook login button in the laravel based project, Lets go to https://developers.facebook.com/apps/ and create a new app. We have put an app name, email on this page for creating an app.

create facebook app with laravel

After create the facebook app, go to setting->advanced and set redirect url like below :

get credential with facebook

Last step, we need to set valid auth redirect url, click facebook login->setting and set valid auth redirect url.

laravel facebook

You will go to your facebook developers dashboard and copy the following App ID and App SECRET.

facebook app id and app secret

After successfully create an app in facebook and get credentials from facebook developers dashboard, Set client id and client secret config/service.php file :

'facebook' => [
'client_id' => 'xxxx',
'client_secret' => 'xxx',
'redirect' => 'https://www.tutsmake.com/laravel-example/callback/facebook',
],

Go to app/User.php and set fillable property put the below code here :

protected $fillable = [
'name', 'email', 'password', 'provider', 'provider_id'
];

Next step, Go to app/database/create_users_table.php and put the below code here :

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
Run the migrations.
*
@return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique()->nullable();
$table->string('provider');
$table->string('provider_id');
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken()->nullable();
$table->timestamps();
});
}
/**
Reverse the migrations.
*
@return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

Next step, we need to create new blade views for login, registration, forget the password and reset password. So visit this link for new laravel auth scaffolding.

Before we run PHP artisan migrate command go to app/providers/AppServiceProvider.php and put the below code :

 

use Illuminate\Support\Facades\Schema;
….
function boot()
{
Schema::defaultStringLength(191);
}

Next migrate the table using the below command :

php artisan migrate

5. Make Route

We will create two routes in the web.php file. Go to app/routes/web.php file and create two below routes here :

  Route::get('/auth/redirect/{provider}', '[email protected]');
Route::get('/callback/{provider}', '[email protected]');

6. Create Controller

We need to create a controller name SocialController. Use the below command and create a Controller.

php artisan make:controller SocialController

After successfully create controller go to app/controllers/SocialController.php and put the below code.

<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use Validator,Redirect,Response,File;
 use Socialite;
 use App\User;
 class SocialController extends Controller
 {
 public function redirect($provider)
 {
     return Socialite::driver($provider)->redirect();
 }
 public function callback($provider)
 {
   $getInfo = Socialite::driver($provider)->user(); 
   $user = $this->createUser($getInfo,$provider); 
   auth()->login($user); 
   return redirect()->to('/home');
 }
 function createUser($getInfo,$provider){
 $user = User::where('provider_id', $getInfo->id)->first();
 if (!$user) {
      $user = User::create([
         'name'     => $getInfo->name,
         'email'    => $getInfo->email,
         'provider' => $provider,
         'provider_id' => $getInfo->id
     ]);
   }
   return $user;
 }
 }
<hr>
<div class="form-group row mb-0">
 <div class="col-md-8 offset-md-4">
    <a href="{{ url('/auth/redirect/facebook') }}" class="btn btn-primary"><i class="fa fa-facebook"></i> Facebook</a>
</div>
</div>

In Project, go to Resources/Views/Auth/login.blade.php and add a facebook social login button :

<hr>
<div class="form-group row mb-0">
 <div class="col-md-8 offset-md-4">
    <a href="{{ url('/auth/redirect/facebook') }}" class="btn btn-primary"><i class="fa fa-facebook"></i> Facebook</a>
</div>
</div>

7. Run Development Server

We need to start the development server. Use the PHP artisan serve command and start your server :

 php artisan serve

Now we are ready to run our LinkedIn login laravel 6 examples so run bellow command to quick run.

 http://127.0.0.1:8000/login
 Or direct hit in your browser
 http://localhost/blog/public/login

Now we are ready to run our facebook login example so run bellow command to quick run.

Conclusion

In this laravel facebook social login tutorial, We have successfully added a login button on our laravel based project and using the facebook button. We have also implemented register code in this example and learn how to users log in with facebook in Laravel.

You may like

  1. Laravel 6 Twitter Login Example Using Socialite Package
  2. Laravel 6 Linkedin Login Tutorial With Live Demo
  3. Github Login in laravel 6 with Example
  4. Laravel 6 Socialite Google Login Example
  5. Laravel 6 Multi Auth( Authentication) Example Tutorial
  6. Laravel 6 Custom Login Registration Example Tutorial
  7. Laravel 6 REST API (Login & Registration) Authentication

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *