Skip to content
Experts PHP
  • Web Tutorial Exercise
  • LARAVEL FRAMEWORK TUTORIAL
  • PYTHON TUTORIAL
  • LARAVEL 5.8
  • PHP TUTORIAL
  • HTML TUTORIAL
  • Mysql
  • C PROGRAM
  • PHP PROGRAM
Posted on February 17, 2019March 29, 2019 by jyoti

Login with Facebook in Laravel 5.7 Or Laravel 5.8 using Socialite Package

Hi Friends Today, I will tell you through this tutorial how you can login to the Laravel 5.7 Or Laravel 5.8 Framework by Facebook Social Media Website.

Also Learn

Login with Google in Laravel 5.7 Or Laravel 5.8 using Socialite Package

Friends Laravel has a very power full Framework. In today’s day more developer is using the Laravel Framework. Because most functinality is already inbuilt in this framework. We just have to manage it by command. So we were talking that in facebook laravel 5.7  Or Laravel 5.8 How to login.

So Friends, I would like to tell you that Laravel has given a package of itself. The name of this is socialite package. By installing this package in our application, we can easily login from Facebook. So let’s go, we will tell you step by step and together you will also explain the step of installetion.

Overview

  1. Make a project of Laravel 5.7 Or Laravel 5.8 Facebook Login.
  2. Create users table with column.
  3. Create a Laravel Authentication Using Auth Command.
  4. Install Socialite Package.
  5. Add Service Provider and Alias.
  6. Create Facebook App.
  7. Create Model and ADD Column.
  8. Create New Routes.
  9. Create New FacebookloginController.
  10. Create Blade File.

 

Step 1: Make a project of Laravel 5.7 Or Laravel 5.8 Facebook Login.

 

First of all we will install laravel 5.7 Or Laravel 5.8 application.

 

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

 

Step 2:then create users table with column.

 

CREATE TABLE `users` (
`id` int(100) NOT NULL,
`fname` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`facebook_id` varchar(100) NOT NULL,
`image` varchar(250) NOT NULL,
`password` varchar(100) NOT NULL,
`remember_token` varchar(100) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` int(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Step 3: Create a Laravel Authentication Using Auth Command

 

php artisan make:auth

Now, if we switch to the routes/web.php file then there is one added route, and also our view is configured automatically.

<?php 
// web.php
Auth::routes();

After running auth command, a folder of resources/views/auth / name will be created in your application. This will be two file inside the auth folder login.blade.php and register.blade.php

 

Install the laravel/socialite package for laravel 5.7 Or Laravel 5.8 Facebook Login

 

Step 4: Install Socialite Package

 

In this step we will learn to install socialite packege.

 

composer require laravel/socialite

 

Step 5: add service provider and alias.

 

After installing the Socialite Package, we will add the service of provider and alias to the config/app.php file.

 

'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
],

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

 

Step 6: Create Facebook App

 

In the third step you will have to create a facebook app account. You can create this by visiting this link of the Facebook app. After creating facebook account you will need cliend id and secret id.

You can create a client id and secret that you want to add to this config/services.php file. You can see below.

config/services.php

return [
....
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
],
]

 

Step 7: Create Model and ADD Column

 

app/User.php

<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'fname', 'email', 'password', 'facebook_id', 'image',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}

 

Step 8: Create New Routes

 

In this step we need to create routes for facebook login, so you need to add following route on bellow file.

route/web.php

Route::get('facebookpage', function () {
return view('facebookpage');
});

Auth::routes();
Route::get('auth/facebook', 'Auth\FacebookloginController@redirectToFacebook');
Route::get('auth/facebook/callback', 'Auth\FacebookloginController@handleFacebookCallback');

 

Step 9: Create New FacebookloginController

 

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 FacebookloginController.php file.

app/Http/Controllers/Auth/FacebookloginController.php

<?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()
{
  $userSocial = Socialite::driver('facebook')->user();
  //return $userSocial;
  $finduser = User::where('facebook_id', $userSocial->id)->first();
  if($finduser){
      Auth::login($finduser);
      return Redirect::to('/');
  }else{
  $new_user = User::create([
        'fname'      => $userSocial->name,
        'email'      => $userSocial->email,
        'image'  => $userSocial->avatar_original,
        'facebook_id'=> $userSocial->id
    ]);
    Auth::login($new_user);
    return redirect()->back();
}
}
}

1)redirect(): Redirect our users to the Facebook.

2) callback(): Handle callback from Facebook.

 

Step 10: Create Blade File

 

Ok, now at last we need to add blade view so first create new file facebooklogin.blade.php file and put bellow code:

resources/views/facebooklogin.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</strong>
</a> 
</div>
</div>
</div>
@endsection

 

Post navigation

How to Get the IP Address of any Client System by c# ?
Laravel Eloquent skip and take Query

Laravel Tutorial

  • Introduction
  • Types of Version in Laravel
  • Installation
  • Database Connection in Laravel
  • Laravel Application Structure
  • Laravel Route File
  • Middleware Create in Laravel
  • Controller Create in Laravel
  • Model Create in Laravel
  • Laravel Views
  • Laravel Migration
  • Authentication Create in Laravel
  • Namespace in Laravel
  • Include Function
  • CSRF Token
  • Insert query in Laravel by model
  • Update query in Laravel by model
  • Delete query in Laravel by model
  • Sort Query in laravel (Eloquent Sorting)
  • Limit Query Laravel
  • Join Queries Laravel
  • Count Query
  • Random Rows Ger Laravel Query
  • OFFSET with Limit Query
  • wherebetween Query
  • wherenotbetween Query

Calculators & Converter Tools

MD5 Hash Generator

Hypotenuse Calculator

Find and Replace Word Calculator

HTML Heading Tag Generator

Text Font Style Generator

AI Text Converter

Comma Remover Calculator

Comma Separator Calculator

Parentheses Removal Calculator

Slope Calculator & Converter

Temprature Converter

Speed Converter

What is My IP

Your Internet Speedtest

Ratio Calculator

Alexa Rank Checker

GST Calculator

Square(√) Root Calculator

Prime Numbers Calculator

Area of Circle Calculator

Percentage(%) Calculator

Hexa to Decimal Converter

Binary to Decimal Converter

HTML Minify Tools

Remove Blank Lines Minify Tools

Word Counter

Multiplication Calculator

Add Fraction Calculator

ODD Number Calculator

Even Number Calculator

Learn Programming Blog, Tutorials, PHP, MySQL, JavaScript & JQuery, Ajax, WordPress, Laravel, Web Development, Many Tools and Demos with Experts PHP.powered by Experts PHP

About us|Terms and Condition|Privacy Policy|Sitemap