Laravel socialite is a package, which is help you to login or register your user with google, facebook, linkined, github easily. Follow below process for setup socialite in laravel project.
composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run dev
$table->string('provider')->nullable();
$table->string('provider_id')->nullable();
php artisan migrate
'google' => [
'client_id' => '***************************************.apps.googleusercontent.com',
'client_secret' => '***************************',
'redirect' => 'http://localhost:8000/callback/google',
],
'facebook' => [
'client_id' => '****************',
'client_secret' => '*********************************',
'redirect' => 'http://localhost:8000/login/callback/facebook',
],
composer require laravel/socialite
'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
]
'aliases' => [
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]
'google' => [
'client_id' => 'Google ID',
'client_secret' => 'Google key',
'redirect' => 'http://localhost:8000/callback/google',
],
'facebook' => [
'client_id' => 'facebook id',
'client_secret' => 'facebook key',
'redirect' => 'http://localhost:8000/login/callback/facebook',
],
php artisan make:controller GoogleController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
use Laravel\Socialite\Facades\Socialite;
use App\User;
class GoogleLoginController 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,
'password' => encrypt('user@123'),
'provider' => $provider,
'provider_id' => $getInfo->id
]);
}
return $user;
}
}
php artisan make:controller FacebookController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\User;
use Illuminate\Support\Facades\Hash;
class FacebookLoginController extends Controller
{
public function redirectToProvider($provider){
return Socialite::driver($provider)->redirect();
}
public function handelRedirectCallback($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,
'password' => encrypt('user@123'),
'provider' => $provider,
'provider_id' => $getInfo->id
]);
}
return $user;
}
}
Route::get('/auth/redirect/{provider}', 'GoogleLoginController@redirect');
Route::get('/callback/{provider}', 'GoogleLoginController@callback');
Route::get('/auth/redirect/{provider}', 'FacebookLoginController@redirectToProvider');
Route::get('/login/callback/{provider}', 'FacebookLoginController@handelRedirectCallback');