Login via QR code in Laravel

Posted By : Piyush Mishra | 29-Jun-2022

What is a QR code?

QR code is a type matrix invented in 1994 by a Japanese company. and used for the various tasks to perform. commonly used for accept payments in phone pay, Paytm, etc and now the trend become popular for login via QR code so today we are going to learn the same.

In this article, we will build a QR code for login in the WebApp.

 

Step 1:- First create a file with the name LoginController.php in Controllers

Step 2:- Now create the Qrcode action function

 

public function CreateQrcodeAction(){

// ob_start ();

// echo url()->current();

$url = url(''); // Get the current url

// dd($url);

$http = $url .'/api/login/mobile/scan/qrcode'; // Verify the url method of scanning code

$key = Str::random(30);//$this->getRandom(30); // The key value stored in Memcached, a random 32-bit string

$random = mt_rand(1000000000, 9999999999);//random integer

$_SESSION['qrcode_name'] = $key ; // Save the key as the name of the picture in the session

$forhash=substr( $random,0,2);

$sgin_data = HashUserID($forhash); // The basic algorithm for generating the sign string

$sgin =strrev(substr($key,0,2)).$sgin_data ; // Intercept the first two digits and reverse them

$value = $http .'?key='. $key .'&type=1'; // Two-dimensional Code content

$pngImage = QrCode::format('png')

// ->merge(public_path('frontend/img/streamly-logo.png'), 0.3, true)

->size(300)->errorCorrection('H')

->generate($value, public_path('assets/img/qrcodeimg/'. $key .'.png'));

$return = array ('status'=>0,'msg'=>'' );

$qr = public_path('assets/img/qrcodeimg/'. $key .'.png');

// $qr = asset('assets/img/qrcodeimg/'. $key .'.png');

// dump($qr);

if (!file_exists($qr)) {

$return = array ('status'=>0,'msg'=>'' );

return response()->json($return, 404);

// return "no found qr img";

}

$qr = asset('assets/img/qrcodeimg/'. $key .'.png');

$mem = new \Memcached();

$mem->addServer('127.0.0.1',11211 );

$res=json_encode(array('sign'=> $sgin ,'type'=>0 ));

// store in Memcached, expiration time is three minutes

$mem->set($key,$res ,180);// 180

$return = array('status'=>1,'msg'=> $qr,'key'=>$key);

return response()->json($return, 200);

// return $this ->createJsonResponse( $return );

}

 

Step 3:- Create action function for QR code scan

I will give the response when the QR code gets scanned.


 

public function isScanQrcodeAction(Request $request){

 

$key = $request['key'];

$mem = new \Memcached();

$mem->addServer('127.0.0.1',11211 );

$data = json_decode($mem->get($key),true);

if (empty($data)){

$return = array ('status'=>2,'msg'=>'expired' );

} else {

if ($data['type']){

$return = array ('status'=>1,'msg'=>'success' );

 

} else {

$return = array ('status'=>0,'msg'=>'' );

}

}

return response()->json($return, 200);

// return $this->createJsonResponse( $return );

}

 

Step 4:- Create a function for login action after success response from QrCode scanned function

from step 3. if QrCode scanned function returns a positive response then the login action function will log in the user otherwise gives an alert and didn't respond. now we have to create a scanned login function name qrcodeDoLoginAction.


 

public function qrcodeDoLoginAction(Request $request ){

 

$login=$_GET['login'];//jwt or passcode

$key = $_GET['key'];

$sign = $_GET['sign'];

$mem = new \Memcached();

$mem->addServer('127.0.0.1',11211);

$data = json_decode($mem->get($key),true); // Remove the value of Memcached

if (empty($data)){

$return = array ('status'=>2,'msg'=>'expired' );

return response()->json($return, 200);

} else {

if (!isset($data['sign'])){

$return = array('status'=>0,'msg'=>'Sign notset' );

}

if ($data['sign']!= $sign ){ // Verify delivery Sign

$return = array('status'=>0,'msg'=>'Verification Error' );

// return $this ->createJsonResponse( $return );

return response()->json($return, 403);

}

 

Now the user will be login when the user will get success response from the function after scanning the QrCode.



Thanks 
Happy Coding...

 

About Author

Author Image
Piyush Mishra

Piyush Mishra is a skilled Backend Developer with over 3+ years of experience in the industry. He has an in-depth understanding of the latest technologies and practical experience in working with various tools and frameworks such as PHP, Laravel Framework, Wordpress CMS, Nodejs, Apache, Javascript, Jquery, and databases. He possesses a creative mindset and strong analytical skills that allow him to think critically and explore new technologies with ease.He has worked on several noteworthy projects, including NCG Television, INW (I Need a Word), and StreamlyTV. For NCG Television, he created the backend of an OTT platform with a subscription-based model using Wordpress. For INW, he developed an OTT platform with live streaming, podcast features, and stories functionality using Wordpress, Vimeo, and Nodejs on the backend. For StreamlyTV, he developed a channel live streaming platform using Laravel, Wowza Streaming Engine, AWS, and Stripe payment gateway on the backend.

Request for Proposal

Name is required

Comment is required

Sending message..