Skip to content

Instantly share code, notes, and snippets.

@saeedvir
Last active November 26, 2024 20:19
Show Gist options
  • Save saeedvir/19b43f0c03c29b04a9fa6d360583b3ed to your computer and use it in GitHub Desktop.
Save saeedvir/19b43f0c03c29b04a9fa6d360583b3ed to your computer and use it in GitHub Desktop.
Laravel QR-Code Verification Code
<?php
// composer require sebastiandevs/simplethenticator
use Illuminate\Http\Request;
use src\SimpleAuthenticator;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
$auth = new SimpleAuthenticator(6, 'SHA1');
try
{
$secret = $auth->createSecret();
}
catch (Exception $e)
{
echo $e->getMessage();
exit();
}
session(['QRCODE_SECRET' => $secret]);
$qrCodeUrl = $auth->getQRCodeGoogleUrl($secret, config('app.url'), config('app.name'));
return view('qrcode',compact('qrCodeUrl'));
});
Route::post('/verify', function (Request $request) {
$auth = new SimpleAuthenticator(6, 'SHA1');
$secret = session('QRCODE_SECRET',null);
$request->validate([
'code' => 'required|numeric|size:6',
]);
$oneCode = $request->get('code',null);
$verify2FA = $auth->verifyCode($secret, $oneCode, 2)? true: false;
return $verify2FA === true ? 'ok' : 'NOT OK !!';
});
@saeedvir
Copy link
Author

saeedvir commented Nov 26, 2024

qrcode.blade.php

<html lang="fa" dir="ltr">
<head>
    <!--
        https://github.com/saeedvir

        and

        https://virgool.io/@saeed.es91
    -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel QR-Code Verification</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity_no="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-12 col-md-6 offset-md-3">
                <form
                    class="form my-2 bg-secondary px-2 py-2 rounded shadow-lg border border-info border-start-0 text-center"
                    action="/verify" method="POST">
                    @csrf
                    <div class="form-group">
                        <h4 class="text-white">Scan With <a
                                href="https://play.google.com/store/apps/details?id=com.azure.authenticator&hl=en&pli=1">Microsoft
                                Authenticator App </a> And Enter Code</h4>
                        <img class="img-fluid my-4" src="{{ $qrCodeUrl }}" alt="">
                    </div>
                    <div class="form-group my-2">
                        <input autofocus required type="text" name="code" placeholder="Code Here"
                            class="form-control">
                    </div>
                    <button type="submit" class="btn btn-success">Verify now</button>
                </form>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity_no="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous">
    </script>

</body>

</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment