Laravel 5.8 Socialite Login

There are many different sites and packages which we can integrate on our site to provide social login functionality. Laravel has released its own package name Socialite which we can use in our projects. Currently socialite support following social logins:

  • Facebook
  • Twitter
  • LinkedIn
  • Github
  • Google
  • Bitbucket
STEP 1 – Download Socialite Package
composer require laravel/socialite

STEP 2 – Configure Aliases and Providers in config/app.php
'providers' => [
// Other service providers…
Laravel\Socialite\SocialiteServiceProvider::class,
],

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

STEP 3 – Get secret id and secret key from facebook

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

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

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

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

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

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

OR
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
],
// AND DECLARE ITS VALUE IN .env file as:
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_CALLBACK_URL=

 

STEP 4 – User Model – Add New Fillable Column

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

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

STEP 5 -Migration table: Existing user Table

<?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');
}
}

STEP 6 -Install Authentication System

php artisan make:auth

STEP 7 – String to set default size of 191 – in app/providers/AppServiceProvider.php


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


STEP 8 – Migrtion – Execution

php artisan migrate

STEP 9 – Make a Route

Route::get('/auth/redirect/{provider}', 'SocialController@redirect');
Route::get('/callback/{provider}', 'SocialController@callback');

STEP 10 – Make Controller – SocialController

php artisan make:controller SocialController

STEP 11 – SocialController file

<?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;
 }
 }
 

STEP 12 – In Resources/Views/Auth/login.blade.php 

<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>
 
Note: MAKE SURE DATABASE SETTING IS DONE BEFORE YOU START FROM STEP 1.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password

 

Leave a Reply

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