Laravel 5.6 - Login with Facebook with Socialite
https://www.itsolutionstuff.com/post/laravel-56-login-with-facebook-with-socialiteexample.html
Laravel 5.6 - Login with Facebook with Socialite
In this tutorial, I would like to share with you how to login with facebook account on your laravel website. here I will use Socialite composer package for sign in with FB. so just follow bellow all step for authentication with Facebook in you laravel application.
In today's, Social authentication is important to implement on our website for increase traffic or making marketing, so you can connect with a Social network like facebook, twitter, google+, gitbub etc.
In this post i want to share with you how to do sign in with facebook and how to do sign up with facebook. Laravel 5.6 provide very easy way to implement login with your facebook account and register with your fb id. Laravel 5 provide us Socialite package that is help to social authentication. In this post we will make example same as like bellow preview and you can do that easily by using few following step:
Step 1: Install Socialite Package
In first step we will install Socialite Package that provide fb api to connect with facebook. So, first open your terminal and run bellow command:
After install above package we should add providers and aliases in config file, Now open config/app.php file and add service provider and alias.
config/app.php
Step 2: Create Facebook App
In this step we need facebook app id and secret that way we can get information of other user. so if you don't have facebook app account then you can create from here : https://developers.facebook.com/apps and after create account you can copy client id and secret.
Now you have to set app id, secret and call back url in config file so open config/services.php and .env file then set id and secret this way:
config/services.php
.env
Read Also: Laravel Google OAuth authentication using Socialite Package
Step 3: Create Migration and Model
In this step first we have to create migration for add facebook_id in your user table. so let's create new migration and bellow column this way:
Migration:
Now add addNew() in User model, that method will check if facebook id already exists then it will return object and if not exists then create new user and return user object. so open user model and put bellow code:
app/User.php
Step 4: Create New Routes
In this step we need to create routes for facebook login, so you need to add following route on bellow file.
routes/web.php
Step 5: Create New FacebookController
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 FacebookController.php file.
app/Http/Controllers/Auth/FacebookController.php
Step 6: Create Blade File
Ok, now at last we need to add blade view so first create new file facebook.blade.php file and put bellow code:
resources/views/facebook.blade.php
Read Also: Laravel 5.6 - User Roles and Permissions (ACL) using Spatie Tutorial
Ok, now you are ready to use open your browser and check here : URL + '/facebook'.
I hope it can help you.....
Last updated