In this article, I am going to explain “How to create Login with Facebook in Laravel” , Currently login with facebook required in most of application.
Sometime you need to implement SignIn and Signup functionality using facebook API most of developer very tired with this problem, so we are make one very easy tutorial for login with facebook laravel and it share with you.
In laravel its not a big deal laravel’s creater Taylor Otwell is created one laravel package called laravel socialite for laravel devloper to integrate login with facebook laravel in any laravel application to very easy way.
So, we are share with you how to use laravel socialite package in very easy way for integrate login with facebook in laravel application. simple follow this step and easily integrate login with facebook in your laravel application.
Step 1:- Here, we are create new one laravel project by using collowing command
1 2 3 |
composer create-project --prefer-dist laravel/laravel loginwithfb |
Step 2:- Configure database setting in .env file like that
1 2 3 4 5 6 |
DB_HOST=localhost DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret |
Step 3:- After database setting install laravel/socialite package in your laravel application using given command
1 2 3 |
composer require laravel/socialite |
After installation socialite package configer socialite package like that.
Step 4:- Open your config/app.php file and set provider class
1 2 3 4 5 6 7 |
'providers' => [ ........ ........ Laravel\Socialite\SocialiteServiceProvider::class ], |
Step 5:- Open your config/app.php file and set this aliases
1 2 3 4 5 6 7 |
'aliases' => [ ........ ........ 'Socialite' => Laravel\Socialite\Facades\Socialite::class ], |
Step 6:- After configuration of socialite package you need to create facebook app and get facebook app client_id and client_secret.
Step 7:- Open your config/services.php file and set your fb configration
1 2 3 4 5 6 7 |
'facebook' => [ 'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'redirect' => env('CALLBACK_URL'), ], |
Step 8:- Open your .env file and set following values
1 2 3 4 5 |
FACEBOOK_CLIENT_ID=xxxxxxxxx FACEBOOK_CLIENT_SECRET=xxxxxxx CALLBACK_URL=http://localhost:8000/auth/facebook/callback |
Now create laravel auth using given below command
1 2 3 |
php artisan make:auth |
Now you not need to create new migration for it. simple open your uses table migration file which laravel bydefault provide
Simple open this file and put into some extra fields you want to like that…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?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('facebook_id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
Step 9:- Now create 2 following route in your routes/web.php file
1 2 3 4 |
Route::get('auth/facebook', 'Auth\LoginController@redirectToProvider'); Route::get('auth/facebook/callback', 'Auth\LoginController@handleProviderCallback'); |
Step 10:- Open your views/auth/login.blade.php file and add one extra button for login with facebook and set route.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading">Login</div> <div class="panel-body"> <form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}"> {{ csrf_field() }} <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> <label for="email" class="col-md-4 control-label">E-Mail Address</label> <div class="col-md-6"> <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus> @if ($errors->has('email')) <span class="help-block"> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> <label for="password" class="col-md-4 control-label">Password</label> <div class="col-md-6"> <input id="password" type="password" class="form-control" name="password" required> @if ($errors->has('password')) <span class="help-block"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <button type="submit" class="btn btn-primary btn-block"> Login </button> </div> </div> <div class="form-group"> <div class="col-md-8 col-md-offset-4"> <div class="checkbox"> <label> <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me </label> </div> <a class="btn btn-link" href="{{ route('password.request') }}"> Forgot Your Password? </a> </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <a href="{!! url('auth/facebook') !!}" class="btn btn-primary btn-block"> Login with facebook </a> </div> </div> </form> </div> </div> </div> </div> </div> @endsection |
Step 11:- Now open your app/Http/Controllers/Auth/LoginController.php file and put following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Socialite; use Auth; use Exception; use App\User; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function redirectToProvider() { return Socialite::driver('facebook')->redirect(); } public function handleProviderCallback() { try { $user = Socialite::driver('facebook')->user(); } catch (Exception $e) { return redirect('auth/facebook'); } $authUser = $this->findOrCreateUser($user); Auth::login($authUser, true); return redirect()->route('home'); } private function findOrCreateUser($facebookUser) { $authUser = User::where('facebook_id', $facebookUser->id)->first(); if ($authUser){ return $authUser; } return User::create([ 'name' => $facebookUser->name, 'email' => $facebookUser->email, 'facebook_id' => $facebookUser->id, 'avatar' => $facebookUser->avatar ]); } } |
I hope this article is helpful for facebook login in laravel…
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.