Laravel Socialite package Implementation

It is used to make a communication with social applications. If you want to authenticate your application without login or registration then you need oauth login or oauth registration. Here, I will use laravel-socialite package to implement oauth. It is described step by step as belows:

Step 1: fill your config/services.php file with below credentials, you should take your expected social app's credentials:

<?php
return [

'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT'),
],

'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
],

'twitter' => [
'client_id' => env('TWITTER_CLIENT_ID'),
'client_secret' => env('TWITTER_CLIENT_SECRET'),
'redirect' => env('TWITTER_REDIRECT'),
],

'linkedin' => [
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
'redirect' => env('LINKEDIN_REDIRECT'),
],

'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT'),
],
]

Step 2: Taking social apps ( facebook, twitter, linkedin etc) client_id, secret_key with redirect url where ( social app will send reply request to your app)

Step 3: making two routes for social app one for sending request and other for accepting reply
 request.

// Social github routes...
Route::get('github', 'Auth\RegisterController@redirectToProvider');
Route::get('github/callback', 'Auth\RegisterController@handleProviderCallback');

// Social facebook routes...
Route::get('facebook', 'Auth\RegisterController@redirectToProvider');
Route::get('facebook/callback', 'Auth\RegisterController@handleProviderCallback');

// Social google routes...
Route::get('google', 'Auth\RegisterController@redirectToProvider');
Route::get('google/callback', 'Auth\RegisterController@handleProviderCallback');

// Social twitter routes...
Route::get('twitter', 'Auth\RegisterController@redirectToProvider');
Route::get('twitter/callback', 'Auth\RegisterController@handleProviderCallback');

// Social linkedin routes...
Route::get('linkedin', 'Auth\RegisterController@redirectToProvider');
Route::get('linkedin/callback', 'Auth\RegisterController@handleProviderCallback');

Step 4: making a controller according to your routes. I have made a controler named RegisterController within this controller making two method named 'redirectToProvider()', 'handleProviderCallback()'.

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
// use Illuminate\Support\Collection;
use App\User;

use Socialite;
use Log;

class Register extends Controller
{


public $requestPath;
protected $redirectTo = '/home';

public function __construct(Request $request){
$this->requestPath = $request->path();
$this->middleware('guest', ['except' => 'logout']);
}

public function redirectToProvider(){
Log::info($this->requestPath);
return Socialite::driver($this->requestPath)->redirect();
}

public function handleProviderCallback(Request $request){
$driver = explode('/', $this->requestPath);
$driverName = $driver[0];

$user = Socialite::driver($driverName)->user(); //making an object for retrieving data

$email = $user->getEmail();

if(User::count() == 0){
$insertedData = [
'name' => $user->getName(),
'email' => $email,
'status' => 'Active',
'platform' => $driverName,
'oauth_user_id' => $user->getId(),
'photo' => $user->getAvatar(),
'role' => 'superadmin'
];
}
$id = User::where('email', $email)->pluck('id');
Log::alert($id[0]);
if($id->isEmpty()){
$insertedData = [
'name' => $user->getName(),
'email' => $email,
'status' => 'Active',
'platform' => $driverName,
'oauth_user_id' => $user->getId(),
'photo' => $user->getAvatar(),
];

Auth::login(User::create($insertedData));
return redirect()->intended('/home');
}

Auth::login(User::find($id[0]));
return redirect()->intended('/home');
}
}


Step 5: fill with credentials in .env file:

GITHUB_CLIENT_ID=*****************
GITHUB_CLIENT_SECRET=*****************
GITHUB_REDIRECT=http://local.email.com/github/callback

FACEBOOK_CLIENT_ID=*****************
FACEBOOK_CLIENT_SECRET=*****************
FACEBOOK_REDIRECT=https://local.email.com/facebook/callback

GOOGLE_CLIENT_ID=*****************
GOOGLE_CLIENT_SECRET=*****************
GOOGLE_REDIRECT=http://local.email.com/google/callback

TWITTER_CLIENT_ID=*****************
TWITTER_CLIENT_SECRET=*****************
TWITTER_REDIRECT=http://local.email.com/twitter/callback

LINKEDIN_CLIENT_ID=*****************
LINKEDIN_CLIENT_SECRET=*****************
LINKEDIN_REDIRECT=http://local.email.com/linkedin/callback


Now execute your code and enjoy!!!!!!!!!!!

Comments

Popular posts from this blog

composer installation on aws ec2 instance ami ubuntu 18.04

nginx installation on aws ec2 instance ami ubuntu 18.04

PHP7.3 installation on aws ec2 instance ami ubuntu 18.04