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
- Make a project of Laravel 5.7 Or Laravel 5.8 Facebook Login.
- Create users table with column.
- Create a Laravel Authentication Using Auth Command.
- Install Socialite Package.
- Add Service Provider and Alias.
- Create Facebook App.
- Create Model and ADD Column.
- Create New Routes.
- Create New FacebookloginController.
- 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