Wednesday, August 1, 2018

Laravel Allow Single Login Only


Recently in one of my project we need functionality to allow only single session for user. So if I am logged in with one user account then I can not use it on other device. If they try to do so, it will log you out from first last session and will continue with current session.

So here is the trick I have used is to store active session id to users table and then check it with new session id on login. If both are not same then destroy the old session.

So first of all create migration and add session id column in users table.

Schema::table('users', function($table)

Now in your AuthController or LoginController add following function after user is authenticated.

$user = \Auth::user();
$currentSessionId = $request->session()->getId();
if($lastSessionId != null){
if($currentSessionId != $lastSessionId){
//destroy last session
$user->session_id = $currentSessionId;

So here it will destroy old session and create new one. Please note this is one way to achieve single login. There could be better trick, in that case please share here.lara

Saturday, July 21, 2018

Step By Step : Laravel - Publish Post on Facebook Page with Graph API


In this quick blog we will quickly go through how to publish a post on Facebook Page with graph API from your Laravel Application.

Step 1 : Create Facebook App

Login to with your Facebook account and create an app.

Now go to Settings -> Basic of your Facebook app and copy app id and app secret.

Step 2 : Install Facebook PHP SDK

Run following command to install PHP SDK in your laravel application.

composer require facebook/graph-sdk

Step 3: Get Exchange Token from Facebook App

Go to Facebook Graph API Explorer. Here is the link

From here first select your application and then select the page for Page Access Token

When you are generating this token please select graph API version 2.2 and select the following permissions.


Step 4 : Generate Access Token 

From the exchange token generated in Step 3, Get the access token.

$url = ' YOUR_APP_ID&client_secret= YOUR_APP_SECRET&fb_exchange_token=YOUR_TOKEN';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$output = json_decode($result);
$access_token = $output->access_token;

Step 5 : Call Facebook Graph API to Send Post to Page

Create SDK object

$fb = new \Facebook\Facebook([
'app_id' => 'YOUR_APP_ID',
'app_secret' => 'YOUR_APP_SECRET',
'default_graph_version' => 'v2.2',

Generate Payload

$linkData = [
 'link' => YOUR_LINK,
 'message' => YOUR_TITLE

Call Graph API.

$pageAccessToken =$access_token;

try {
 $response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
 echo 'Graph returned an error: '.$e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
 echo 'Facebook SDK returned an error: '.$e->getMessage();
$graphNode = $response->getGraphNode();

Now check your Facebook page there will be post on your Facebook page. Hope this helps you.

Friday, April 20, 2018

Laravel ValidationException Handling

This is the creepiest thing I have ever fixed in Laravel. I can't even imagine that in Laravel we can have such creepy problem. But anyways other than this I really like Laravel framework.

First let me explain the problem. In our Laravel 5.5 application we have added exception handling in app/Exceptions/Handler.php file

public function render($request, Exception $exception)

Now the problem was in case of form validations it was throwing ValidationException so in case of returning back to form it always took me to the error page and I am not able to see what are the validation issues. Now that was really strange. So there were two options . First, I have to remove server side validations so it does not throw ValidationException and we shall do all client side validations. Or find out some other way to handle this.

So after 3 to 4 hours of struggle of going though framework code and documentation and online help I finally figure out the solution. Here are steps to add it.

1) Step 1 : Add  ValidationException in dontReport field in app/Exceptions/Handler.php

protected $dontReport = [

2) Step 2 : Update the render method declaration

public function render($request, Exception $exception)
       return $this->convertValidationExceptionToResponse($exception, $request);

So here first we are checking if the current exception to be reported or not by checking shouldReport function

If not to be reported then we are using convertValidationExceptionToResponse method of super class to generate the response and send it back.

Please note that this solution will only work